Completed
Push — master ( eb575e...c103e9 )
by Romain
13s
created

Address::fromPayload()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 0
cp 0
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 1
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Common;
6
7
class Address implements \JsonSerializable
8
{
9
    /**
10
     * @var null|string
11
     */
12
    protected $name;
13
14
    /**
15
     * @var string
16
     */
17
    protected $street;
18
19
    /**
20
     * @var null|string
21
     */
22
    protected $additionalStreet;
23
24
    /**
25
     * @var string
26
     */
27
    protected $city;
28
29
    /**
30
     * @var string
31
     */
32
    protected $postalCode;
33
34
    /**
35
     * @var string
36
     */
37
    protected $state;
38
39
    /**
40
     * @var string
41
     */
42
    protected $country;
43
44
    /**
45
     * @var null|int
46
     */
47
    protected $id;
48
49
    /**
50
     * Address constructor.
51
     *
52
     * @param string $street
53
     * @param string $city
54
     * @param string $postalCode
55
     * @param string $state
56 16
     * @param string $country
57
     */
58
    public function __construct(
59
        string $street,
60
        string $city,
61
        string $postalCode,
62
        string $state,
63 16
        string $country
64 16
    ) {
65 16
        $this->street = $street;
66 16
        $this->city = $city;
67 16
        $this->postalCode = $postalCode;
68 16
        $this->state = $state;
69
        $this->country = $country;
70
    }
71
72
    /**
73
     * @param string $street
74
     * @param string $city
75 4
     * @param string $postalCode
76
     * @param string $state
77 4
     * @param string $country
78
     *
79 4
     * @return \Kerox\Messenger\Model\Common\Address
80
     */
81
    public static function create(
82
        string $street,
83
        string $city,
84
        string $postalCode,
85 1
        string $state,
86
        string $country
87 1
    ): self {
88
        return new self($street, $city, $postalCode, $state, $country);
89
    }
90
91
    /**
92
     * @param string $name
93 3
     *
94
     * @return \Kerox\Messenger\Model\Common\Address
95 3
     */
96
    public function setName(string $name): self
97
    {
98
        $this->name = $name;
99
100
        return $this;
101
    }
102
103 13
    /**
104
     * @return null|string
105 13
     */
106
    public function getName(): ?string
107 13
    {
108
        return $this->name;
109
    }
110
111
    /**
112
     * @return string
113 3
     */
114
    public function getStreet(): string
115 3
    {
116
        return $this->street;
117
    }
118
119
    /**
120
     * @param string $additionalStreet
121 3
     *
122
     * @return Address
123 3
     */
124
    public function setAdditionalStreet(string $additionalStreet): self
125
    {
126
        $this->additionalStreet = $additionalStreet;
127
128
        return $this;
129 3
    }
130
131 3
    /**
132
     * @return null|string
133
     */
134
    public function getAdditionalStreet(): ?string
135
    {
136
        return $this->additionalStreet;
137 3
    }
138
139 3
    /**
140
     * @return string
141
     */
142
    public function getCity(): string
143
    {
144
        return $this->city;
145 3
    }
146
147 3
    /**
148
     * @return string
149
     */
150
    public function getPostalCode(): string
151
    {
152
        return $this->postalCode;
153
    }
154
155 3
    /**
156
     * @return string
157 3
     */
158
    public function getState(): string
159 3
    {
160
        return $this->state;
161
    }
162
163
    /**
164
     * @return string
165 1
     */
166
    public function getCountry(): string
167 1
    {
168
        return $this->country;
169
    }
170
171
    /**
172
     * @param int $id
173 4
     *
174
     * @return Address
175
     */
176 4
    public function setId(int $id): self
177 4
    {
178 4
        $this->id = $id;
179 4
180 4
        return $this;
181 4
    }
182 4
183 4
    /**
184
     * @return null|int
185
     */
186 4
    public function getId(): ?int
187
    {
188
        return $this->id;
189
    }
190
191
    /**
192
     * @return array
193
     */
194 12
    public function toArray(): array
195
    {
196 12
        $array = [
197
            'name'        => $this->name,
198 12
            'street_1'    => $this->street,
199 12
            'street_2'    => $this->additionalStreet,
200
            'city'        => $this->city,
201
            'postal_code' => $this->postalCode,
202 12
            'state'       => $this->state,
203 3
            'country'     => $this->country,
204
            'id'          => $this->id,
205
        ];
206 12
207 4
        return array_filter($array);
208
    }
209
210 12
    /**
211
     * @return array
212
     */
213
    public function jsonSerialize(): array
214
    {
215
        return $this->toArray();
216
    }
217
218
    /**
219
     * @param array $payload
220
     *
221
     * @return \Kerox\Messenger\Model\Common\Address
222
     */
223
    public static function fromPayload(array $payload): self
224
    {
225
        $address = self::create(
226
            $payload['street_1'],
227
            $payload['city'],
228
            $payload['postal_code'],
229
            $payload['state'],
230
            $payload['country']
231
        );
232
233
        if (isset($payload['street_2'])) {
234
            $address->setAdditionalStreet($payload['street_2']);
235
        }
236
237
        if (isset($payload['id'])) {
238
            $address->setId($payload['id']);
239
        }
240
241
        if (isset($payload['name'])) {
242
            $address->setName($payload['name']);
243
        }
244
245
        return $address;
246
    }
247
}
248