City   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 50
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A sanitizeCity() 0 4 2
A __construct() 0 9 2
A value() 0 3 1
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 City extends ValueObject
11
{
12
    /**
13
     * @var string|null
14
     */
15
    protected ?string $city = null;
16
17
    /**
18
     * @var Country|null
19
     */
20
    protected ?Country $country = null;
21
22
    /**
23
     * @param array|null $data
24
     */
25 27
    final public function __construct(?array $data)
26
    {
27 27
        $this->city = $data['city'] ?? null;
28
29 27
        if (isset($data['country_code'])) {
30 8
            $this->country = new Country($data);
31
        }
32
33 27
        $this->sanitizeCity();
34
    }
35
36
    /**
37
     * @return string
38
     */
39 22
    public function value(): string
40
    {
41 22
        return $this->city ?? '';
42
    }
43
44
    /**
45
     * @return void
46
     */
47 27
    protected function sanitizeCity(): void
48
    {
49 27
        if (! is_null($this->city)) {
50 18
            $this->city = Str::squish($this->city);
51
        }
52
    }
53
54
    /**
55
     * @return string
56
     */
57 22
    public function __toString(): string
58
    {
59 22
        return $this->value();
60
    }
61
}
62