Completed
Push — master ( d2b3f3...05ef8e )
by João Felipe Magro
02:25
created

Customer::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
namespace Ipag\Classes;
4
5
use Ipag\Classes\Contracts\Emptiable;
6
use Ipag\Classes\Contracts\Serializable;
7
use Ipag\Classes\Traits\EmptiableTrait;
8
9
final class Customer extends BaseResource implements Emptiable, Serializable
10
{
11
    use EmptiableTrait;
12
13
    const INDIVIDUAL = 'f';
14
    const BUSINESS = 'j';
15
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var string
23
     */
24
    private $type;
25
26
    /**
27
     * @var string
28
     */
29
    private $taxpayerId;
30
31
    /**
32
     * @var string
33
     */
34
    private $email;
35
36
    /**
37
     * @var Phone
38
     */
39
    private $phone;
40
41
    /**
42
     * @var Address
43
     */
44
    private $address;
45
46
    /**
47
     * @return Address
48
     */
49
    public function getAddress()
50
    {
51
        if (is_null($this->address)) {
52
            $this->address = new Address();
53
        }
54
55
        return $this->address;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getName()
62
    {
63
        return $this->name;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getType()
70
    {
71
        return $this->type;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getTaxpayerId()
78
    {
79
        return $this->taxpayerId;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getEmail()
86
    {
87
        return $this->email;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getPhone()
94
    {
95
        if (is_null($this->phone)) {
96
            $this->phone = new Phone();
97
        }
98
99
        return $this->phone->getAreaCode().$this->phone->getNumber();
100
    }
101
102
    /**
103
     * @param string $name
104
     */
105
    public function setName($name)
106
    {
107
        $this->name = substr((string) $name, 0, 50);
108
109
        return $this;
110
    }
111
112
    private function setType()
113
    {
114
        if ($this->isIndividual()) {
115
            $this->type = self::INDIVIDUAL;
116
117
            return $this;
118
        }
119
120
        $this->type = self::BUSINESS;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param string $taxpayerId
127
     */
128
    public function setTaxpayerId($taxpayerId)
129
    {
130
        $this->taxpayerId = substr($this->getNumberUtil()->getOnlyNumbers($taxpayerId), 0, 14);
131
132
        $this->setType();
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param string $email
139
     */
140
    public function setEmail($email)
141
    {
142
        $this->email = substr((string) $email, 0, 50);
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param string $areaCode
149
     * @param string $number
150
     */
151
    public function setPhone($areaCode, $number)
152
    {
153
        $this->phone = new Phone();
154
        $this->phone
155
            ->setAreaCode($areaCode)
156
            ->setNumber($number);
157
158
        return $this;
159
    }
160
161
    /**
162
     * @param Address $address
163
     */
164
    public function setAddress(Address $address)
165
    {
166
        $this->address = $address;
167
168
        return $this;
169
    }
170
171
    private function isIndividual()
172
    {
173
        return (bool) (strlen($this->taxpayerId) <= 11);
174
    }
175
176
    public function serialize()
177
    {
178
        if ($this->isEmpty()) {
179
            return [];
180
        }
181
182
        $_customer = [
183
            'nome'  => urlencode($this->getName()),
184
            'email' => urlencode($this->getEmail()),
185
            'doc'   => urlencode($this->getTaxpayerId()),
186
            'fone'  => urlencode($this->getPhone()),
187
        ];
188
189
        $_address = $this->getAddress()->serialize();
190
191
        return array_merge($_customer, $_address);
192
    }
193
}
194