Skip to content

Commit ab20aea

Browse files
committed
Lesson 26 Composer as dependency manager
1 parent f22ae06 commit ab20aea

File tree

226 files changed

+20462
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+20462
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Este curso explica programación orientada a objetos desde cero, para verlo sól
7171

7272
25. [Creación de macros con traits, métodos estáticos y __call](https://styde.net/creacion-de-macros-en-php-usando-traits-metodos-estaticos-y-__call/) - 16:35
7373

74-
26. Instalación y uso de paquetes con Composer
74+
26. [Instalación y uso de paquetes con Composer](https://styde.net/instalacion-y-uso-de-componentes-de-terceros-con-composer/) - 10:39
7575

7676
27. Desarrollo de métodos con pruebas automatizadas
7777

lesson26/composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "styde/oop",
3+
"description": "Proyecto demo para el curso de programacion orientada a objetos",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Duilio Palacios",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"nesbot/carbon": "^1.21"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Styde\\": "src/"
18+
}
19+
}
20+
}

lesson26/composer.lock

+189
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lesson26/public/index.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require "../vendor/autoload.php";
4+
5+
use Styde\User;
6+
7+
$user = new User([
8+
'name' => 'Walter White',
9+
'birthDate' => '07/09/1959',
10+
]);
11+
12+
echo "<p>{$user->name} tiene {$user->age} años";

lesson26/src/Model.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
abstract class Model
6+
{
7+
protected $attributes = [];
8+
9+
public function __construct(array $attributes = [])
10+
{
11+
$this->fill($attributes);
12+
}
13+
14+
public function fill(array $attributes = [])
15+
{
16+
$this->attributes = $attributes;
17+
}
18+
19+
public function getAttributes()
20+
{
21+
return $this->attributes;
22+
}
23+
24+
public function getAttribute($name)
25+
{
26+
$value = $this->getAttributeValue($name);
27+
28+
if ($this->hasGetMutator($name)) {
29+
return $this->mutateAttribute($name, $value);
30+
}
31+
32+
return $value;
33+
}
34+
35+
protected function hasGetMutator($name)
36+
{
37+
return method_exists($this, 'get'.Str::studly($name).'Attribute');
38+
}
39+
40+
protected function mutateAttribute($name, $value)
41+
{
42+
return $this->{'get'.Str::studly($name).'Attribute'}($value);
43+
}
44+
45+
public function getAttributeValue($name)
46+
{
47+
if (array_key_exists($name, $this->attributes)) {
48+
return $this->attributes[$name];
49+
}
50+
}
51+
52+
public function setAttribute($name, $value)
53+
{
54+
$this->attributes[$name] = $value;
55+
}
56+
57+
public function __set($name, $value)
58+
{
59+
$this->setAttribute($name, $value);
60+
}
61+
62+
public function __get($name)
63+
{
64+
return $this->getAttribute($name);
65+
}
66+
67+
public function __isset($name)
68+
{
69+
return isset($this->attributes[$name]);
70+
}
71+
72+
public function __unset($name)
73+
{
74+
unset ($this->attributes[$name]);
75+
}
76+
}

lesson26/src/Str.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
class Str
6+
{
7+
public static function studly($value)
8+
{
9+
$result = ucwords(str_replace('_', ' ', $value));
10+
11+
return str_replace(' ', '', $result);
12+
}
13+
14+
public static function snake($value)
15+
{
16+
$result = preg_replace("@([A-Z])@", "_$1", $value);
17+
return strtolower(trim($result, '_'));
18+
}
19+
}

lesson26/src/User.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
use Exception;
6+
use Carbon\Carbon;
7+
8+
class User extends Model
9+
{
10+
public function getAgeAttribute()
11+
{
12+
$date = Carbon::createFromFormat('d/m/Y', $this->birthDate);
13+
14+
return $date->age;
15+
}
16+
}
17+
18+
19+
20+
21+
22+
23+

lesson26/storage/user.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
O:10:"Styde\User":1:{s:2:"id";i:10;}

lesson26/vendor/autoload.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer' . '/autoload_real.php';
6+
7+
return ComposerAutoloaderInit694845635ff07f784c90126d627b86c2::getLoader();

0 commit comments

Comments
 (0)