Created
September 8, 2012 20:45
-
-
Save vstarck/3679586 to your computer and use it in GitHub Desktop.
Ejercicio en PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace exercise; | |
class Company { | |
use Xetter; | |
private $name; | |
private $employees = array(); | |
public function __construct() { | |
$this->employees = new EmployeeCollection; | |
} | |
public function setName($name) { | |
$this->name = $name; | |
} | |
public function getName() { | |
return $this->name; | |
} | |
public function addEmployee(Employee $e) { | |
$this->employees[] = $e; | |
return $this; | |
} | |
public function getEmployees() { | |
return $this->employees; | |
} | |
public function getAgeMedia() { | |
return $this->employees->getAgeMedia(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace exercise; | |
class Designer extends Employee { | |
const TYPE_GRAPHIC = 'graphic'; | |
const TYPE_WEB = 'web'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace exercise; | |
class Developer extends Employee { | |
const TYPE_PHP = 'php'; | |
const TYPE_PYTHON = 'python'; | |
const TYPE_JAVASCRIPT = 'javascript'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace exercise; | |
class Employee { | |
use Xetter; | |
private $name; | |
private $lastname; | |
private $age; | |
private $type; | |
public function setAge($age) { | |
$this->age = $age; | |
return $this; | |
} | |
public function getAge() { | |
return $this->age; | |
} | |
public function setLastname($lastname) { | |
$this->lastname = $lastname; | |
return $this; | |
} | |
public function getLastname() { | |
return $this->lastname; | |
} | |
public function setName($name) { | |
$this->name = $name; | |
return $this; | |
} | |
public function getName() { | |
return $this->name; | |
} | |
public function setType($type) { | |
$reflection = new \ReflectionClass(get_called_class()); | |
$allowedTypes = array_values($reflection->getConstants()); | |
if(array_search($type, $allowedTypes) === false) { | |
throw new \ErrorException('Invalid type: ' .$type); | |
} | |
$this->type = $type; | |
return $this; | |
} | |
public function getType() { | |
return $this->type; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmployeeCollection extends \ArrayObject { | |
public function each($fn) { | |
foreach($this as $employee) { | |
$fn($employee); | |
} | |
} | |
public function reduce($fn, $memo) { | |
$this->each(function($employee) use($fn, &$memo) { | |
$memo = $fn($memo, $employee); | |
}); | |
return $memo; | |
} | |
public function getAgeSum() { | |
return $this->reduce(function($memo, $employee) { | |
return $memo + $employee->getAge(); | |
}, 0); | |
} | |
public function getAgeMedia() { | |
return $this->getAgeSum() / $this->count(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$summa = new Company; | |
$summa->name = 'Summa Solutions'; | |
// Property not found: employees | |
try { | |
$summa->employees = 1; | |
} catch (\Exception $e) { | |
echo $e->getMessage() . '<br/>'; | |
} | |
$valentin = new Developer; | |
// Using explicit setters | |
$valentin | |
->setName('Valentin') | |
->setLastname('Starck') | |
->setAge(25) | |
->setType(Developer::TYPE_JAVASCRIPT); | |
$summa->addEmployee($valentin); | |
$agustin = new Developer; | |
// Using __set | |
$agustin->name = 'Agustin'; | |
$agustin->lastname = 'Didiego'; | |
$agustin->age = 33; | |
$agustin->type = Developer::TYPE_PHP; | |
// throws "Invalid type: HTML" | |
try { | |
$agustin->type = 'HTML'; | |
} catch (\Exception $e) { | |
echo $e->getMessage() . '<br/>'; | |
} | |
$summa->addEmployee($agustin); | |
// throws Catchable fatal error: Argument 1 passed (...) must be an instance of exercise\Employee | |
//$summa->addEmployee(1); | |
echo $summa->getAgeMedia(); // 29 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace exercise; | |
trait Xetter { | |
public function __set($name, $value) { | |
$method = 'set' . ucfirst($name); | |
if (method_exists($this, $method)) { | |
return $this->{$method}($value); | |
} | |
throw new \ErrorException('Property not found: ' . $name); | |
} | |
public function __get($name) { | |
$method = 'get' . ucfirst($name); | |
if (method_exists($this, $method)) { | |
return $this->{$method}(); | |
} | |
throw new \ErrorException('Property not found: ' . $name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment