@ 2013-05-31T11:17:10Z <?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, )
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');
$hydrator = new ReflectionHydrator();
var_dump($hydrator->extract($book));
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.012 0.042 12.38 5.4.14 0.012 0.042 12.08 5.4.13 0.011 0.043 12.06 5.4.12 0.013 0.039 12.02 5.4.11 0.012 0.042 12.02 5.4.10 0.011 0.043 12.02 5.4.9 0.013 0.041 12.02 5.4.8 0.012 0.040 12.02 5.4.7 0.010 0.042 12.02 5.4.6 0.014 0.059 12.01 5.4.5 0.009 0.043 12.02 5.4.4 0.013 0.038 12.00 5.4.3 0.017 0.056 12.00 5.4.2 0.012 0.039 12.00 5.4.1 0.013 0.040 12.00 5.4.0 0.010 0.043 11.49 5.3.25 0.016 0.066 12.72 5.3.24 0.012 0.044 12.72 5.3.23 0.013 0.044 12.71 5.3.22 0.014 0.042 12.68 5.3.21 0.011 0.045 12.68 5.3.20 0.012 0.044 12.68 5.3.19 0.019 0.058 12.68 5.3.18 0.014 0.042 12.67 5.3.17 0.015 0.041 12.67 5.3.16 0.015 0.043 12.67 5.3.15 0.016 0.057 12.67 5.3.14 0.015 0.044 12.66 5.3.13 0.014 0.043 12.66 5.3.12 0.020 0.057 12.66 5.3.11 0.019 0.052 12.66 5.3.10 0.013 0.044 12.13 5.3.9 0.013 0.041 12.10 5.3.8 0.020 0.056 12.09 5.3.7 0.009 0.047 12.09 5.3.6 0.011 0.046 12.08 5.3.5 0.016 0.050 12.03 5.3.4 0.012 0.044 12.03 5.3.3 0.017 0.052 11.99 5.3.2 0.019 0.049 11.77 5.3.1 0.011 0.042 11.73 5.3.0 0.014 0.040 11.72
preferences:dark mode live preview
144.11 ms | 1399 KiB | 7 Q