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

PostalCode::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
/**
11
 * @see https://en.wikipedia.org/wiki/Address#Postal_codes
12
 * @see https://en.wikipedia.org/wiki/Postal_code
13
 */
14
class PostalCode implements AddressElement
15
{
16
    /** @var string */
17
    private $value;
18
19
    /**
20
     * @throws \InvalidArgumentException
21
     */
22
    public function __construct(string $value)
23
    {
24
        $this->setValue($value);
25
    }
26
27
    private function setValue(string $value) : void
28
    {
29
        $normalized = StringUtils::trimSpacesWisely($value);
30
        if (empty($normalized)) {
31
            throw new \InvalidArgumentException(sprintf('The value "%s" is not a valid postal code.', $value));
32
        }
33
34
        $this->value = $normalized;
35
    }
36
37
    public function getValue() : string
38
    {
39
        return $this->value;
40
    }
41
42
    public function equals(?ValueObject $object) : bool
43
    {
44
        if (!$object instanceof self) {
45
            return false;
46
        }
47
48
        return $object->getValue() === $this->getValue();
49
    }
50
51
    public function getFormatted() : string
52
    {
53
        return sprintf('%s', $this->getValue());
54
    }
55
56
    public function __toString() : string
57
    {
58
        return $this->value;
59
    }
60
61
    public function jsonSerialize()
62
    {
63
        return [
64
            'value' => $this->value,
65
        ];
66
    }
67
}
68