Skip to content

Instantly share code, notes, and snippets.

@manuakasam
Last active September 28, 2015 08:37
Show Gist options
  • Save manuakasam/d33d2f8430abcd091bdd to your computer and use it in GitHub Desktop.
Save manuakasam/d33d2f8430abcd091bdd to your computer and use it in GitHub Desktop.
Dafaq
<?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
*/
}
<?php
/**
* @author Manuel Stosic <manuel.stosic@krankikom.de>
*/
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.....
];
}
}
<?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());
}
}
<?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