Created
February 19, 2014 22:01
-
-
Save codenamegary/9102600 to your computer and use it in GitHub Desktop.
Just a random example of how a ContainerBuilder for DataContain might be implemented, needs work
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 | |
class Entity extends DataContain\Container { | |
/** | |
* @var DataContain\DefinitionContainer | |
*/ | |
protected $attributes; | |
public function __construct(array $data = array(), DataContain\DefinitionContainer $attributes = null) | |
{ | |
// Make a new definition container if one isn't passed | |
if(null === $attributes) $attributes = new DataContain\DefinitionContainer; | |
// Set some attributes for this entity | |
$this->attributes = $attributes; | |
// Call the actual 'DataContain\Container' constructor | |
parent::__construct($data, $attributes); | |
} | |
// Must implement | |
public function getDefinitionConfig() | |
{ | |
return $this->attributes; | |
} | |
} | |
class User extends Entity { | |
public function __construct(array $data = array()) | |
{ | |
$builder = new DataContain\ContainerBuilder; | |
$builder->extend($someOtherContainer); | |
$builder->setAttributes(array('name', 'email', 'role')); | |
$builder->addStringValidator('name', ContainerBuilder::REQUIRED, 'John Doe'); | |
$builder->addConstraint('name', ContainerBuilder::UNIQUE); | |
$builder->addEnumValidator('role', ContainerBuilder::REQUIRED, array('user', 'guest', 'employee')); | |
$userValidator = $builder->make(); | |
// Manually set the role, else this thing throws an exception | |
$data['role'] = 'user'; | |
// Call the Entity constructor | |
parent::__construct($data, $userValidator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment