| Total Complexity | 7 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
| 1 | <?php |
||
| 8 | trait StringTrait |
||
| 9 | { |
||
| 10 | private $value; |
||
| 11 | |||
| 12 | final public static function fromNative($value) |
||
| 13 | { |
||
| 14 | if ($value instanceof ValueObjectInterface) { |
||
| 15 | $value = $value->toNative(); |
||
| 16 | } |
||
| 17 | if (is_array($value)) { |
||
| 18 | throw new InvalidValueForValueObjectException($value, static::class); |
||
| 19 | } |
||
| 20 | return new self((string) $value); |
||
| 21 | } |
||
| 22 | |||
| 23 | final public function __construct(string $value) |
||
| 24 | { |
||
| 25 | if (!$this->validValue($value)) { |
||
| 26 | throw new InvalidValueForValueObjectException($value, static::class); |
||
| 27 | } |
||
| 28 | $this->value = $this->sanitizeValue($value); |
||
| 29 | } |
||
| 30 | |||
| 31 | abstract protected function validValue(string $value): bool; |
||
| 32 | |||
| 33 | abstract protected function sanitizeValue(string $value): string; |
||
| 34 | |||
| 35 | final public function toNative() |
||
| 38 | } |
||
| 39 | |||
| 40 | final public function __toString(): string |
||
| 43 | } |
||
| 44 | } |
||
| 45 |