Skip to content

Instantly share code, notes, and snippets.

@coderabbi
Created August 10, 2014 05:47
Show Gist options
  • Save coderabbi/254f4d87c5cc35c6ef03 to your computer and use it in GitHub Desktop.
Save coderabbi/254f4d87c5cc35c6ef03 to your computer and use it in GitHub Desktop.
class MyValueObject
{
/**
* @Assert\Length(min = 3)
* @Assert\NotBlank
*/
private $propertyOne;
/**
* @Assert\Length(min = 5)
* @Assert\NotBlank
*/
private $propertyTwo;
private function __construct(array $params)
{
foreach($params as $key => $value)
{
if (property_exists($this, $key))
{
$this->$key = $value;
}
}
}
public static function create(array $params)
{
$object = new self(array $params);
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
if ($validator->validate($object))
throw new \InvalidArgumentException();
return $object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment