Skip to content

Instantly share code, notes, and snippets.

@vv12131415
Created April 10, 2020 19:01
Show Gist options
  • Select an option

  • Save vv12131415/3a07b15e1e021eb2a9440a27ddd326a3 to your computer and use it in GitHub Desktop.

Select an option

Save vv12131415/3a07b15e1e021eb2a9440a27ddd326a3 to your computer and use it in GitHub Desktop.
VO with external service
<?php
declare(strict_types=1);
final class PostCode
{
/** @var string */
private $code;
private function __construct()
{
}
public static function create(string $code, Country $country, PostCodeValidatorInterface $postCodeValidator): PostCode
{
if (!$postCodeValidator->isValid($country, $code)) {
throw new InvalidArgumentException('Post code is not valid for ' . $country->getName());
}
$self = new self();
$self->code = $code;
return $self;
}
public static function createWithClosure(string $code, Country $country, Closure $postCodeValidator): PostCode
{
if (!$postCodeValidator($country, $code)) {
throw new InvalidArgumentException('Post code is not valid for ' . $country->getName());
}
$self = new self();
$self->code = $code;
return $self;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment