Last active
August 29, 2015 14:05
-
-
Save coderabbi/50bde28811734fd5b41d 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
abstract class ValueObject | |
{ | |
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; | |
} | |
} | |
class Example | |
extends ValueObject | |
{ | |
/** | |
* @Assert\Length(min = 3) | |
* @Assert\NotBlank | |
*/ | |
private $propertyOne; | |
/** | |
* @Assert\Length(min = 5) | |
* @Assert\NotBlank | |
*/ | |
private $propertyTwo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment