Passed
Push — master ( 0a1931...7eb449 )
by Jhao
02:10
created

Recipient::create()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of RussianPost SDK package.
5
 *
6
 * © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites;
15
16
use Appwilio\RussianPostSDK\Dispatching\DataAware;
17
use Appwilio\RussianPostSDK\Dispatching\Instantiator;
18
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable;
19
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Entities\NormalizedFio;
20
21
class Recipient implements Arrayable
22
{
23
    use DataAware;
24
25
    public static function fromNormalizedFio(NormalizedFio $normalizedFio)
26
    {
27
        return Instantiator::instantiate(self::class, [
28
            'surname'     => $normalizedFio->getLastName(),
29
            'given-name'  => $normalizedFio->getFirstName(),
30
            'middle-name' => $normalizedFio->getMiddleName(),
31
        ]);
32
    }
33
34
    public static function create(?string $fullName = null): self
35
    {
36
        return new self($fullName);
37
    }
38
39
    public function __construct(?string $fullName = null)
40
    {
41
        if ($fullName) {
42
            $this->data['recipient-name'] = $fullName;
43
        }
44
    }
45
46
    public function firstName(string $firstName)
47
    {
48
        $this->data['given-name'] = $firstName;
49
    }
50
51
    public function middleName(string $middleName)
52
    {
53
        $this->data['middle-name'] = $middleName;
54
    }
55
56
    public function lastName(string $lastName)
57
    {
58
        $this->data['surname'] = $lastName;
59
    }
60
61
    public function phoneNumber(string $phoneNumber)
62
    {
63
        $this->data['tel-address'] = $phoneNumber;
64
    }
65
66
    public function toArray(): array
67
    {
68
        if (empty($this->data['recipient-name'])) {
69
            $fullName = \trim(\implode(' ', [
70
                $this->data['surname'],
71
                $this->data['given-name'],
72
                $this->data['middle-name'],
73
            ]));
74
75
            if ($fullName) {
76
                $this->data['recipient-name'] = $fullName;
77
            }
78
        }
79
80
        return $this->data;
81
    }
82
}
83