Last active
September 28, 2015 08:37
-
-
Save manuakasam/d33d2f8430abcd091bdd to your computer and use it in GitHub Desktop.
Dafaq
This file contains 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 | |
trait WithTrait | |
{ | |
public function with($key, $value) { | |
if (false === array_key_exists($key, get_class_vars(self::class))) { | |
sprintf( | |
'Invalid key called. Key %s does not exist in class %s', | |
$key, | |
self::class | |
) | |
} | |
$new = clone $this; | |
$new->$key = $value; | |
return $new; | |
} | |
} | |
class Foo | |
{ | |
protected $title; | |
use WithTrait; | |
/** | |
* Essentially ends up at | |
* Invalid key called. Key title does not exist in class WithTrait | |
*/ | |
} |
This file contains 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 | |
/** | |
* @author Manuel Stosic <[email protected]> | |
*/ | |
namespace DrWolff\EventManagement\Hydrator\Seminar; | |
use DrWolff\EventManagement\Entity\Seminar\CoreData; | |
use Zend\Stdlib\Hydrator\HydratorInterface; | |
final class FooHydrator implements HydratorInterface | |
{ | |
/** | |
* Hydrate $object with the provided $data. | |
* | |
* @param array $data | |
* @param object $object | |
* @return object | |
*/ | |
public function hydrate(array $data, $object) | |
{ | |
foreach ($data as $key => $value) { | |
$object = $object->with($key, $value); | |
} | |
return $object; | |
} | |
/** | |
* Extract values from an object | |
* | |
* @param CoreData $object | |
* @return array | |
*/ | |
public function extract($object) | |
{ | |
return [ | |
'id' => $object->getId(), | |
'title' => $object->getTitle(), | |
'description' => $object->getDescription(), | |
// etc..... | |
]; | |
} | |
} |
This file contains 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 | |
final class FooFieldset extends Fieldset | |
{ | |
public function __construct($name = 'foo-fieldset', $options = []) | |
{ | |
parent::__construct($name, $options); | |
} | |
public function init() | |
{ | |
$this->setHydrator(new FooHydrator()); | |
$this->setObject(new Foo()); | |
} | |
} |
This file contains 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 | |
class Foo | |
{ | |
protected $title; | |
public function with($key, $value) { | |
if (false === array_key_exists($key, get_class_vars(self::class))) { | |
sprintf( | |
'Invalid key called. Key %s does not exist in class %s', | |
$key, | |
self::class | |
) | |
} | |
$new = clone $this; | |
$new->$key = $value; | |
return $new; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment