Last active
April 19, 2020 20:12
-
-
Save dzentota/8ce0167e1aeacb8ce54f9793f2ea6baf to your computer and use it in GitHub Desktop.
ItemName.php
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
<?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