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\target; |
||
12 | |||
13 | use hiqdev\php\billing\Exception\CannotReassignException; |
||
14 | |||
15 | /** |
||
16 | * @see TargetInterface |
||
17 | * |
||
18 | * @author Andrii Vasyliev <[email protected]> |
||
19 | */ |
||
20 | abstract class AbstractTarget implements TargetInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var int|string |
||
24 | */ |
||
25 | protected $id; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $type; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | 46 | protected $state; |
|
36 | |||
37 | 46 | /** |
|
38 | 46 | * @var string |
|
39 | 46 | */ |
|
40 | 46 | protected $name; |
|
41 | |||
42 | /** @var string */ |
||
43 | protected $label; |
||
44 | |||
45 | 28 | public function __construct($id, $type, $name = null) |
|
46 | { |
||
47 | 28 | $this->id = $id; |
|
48 | $this->type = $type; |
||
49 | $this->name = $name; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return int|string |
||
54 | */ |
||
55 | public function getId() |
||
56 | { |
||
57 | return $this->id; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | 24 | * @return bool |
|
62 | */ |
||
63 | 24 | public function hasId() |
|
64 | { |
||
65 | return $this->id !== null; |
||
66 | } |
||
67 | |||
68 | public function setId($id) |
||
69 | 3 | { |
|
70 | if ((string) $this->id === (string) $id) { |
||
71 | 3 | return; |
|
72 | } |
||
73 | if ($this->hasId()) { |
||
74 | 1 | throw new CannotReassignException('sale id'); |
|
75 | } |
||
76 | 1 | $this->id = $id; |
|
77 | 1 | } |
|
78 | |||
79 | 1 | /** |
|
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function getType() |
||
83 | { |
||
84 | return $this->type; |
||
85 | 4 | } |
|
86 | |||
87 | 4 | /** |
|
88 | 4 | * {@inheritdoc} |
|
89 | */ |
||
90 | 4 | public function getState(): ?string |
|
91 | { |
||
92 | return $this->state; |
||
93 | 1 | } |
|
94 | |||
95 | 1 | public function setState(string $state): void |
|
96 | { |
||
97 | $this->state = $state; |
||
98 | 21 | } |
|
99 | |||
100 | 21 | /** |
|
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 21 | public function getName() |
|
104 | { |
||
105 | 21 | return $this->name; |
|
106 | } |
||
107 | |||
108 | public function getLabel(): ?string |
||
109 | { |
||
110 | return $this->label; |
||
111 | } |
||
112 | |||
113 | 21 | public function getFullName(): string |
|
114 | { |
||
115 | $type = $this->getType(); |
||
116 | $name = $this->getName(); |
||
117 | 21 | ||
118 | return $type === self::ANY && $name === null ? '' : "$type:$name"; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
119 | } |
||
120 | 21 | ||
121 | /** |
||
122 | 21 | * @return string |
|
123 | */ |
||
124 | public function getUniqueId() |
||
125 | 21 | { |
|
126 | $id = $this->getId(); |
||
127 | 21 | $type = $this->getType(); |
|
128 | |||
129 | return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id); |
||
0 ignored issues
–
show
|
|||
130 | 21 | } |
|
131 | |||
132 | 21 | public function equals(TargetInterface $other): bool |
|
133 | { |
||
134 | return $this->getUniqueId() === $other->getUniqueId(); |
||
135 | } |
||
136 | 21 | ||
137 | public function matches(TargetInterface $other): bool |
||
138 | { |
||
139 | 1 | return $this->checkMatches($other) || $other->checkMatches($this); |
|
140 | } |
||
141 | 1 | ||
142 | public function checkMatches(TargetInterface $other): bool |
||
143 | { |
||
144 | if ($this->id === self::ANY) { |
||
145 | if ($this->type === self::ANY) { |
||
146 | return true; |
||
147 | } |
||
148 | |||
149 | return $this->matchTypes($other); |
||
150 | } |
||
151 | |||
152 | if ($this->type === self::ANY) { |
||
153 | return $this->matchIds($other); |
||
154 | } |
||
155 | |||
156 | return $this->matchIds($other) && $this->matchTypes($other); |
||
157 | } |
||
158 | |||
159 | protected function matchIds(TargetInterface $other) |
||
160 | { |
||
161 | return $this->matchStrings($this->id, $other->getId()); |
||
162 | } |
||
163 | |||
164 | protected function matchTypes(TargetInterface $other) |
||
165 | { |
||
166 | return $this->matchStrings($this->type, $other->getType()); |
||
167 | } |
||
168 | |||
169 | protected function matchStrings($lhs, $rhs) |
||
170 | { |
||
171 | if ($lhs === self::NONE || $rhs === self::NONE) { |
||
172 | return false; |
||
173 | } |
||
174 | |||
175 | return (string) $lhs === (string) $rhs; |
||
176 | } |
||
177 | |||
178 | public function jsonSerialize(): array |
||
179 | { |
||
180 | return array_filter(array_merge( |
||
181 | get_object_vars($this), |
||
182 | [ |
||
183 | 'id' => $this->getId(), |
||
184 | 'type' => $this->getType(), |
||
185 | 'state' => $this->getState(), |
||
186 | 'name' => $this->getName(), |
||
187 | ], |
||
188 | )); |
||
189 | } |
||
190 | |||
191 | protected static $anyTarget; |
||
192 | |||
193 | public static function any(): self |
||
194 | { |
||
195 | if (static::$anyTarget === null) { |
||
196 | static::$anyTarget = new static(static::ANY, static::ANY); |
||
197 | } |
||
198 | |||
199 | return static::$anyTarget; |
||
200 | } |
||
201 | } |
||
202 |