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-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\target; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @see TargetInterface |
15
|
|
|
* |
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractTarget implements TargetInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int|string |
22
|
|
|
*/ |
23
|
|
|
protected $id; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $type; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $name; |
34
|
|
|
|
35
|
37 |
|
public function __construct($id, $type, $name = null) |
36
|
|
|
{ |
37
|
37 |
|
$this->id = $id; |
38
|
37 |
|
$this->type = $type; |
39
|
37 |
|
$this->name = $name; |
40
|
37 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int|string |
44
|
|
|
*/ |
45
|
24 |
|
public function getId() |
46
|
|
|
{ |
47
|
24 |
|
return $this->id; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function hasId() |
54
|
|
|
{ |
55
|
|
|
return $this->id !== null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
24 |
|
public function getType() |
62
|
|
|
{ |
63
|
24 |
|
return $this->type; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getName() |
70
|
|
|
{ |
71
|
|
|
return $this->name; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getFullName(): string |
75
|
|
|
{ |
76
|
|
|
$type = $this->getType(); |
77
|
6 |
|
$name = $this->getName(); |
78
|
|
|
|
79
|
6 |
|
return $type === self::ANY && $name === null ? '' : "$type:$name"; |
|
|
|
|
80
|
6 |
|
} |
81
|
|
|
|
82
|
6 |
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getUniqueId() |
86
|
|
|
{ |
87
|
|
|
$id = $this->getId(); |
88
|
2 |
|
$type = $this->getType(); |
89
|
|
|
|
90
|
2 |
|
return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
22 |
|
public function equals(TargetInterface $other): bool |
97
|
|
|
{ |
98
|
22 |
|
return $this->getUniqueId() === $other->getUniqueId(); |
99
|
|
|
} |
100
|
|
|
|
101
|
22 |
|
/** |
102
|
|
|
* @return bool |
103
|
22 |
|
*/ |
104
|
1 |
|
public function matches(TargetInterface $other): bool |
105
|
1 |
|
{ |
106
|
|
|
return $this->checkMatches($other) || $other->checkMatches($this); |
107
|
|
|
} |
108
|
1 |
|
|
109
|
|
|
public function checkMatches(TargetInterface $other): bool |
110
|
|
|
{ |
111
|
22 |
|
if ($this->id === self::ANY) { |
112
|
|
|
if ($this->type === self::ANY) { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
22 |
|
|
116
|
|
|
return $this->matchTypes($other); |
117
|
|
|
} |
118
|
22 |
|
|
119
|
|
|
if ($this->type === self::ANY) { |
120
|
22 |
|
return $this->matchIds($other); |
121
|
|
|
} |
122
|
|
|
|
123
|
22 |
|
return $this->matchIds($other) && $this->matchTypes($other); |
124
|
|
|
} |
125
|
22 |
|
|
126
|
|
|
protected function matchIds(TargetInterface $other) |
127
|
|
|
{ |
128
|
22 |
|
return $this->matchStrings($this->id, $other->getId()); |
129
|
|
|
} |
130
|
22 |
|
|
131
|
1 |
|
protected function matchTypes(TargetInterface $other) |
132
|
|
|
{ |
133
|
|
|
return $this->matchStrings($this->type, $other->getType()); |
134
|
22 |
|
} |
135
|
|
|
|
136
|
|
|
protected function matchStrings($lhs, $rhs) |
137
|
|
|
{ |
138
|
|
|
if ($lhs === self::NONE || $rhs === self::NONE) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return (string) $lhs === (string) $rhs; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function jsonSerialize() |
146
|
|
|
{ |
147
|
|
|
return array_filter(get_object_vars($this)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|