PostCode   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 63
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A value() 0 3 1
A sanitize() 0 4 2
A __construct() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Temidaio\ValueObjects;
6
7
use Illuminate\Support\Str;
8
use MichaelRubel\ValueObjects\ValueObject;
9
10
class PostCode extends ValueObject
11
{
12
    /**
13
     * @var string|null
14
     */
15
    protected ?string $postcode = null;
16
17
    /**
18
     * @var City|null
19
     */
20
    protected ?City $city = null;
21
22
    /**
23
     * @var Country|null
24
     */
25
    protected ?Country $country = null;
26
27
    /**
28
     * @param array|null $data
29
     */
30 22
    final public function __construct(?array $data)
31
    {
32 22
        $this->postcode = $data['postcode'] ?? null;
33
34 22
        if (isset($data['city'])) {
35 12
            $this->city = new City($data);
36
        }
37
38 22
        if (isset($data['country_code'])) {
39 4
            $this->country = new Country($data);
40
        }
41
42 22
        if (! empty($this->postcode)) {
43 15
            $this->sanitize();
44
        }
45
    }
46
47
    /**
48
     * @return string
49
     */
50 21
    public function value(): string
51
    {
52 21
        return $this->postcode ?? '';
53
    }
54
55
    /**
56
     * @return void
57
     */
58 15
    protected function sanitize(): void
59
    {
60 15
        if (! is_null($this->postcode)) {
61 15
            $this->postcode = Str::squish($this->postcode);
62
        }
63
    }
64
65
    /**
66
     * Cast the object to string.
67
     *
68
     * @return string
69
     */
70 21
    public function __toString(): string
71
    {
72 21
        return $this->value();
73
    }
74
}
75