Street::getFormatted()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
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
It seems like $object->getOtherIdentifiers() can also be of type null; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            strtolower(/** @scrutinizer ignore-type */ $object->getOtherIdentifiers()) === strtolower($this->getOtherIdentifiers());
Loading history...
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