Country::value()   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
use Temidaio\ValueObjects\Formatters\CountryFormatter;
10
11
class Country extends ValueObject
12
{
13
    /**
14
     * Internal properties.
15
     *
16
     * @var string|null
17
     */
18
    protected ?string $iso_code = null;
19
    protected ?string $country = null;
20
21
    /**
22
     * @param array|null $data
23
     */
24 33
    final public function __construct(?array $data)
25
    {
26 33
        $this->iso_code = $data['country_code'] ?? null;
27 33
        $this->country  = $data['country'] ?? null;
28
29 33
        if (! empty($this->country)) {
30 13
            $this->sanitizeIsoCode();
31 13
            $this->sanitizeCountry();
32
        }
33
    }
34
35
    /**
36
     * @return string
37
     */
38 20
    public function value(): string
39
    {
40 20
        return $this->format();
41
    }
42
43
    /**
44
     * Sanitize the ISO Code input.
45
     *
46
     * @return void
47
     */
48 13
    protected function sanitizeIsoCode(): void
49
    {
50 13
        if (! is_null($this->iso_code)) {
51 2
            $this->iso_code = Str::squish($this->iso_code);
52
        }
53
    }
54
55
    /**
56
     * Sanitize the Country input.
57
     *
58
     * @return void
59
     */
60 13
    protected function sanitizeCountry(): void
61
    {
62 13
        if (! is_null($this->country)) {
63 13
            $this->country = Str::squish($this->country);
64
        }
65
    }
66
67
    /**
68
     * Format the input country data.
69
     *
70
     * @return string
71
     */
72 20
    protected function format(): string
73
    {
74 20
        return format(CountryFormatter::class, [
75 20
            'country_code' => $this->iso_code,
76 20
            'country'      => $this->country,
77 20
        ]);
78
    }
79
80
    /**
81
     * Cast the object to string.
82
     *
83
     * @return string
84
     */
85 20
    public function __toString(): string
86
    {
87 20
        return $this->value();
88
    }
89
}
90