Skip to content

Instantly share code, notes, and snippets.

@codenamegary
Created February 19, 2014 22:01
Show Gist options
  • Save codenamegary/9102600 to your computer and use it in GitHub Desktop.
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
<?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