Passed
Push — master ( 04a136...43120b )
by
unknown
56s queued 10s
created

City::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Talentify\ValueObject\Geography\Address;
6
7
use Talentify\ValueObject\StringUtils;
8
use Talentify\ValueObject\ValueObject;
9
10
class City implements AddressElement
11
{
12
    /** @var string */
13
    private $name;
14
15
    /**
16
     * @throws \InvalidArgumentException if supplied value is invalid.
17
     */
18
    public function __construct(string $name)
19
    {
20
        $this->setName($name);
21
    }
22
23
    private function setName(string $name) : void
24
    {
25
        $normalized = StringUtils::trimSpacesWisely($name);
26
        if (empty($normalized)) {
27
            throw new \InvalidArgumentException(sprintf('The value "%s" is not a valid city name.', $name));
28
        }
29
30
        $this->name = StringUtils::convertCaseToTitle($normalized);
31
    }
32
33
    public function getName() : string
34
    {
35
        return $this->name;
36
    }
37
38
    public function equals(?ValueObject $object) : bool
39
    {
40
        if (!$object instanceof self) {
41
            return false;
42
        }
43
44
        return $object->getName() === $this->getName();
45
    }
46
47
    public function getFormatted() : string
48
    {
49
        return sprintf('%s', $this->getName());
50
    }
51
52
    public function __toString() : string
53
    {
54
        return $this->name;
55
    }
56
57
    public function jsonSerialize()
58
    {
59
        return [
60
            'name' => $this->name,
61
        ];
62
    }
63
}
64