Passed
Pull Request — master (#24)
by
unknown
03:08
created

PostalCode   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 8 2
A __toString() 0 3 1
A __construct() 0 3 1
A equals() 0 7 2
A getFormatted() 0 3 1
A getValue() 0 3 1
A jsonSerialize() 0 4 1
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