Passed
Push — master ( 410ab4...187e01 )
by Mr
08:19
created

Address::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/value-object project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\Interop\MakeEmptyInterface;
13
14
final class Address implements ValueObjectInterface, MakeEmptyInterface
15
{
16
    private ?string $name;
17
18
    private ?string $address1;
19
20
    private ?string $address2;
21
22
    private ?string $city;
23
24
    private ?string $postcode;
25
26
    private ?string $country;
27
28
    /** @param self $comparator */
29
    public function equals($comparator): bool
30
    {
31
        Assertion::isInstanceOf($comparator, self::class);
32
        return $this->toNative() === $comparator->toNative();
33
    }
34
35
    /** @param array $value */
36
    public static function fromNative($value): self
37
    {
38
        Assertion::isArray($value, 'Trying to create address from unsupported value type.');
39
        return new self(
40
            $value['name'] ?? null,
41
            $value['address1'] ?? null,
42
            $value['address2'] ?? null,
43
            $value['city'] ?? null,
44
            $value['postcode'] ?? null,
45
            $value['country'] ?? null
46
        );
47
    }
48
49
    public function toNative(): array
50
    {
51
        return array_filter([
52
            'name' => $this->name,
53
            'address1' => $this->address1,
54
            'address2' => $this->address2,
55
            'city' => $this->city,
56
            'postcode' => $this->postcode,
57
            'country' => $this->country
58
        ]);
59
    }
60
61
    public static function makeEmpty(): self
62
    {
63
        return new self;
64
    }
65
66
    public function isEmpty(): bool
67
    {
68
        return empty($this->toNative());
69
    }
70
71
    public function __toString(): string
72
    {
73
        return implode(PHP_EOL, $this->toNative());
74
    }
75
76
    private function __construct(
77
        string $name = null,
78
        string $address1 = null,
79
        string $address2 = null,
80
        string $city = null,
81
        string $postcode = null,
82
        string $country = null
83
    ) {
84
        $this->name = $name;
85
        $this->address1 = $address1;
86
        $this->address2 = $address2;
87
        $this->city = $city;
88
        $this->postcode = $postcode;
89
        $this->country = $country;
90
    }
91
}
92