Passed
Pull Request — master (#16)
by Manuel
02:10
created

CombinedAddress::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sprain\SwissQrBill\DataGroup\Element;
4
5
use Sprain\SwissQrBill\DataGroup\AddressInterface;
6
use Sprain\SwissQrBill\DataGroup\QrCodeableInterface;
7
use Sprain\SwissQrBill\Validator\SelfValidatableInterface;
8
use Sprain\SwissQrBill\Validator\SelfValidatableTrait;
9
use Symfony\Component\Validator\Constraints as Assert;
10
use Symfony\Component\Validator\Mapping\ClassMetadata;
11
12
class CombinedAddress implements AddressInterface, SelfValidatableInterface, QrCodeableInterface
13
{
14
    use SelfValidatableTrait;
15
16
    const ADDRESS_TYPE = 'K';
17
18
    /**
19
     * Name or company
20
     *
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * Address line 1
27
     *
28
     * Street and building number or P.O. Box
29
     *
30
     * @var string
31
     */
32
    private $addressLine1;
33
34
    /**
35
     * Address line 2
36
     *
37
     * Postal code and town
38
     *
39
     * @var string
40
     */
41
    private $addressLine2;
42
43
    /**
44
     * Country (ISO 3166-1 alpha-2)
45
     *
46
     * @var string
47
     */
48
    private $country;
49
50
    public static function create(
51
        string $name,
52
        ?string $addressLine1,
53
        string $addressLine2,
54
        string $country
55
    ): self
56
    {
57
        $combinedAddress = new self();
58
        $combinedAddress->name = $name;
59
        $combinedAddress->addressLine1 = $addressLine1;
60
        $combinedAddress->addressLine2 = $addressLine2;
61
        $combinedAddress->country = strtoupper($country);
62
63
        return $combinedAddress;
64
    }
65
66
    public function getName(): ?string
67
    {
68
        return $this->name;
69
    }
70
71
    public function getAddressLine1(): ?string
72
    {
73
        return $this->addressLine1;
74
    }
75
76
    public function getAddressLine2(): ?string
77
    {
78
        return $this->addressLine2;
79
    }
80
81
    public function getCountry(): ?string
82
    {
83
        return $this->country;
84
    }
85
86
    public function getFullAddress(): string
87
    {
88
        $address = $this->getName();
89
90
        if ($this->getAddressLine1()) {
91
            $address .= "\n" . $this->getAddressLine1();
92
        }
93
94
        $address .= sprintf("\n%s-%s", $this->getCountry(), $this->getAddressLine2());
95
96
        return $address;
97
    }
98
99
    public function getQrCodeData(): array
100
    {
101
        return [
102
            $this->getAddressLine2() ? self::ADDRESS_TYPE : '',
103
            $this->getName(),
104
            $this->getAddressLine1(),
105
            $this->getAddressLine2(),
106
            '',
107
            '',
108
            $this->getCountry()
109
        ];
110
    }
111
112
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
113
    {
114
        $metadata->addPropertyConstraints('name', [
115
            new Assert\NotBlank(),
116
            new Assert\Length([
117
                'max' => 70
118
            ])
119
        ]);
120
121
        $metadata->addPropertyConstraints('addressLine1', [
122
            new Assert\Length([
123
                'max' => 70
124
            ])
125
        ]);
126
127
        $metadata->addPropertyConstraints('addressLine2', [
128
            new Assert\NotBlank(),
129
            new Assert\Length([
130
                'max' => 70
131
            ])
132
        ]);
133
134
        $metadata->addPropertyConstraints('country', [
135
            new Assert\NotBlank(),
136
            new Assert\Country()
137
        ]);
138
    }
139
}