Customer::toJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

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
rs 10
1
<?php
2
3
namespace Stadem\VivaPayments\Request;
4
5
use Stadem\VivaPayments\Traits\unsetData;
6
use Stadem\VivaPayments\Enums\RequestLang;
7
use InvalidArgumentException;
8
9
/**
10
 * Information about the customer.
11
 */
12
class Customer
13
{
14
15
    use unsetData;
16
17
    private const EX_CODE = 500;
18
    private ?string $email;
19
    private ?string $fullName;
20
    private ?string $phone;
21
    private ?string $countryCode;
22
    private string|RequestLang $requestLang;
23
24
25
    public function __construct(
26
        ?string $email = null,
27
        ?string $fullName = null,
28
        ?string $phone = null,
29
        ?string $countryCode = null,
30
        string|RequestLang $requestLang = null,
31
    ) {
32
        $this->setEmail($email)
33
            ->setFullName($fullName)
34
            ->setPhone($phone)
35
            ->setCountryCode($countryCode)
36
            ->setRequestLang($requestLang);
37
    }
38
39
    public function toArray(): array
40
    {
41
        //removing null values
42
        return $this->unsetData([
43
            'email' => $this->email,
44
            'fullName' => $this->fullName,
45
            'phone' => $this->phone,
46
            'countryCode' => $this->countryCode,
47
            'requestLang' => $this->requestLang,
48
        ]);
49
    }
50
51
    public function toJson(): string
52
    {
53
        return json_encode($this->toArray());
54
    }
55
56
    public function setEmail(?string $email): static
57
    {
58
        if ($email && strlen($email) > 50) {
59
            throw new InvalidArgumentException('Customer\'s email value must be less than 50 characters.', self::EX_CODE);
60
        }
61
62
        $this->email = $email;
63
64
        return $this;
65
    }
66
67
    public function setFullName(?string $fullName): static
68
    {
69
        if ($fullName && strlen($fullName) > 50) {
70
            throw new InvalidArgumentException('Customer\'s email value must be less than 50 characters.', self::EX_CODE);
71
        }
72
73
        $this->fullName = $fullName;
74
75
        return $this;
76
    }
77
78
    public function setPhone(?string $phone): static
79
    {
80
        if ($phone && strlen($phone) > 30) {
81
            throw new InvalidArgumentException('Customer\'s phone value must be less than 30 characters.', self::EX_CODE);
82
        }
83
84
        $this->phone = $phone;
85
86
        return $this;
87
    }
88
89
    public function setCountryCode(?string $countryCode): static
90
    {
91
        if ($countryCode && strlen($countryCode) !== 2) {
92
            throw new InvalidArgumentException('Customer\'s country code value is invalid.', self::EX_CODE);
93
        }
94
95
        $this->countryCode = $countryCode;
96
97
        return $this;
98
    }
99
100
    public function setRequestLang(mixed $requestLang): static
101
    {
102
        
103
        $this->requestLang = $requestLang;
104
105
        return $this;
106
    }
107
108
}