1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Talentify\ValueObject\Geography\Address; |
||
6 | |||
7 | use InvalidArgumentException; |
||
8 | use Talentify\ValueObject\StringUtils; |
||
9 | use Talentify\ValueObject\ValueObject; |
||
10 | |||
11 | /** |
||
12 | * @see https://en.wikipedia.org/wiki/Street_or_road_name |
||
13 | */ |
||
14 | class Street implements AddressElement |
||
15 | { |
||
16 | /** @var string */ |
||
17 | protected $name; |
||
18 | /** @var string */ |
||
19 | protected $number; |
||
20 | /** @var string|null other identifiers such as house or apartment numbers */ |
||
21 | protected $otherIdentifiers; |
||
22 | |||
23 | /** |
||
24 | * @throws \InvalidArgumentException if supplied value is invalid. |
||
25 | */ |
||
26 | public function __construct(string $name, ?string $number = null, ?string $otherIdentifiers = null) |
||
27 | { |
||
28 | $this->setName($name); |
||
29 | $this->setNumber($number); |
||
30 | $this->setOthers($otherIdentifiers); |
||
31 | } |
||
32 | |||
33 | protected function setName(string $name) : void |
||
34 | { |
||
35 | $normalized = StringUtils::trimSpacesWisely($name); |
||
36 | if (empty($normalized)) { |
||
37 | throw new InvalidArgumentException(sprintf('The value "%s" is not a valid street name.', $name)); |
||
38 | } |
||
39 | |||
40 | $this->name = StringUtils::convertCaseToTitle($normalized); |
||
41 | } |
||
42 | |||
43 | public function getName() : string |
||
44 | { |
||
45 | return $this->name; |
||
46 | } |
||
47 | |||
48 | protected function setNumber(?string $number = null) : void |
||
49 | { |
||
50 | if ($number === null) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $normalized = StringUtils::trimSpacesWisely($number); |
||
55 | if (empty($normalized)) { |
||
56 | throw new InvalidArgumentException(sprintf('The value "%s" is not a valid "number".', $number)); |
||
57 | } |
||
58 | |||
59 | $this->number = $number; |
||
60 | } |
||
61 | |||
62 | public function getNumber() : ?string |
||
63 | { |
||
64 | return $this->number; |
||
65 | } |
||
66 | |||
67 | protected function setOthers(?string $others = null) : void |
||
68 | { |
||
69 | if ($others === null) { |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | $normalized = StringUtils::trimSpacesWisely($others); |
||
74 | if (empty($normalized)) { |
||
75 | throw new InvalidArgumentException(sprintf('The value "%s" is not a valid "other identifiers".', $others)); |
||
76 | } |
||
77 | |||
78 | $this->otherIdentifiers = $normalized; |
||
79 | } |
||
80 | |||
81 | public function getOtherIdentifiers() : ?string |
||
82 | { |
||
83 | return $this->otherIdentifiers; |
||
84 | } |
||
85 | |||
86 | public function equals(?ValueObject $object) : bool |
||
87 | { |
||
88 | if (!$object instanceof self) { |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | return strtolower($object->getName()) === strtolower($this->getName()) && |
||
93 | strtolower($object->getNumber()) === strtolower($this->getNumber()) && |
||
94 | strtolower($object->getOtherIdentifiers()) === strtolower($this->getOtherIdentifiers()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
95 | } |
||
96 | |||
97 | public function getFormatted() : string |
||
98 | { |
||
99 | $firstPart = sprintf('%s %s', $this->getNumber(), $this->getName()); |
||
100 | if ($this->getOtherIdentifiers() && $this->getOtherIdentifiers() !== '') { |
||
101 | return $firstPart . ', ' . $this->getOtherIdentifiers(); |
||
102 | } |
||
103 | |||
104 | return $firstPart; |
||
105 | } |
||
106 | |||
107 | public function __toString() : string |
||
108 | { |
||
109 | return $this->name; |
||
110 | } |
||
111 | |||
112 | public function jsonSerialize() |
||
113 | { |
||
114 | return [ |
||
115 | 'name' => $this->name, |
||
116 | 'number' => $this->number, |
||
117 | 'otherIdentifier' => $this->otherIdentifiers, |
||
118 | ]; |
||
119 | } |
||
120 | } |
||
121 |