Created
August 10, 2014 05:47
-
-
Save coderabbi/254f4d87c5cc35c6ef03 to your computer and use it in GitHub Desktop.
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
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