temidaio /
value-objects
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Temidaio\ValueObjects; |
||
| 6 | |||
| 7 | use Illuminate\Support\Str; |
||
| 8 | use MichaelRubel\ValueObjects\ValueObject; |
||
| 9 | use Temidaio\ValueObjects\Formatters\StreetFormatter; |
||
| 10 | |||
| 11 | class Street extends ValueObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Internal properties. |
||
| 15 | * |
||
| 16 | * @var string|null |
||
| 17 | */ |
||
| 18 | protected ?string $prefix = null; |
||
| 19 | protected ?string $street = null; |
||
| 20 | protected ?string $number = null; |
||
| 21 | protected ?string $local = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param array|null $data |
||
| 25 | */ |
||
| 26 | 25 | final public function __construct(?array $data) |
|
| 27 | { |
||
| 28 | 25 | collect($data)->each( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 29 | 25 | fn ($value, $key) => ! property_exists($this, $key) |
|
| 30 | 25 | ?: $this->{$key} = $this->sanitize($value) |
|
| 31 | 25 | ); |
|
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | 22 | public function value(): string |
|
| 38 | { |
||
| 39 | 22 | return $this->format(); |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Sanitize the passed property. |
||
| 44 | * |
||
| 45 | * @param string|null $value |
||
| 46 | * |
||
| 47 | * @return string|null |
||
| 48 | */ |
||
| 49 | 17 | protected function sanitize(?string $value): ?string |
|
| 50 | { |
||
| 51 | 17 | return ! is_null($value) |
|
| 52 | 17 | ? Str::squish($value) |
|
| 53 | 17 | : null; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 22 | protected function format(): string |
|
| 60 | { |
||
| 61 | 22 | return format(StreetFormatter::class, [ |
|
| 62 | 22 | 'prefix' => $this->prefix, |
|
| 63 | 22 | 'street' => $this->street, |
|
| 64 | 22 | 'number' => $this->number, |
|
| 65 | 22 | 'local' => $this->local, |
|
| 66 | 22 | ]); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 22 | public function __toString(): string |
|
| 73 | { |
||
| 74 | 22 | return $this->value(); |
|
| 75 | } |
||
| 76 | } |
||
| 77 |