Skip to content

Instantly share code, notes, and snippets.

@dzentota
Last active April 19, 2020 20:12
Show Gist options
  • Save dzentota/8ce0167e1aeacb8ce54f9793f2ea6baf to your computer and use it in GitHub Desktop.
Save dzentota/8ce0167e1aeacb8ce54f9793f2ea6baf to your computer and use it in GitHub Desktop.
ItemName.php
<?php
declare(strict_types=1);
final class ItemName
{
private $value;
private function __contruct()
{}
public static function fromString(string $value): self
{
if (!preg_match('~^[a-z0-9]+(\.png|\.jpg)$~is', $value)) {
throw new DomainException('Input contains invalid characters: ' . $value);
}
$itemName = new self();
$itemName->value = $value;
return $itemName;
}
public function value(): string
{
return $this->value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment