PostCode::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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