|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP Billing Library |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
|
6
|
|
|
* @package php-billing |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\type; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* General Type. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Type implements TypeInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public const string MONTHLY = 'monthly'; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int|string|null|float The unique identifier of the type. Can be an integer or string. |
|
24
|
|
|
* Special values: |
|
25
|
|
|
* - `Type::ANY` indicates that the type can match any ID. |
|
26
|
|
|
* - `Type::NONE` indicates that there is no valid ID. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $id; |
|
29
|
|
|
|
|
30
|
47 |
|
/** |
|
31
|
|
|
* @var string|null|float The name of the type. Can be a specific name or one of the special values: |
|
32
|
47 |
|
* - `Type::ANY` indicates that the type can match any name. |
|
33
|
47 |
|
* - `Type::NONE` indicates that there is no valid name. |
|
34
|
47 |
|
*/ |
|
35
|
|
|
protected $name; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($id, $name = self::ANY) |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->id = $id; |
|
40
|
|
|
$this->name = $name; |
|
41
|
7 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getId() |
|
47
|
29 |
|
{ |
|
48
|
|
|
return $this->id; |
|
49
|
29 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
2 |
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function getName(): ?string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->name; |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function hasName(): bool |
|
60
|
|
|
{ |
|
61
|
|
|
return !empty($this->name) && $this->name !== self::ANY && $this->name !== self::NONE; |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function getUniqueId() |
|
65
|
2 |
|
{ |
|
66
|
|
|
return $this->hasName() ? $this->name : $this->id; |
|
67
|
|
|
} |
|
68
|
22 |
|
|
|
69
|
|
|
public function equals(TypeInterface $other): bool |
|
70
|
22 |
|
{ |
|
71
|
18 |
|
return $this->id === $other->getId() && |
|
72
|
22 |
|
$this->name === $other->getName(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
22 |
|
public function matches(TypeInterface $other): bool |
|
76
|
|
|
{ |
|
77
|
22 |
|
return $this->id === self::ANY || $other->getId() === self::ANY |
|
78
|
1 |
|
? $this->checkMatches($this->name, $other->getName()) |
|
79
|
|
|
: $this->checkMatches($this->id, $other->getId()); |
|
80
|
|
|
} |
|
81
|
22 |
|
|
|
82
|
|
|
protected function checkMatches($lhs, $rhs) |
|
83
|
|
|
{ |
|
84
|
1 |
|
if ($lhs === self::NONE || $rhs === self::NONE) { |
|
85
|
|
|
return false; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
return (string) $lhs === (string) $rhs; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function jsonSerialize(): array |
|
92
|
|
|
{ |
|
93
|
|
|
return array_filter(get_object_vars($this)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function isDefined(): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->id !== null || $this->name !== null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function isMonthly(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->belongsToGroup(self::MONTHLY); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function belongsToGroup(string $group): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->groupName() === $group; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function groupName(): string |
|
112
|
|
|
{ |
|
113
|
|
|
$groupIndex = 0; |
|
114
|
|
|
|
|
115
|
|
|
return $this->extractNamePart($groupIndex); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param int $index - 0 - group index, 1 - local index |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private function extractNamePart(int $index): string |
|
123
|
|
|
{ |
|
124
|
|
|
$name = $this->getName(); |
|
125
|
|
|
if (strpos($name, ',') > 0) { |
|
126
|
|
|
$parts = explode(',', $name, 2); |
|
127
|
|
|
} else { |
|
128
|
|
|
return $name; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $parts[$index] ?? ''; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function belongsToLocalCategory(string $local): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->localName() === $local; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function localName(): string |
|
140
|
|
|
{ |
|
141
|
|
|
$localIndex = 1; |
|
142
|
|
|
|
|
143
|
|
|
return $this->extractNamePart($localIndex); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|