@ 2013-05-31T11:19:20Z <?php
class Book
{
protected $title;
protected $author;
protected $genres;
public function __construct()
{
$this->genres = new ArrayObject();
}
public function addGenre($genre)
{
$this->genres[] = $genre;
return $this;
}
public function getGenres()
{
return $this->genres;
}
public function setAuthor(Person $author)
{
$this->author = $author;
return $this;
}
public function getAuthor()
{
return $this->author;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
}
class Person
{
protected $firstName;
protected $lastName;
public function __construct($firstName = null, $lastName = null)
{
$this->setFirstName($firstName);
$this->setLastName($lastName);
}
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
public function getFirstName()
{
return $this->firstName;
}
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
public function getLastName()
{
return $this->lastName;
}
}
interface Hydrator
{
public function hydrate($object, array $data);
public function extract($object);
}
class ReflectionHydrator implements Hydrator
{
public function hydrate($object, array $data)
{
$reflection = new ReflectionClass($object);
$transform = function ($key) {
return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
};
foreach ($reflection->getMethod() as $method) {
if (!$method->isPublic() || 0 !== strpos($method->getName(), 'set')) {
continue;
}
$field = lcfirst(substr($method->getName(), 3));
// @todo
}
}
public function extract($object)
{
$data = array();
$reflection = new ReflectionClass($object);
$transform = function ($key) {
return '_' . strtolower(array_shift($key));
};
foreach ($reflection->getMethods() as $method) {
if (!$method->isPublic() || 0 !== strpos($method->getName(), 'get')) {
continue;
}
$field = lcfirst(substr($method->getName(), 3));
$normalized = preg_replace_callback('/([A-Z])/', $transform, $field);
$data[$normalized] = $method->invoke($object);
}
return $data;
}
}
class DryReflectionHydrator implements Hydrator
{
public function extract($object)
{
$transform = function ($key) {
return '_' . strtolower(array_shift($key));
};
$data = array();
// Process get methods
$this->processMethods('get', function ($field, ReflectionMethod $method) use (&$data, $transform) {
$normalized = preg_replace_callback('/([A-Z])/', $transform, $field);
$data[$normalized] = $method->invoke(null);
});
return $data;
}
public function hydrate($object, array $data)
{
$transform = function ($key) {
return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
};
$this->processMethods('set', function($field, ReflectionMethod $method) use ($data, $transform) {
$method->invoke(null, $data[$transform($field)]);
});
}
protected function processMethods($prefix, $callback)
{
$reflection = new ReflectionClass($object);
foreach ($reflection->getMethods() as $method) {
if (!$method->isPublic() || 0 !== strpos($method->getName(), $prefix)) {
continue;
}
$field = lcfirst(substr($method->getName(), strlen($prefix)));
$callback($field, $method);
}
}
}
$book = new Book();
$book->setTitle('Harry potter');
$book->addGenre('Drama')->addGenre('Nosense');
$book->setAuthor(new Person('James', 'Bond'));
$hydrator = new ReflectionHydrator();
var_dump($hydrator->extract($book));
var_dump($hydrator->extract($book->getAuthor());
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.15 0.011 0.047 12.44 5.4.14 0.020 0.054 12.13 5.4.13 0.013 0.040 12.11 5.4.12 0.013 0.041 12.08 5.4.11 0.012 0.041 12.07 5.4.10 0.016 0.059 12.07 5.4.9 0.012 0.046 12.07 5.4.8 0.012 0.043 12.07 5.4.7 0.017 0.066 12.07 5.4.6 0.009 0.044 12.07 5.4.5 0.016 0.039 12.07 5.4.4 0.017 0.059 12.05 5.4.3 0.012 0.057 12.05 5.4.2 0.018 0.060 12.05 5.4.1 0.013 0.044 12.05 5.4.0 0.010 0.045 11.54 5.3.25 0.011 0.048 12.76 5.3.24 0.014 0.045 12.75 5.3.23 0.014 0.043 12.75 5.3.22 0.012 0.046 12.72 5.3.21 0.018 0.062 12.72 5.3.20 0.015 0.042 12.71 5.3.19 0.014 0.044 12.71 5.3.18 0.014 0.043 12.71 5.3.17 0.011 0.045 12.71 5.3.16 0.013 0.043 12.71 5.3.15 0.015 0.042 12.71 5.3.14 0.015 0.042 12.70 5.3.13 0.012 0.045 12.70 5.3.12 0.018 0.039 12.70 5.3.11 0.017 0.060 12.70 5.3.10 0.016 0.042 12.18 5.3.9 0.012 0.044 12.17 5.3.8 0.012 0.055 12.15 5.3.7 0.013 0.044 12.15 5.3.6 0.018 0.039 12.14 5.3.5 0.013 0.043 12.09 5.3.4 0.012 0.042 12.09 5.3.3 0.016 0.039 12.05 5.3.2 0.013 0.042 11.83 5.3.1 0.017 0.036 11.79 5.3.0 0.012 0.042 11.78
preferences:dark mode live preview
142.09 ms | 1394 KiB | 7 Q