|
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
|
10 |
|
public function __construct($id, $type) |
|
31
|
|
|
{ |
|
32
|
10 |
|
$this->id = $id; |
|
33
|
10 |
|
$this->type = $type; |
|
34
|
10 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return int |
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function getId() |
|
40
|
|
|
{ |
|
41
|
8 |
|
return $this->id; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
8 |
|
public function getType() |
|
48
|
|
|
{ |
|
49
|
8 |
|
return $this->type; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function getUniqueId() |
|
56
|
|
|
{ |
|
57
|
5 |
|
$id = $this->getId(); |
|
58
|
5 |
|
$type = $this->getType(); |
|
59
|
|
|
|
|
60
|
5 |
|
return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function equals(TargetInterface $other): bool |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->getUniqueId() === $other->getUniqueId(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public function matches(TargetInterface $other): bool |
|
75
|
|
|
{ |
|
76
|
6 |
|
return $this->checkMatches($other) || $other->checkMatches($this); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
public function checkMatches(TargetInterface $other): bool |
|
80
|
|
|
{ |
|
81
|
6 |
|
if ($this->id === self::ANY) { |
|
82
|
1 |
|
if ($this->type === self::ANY) { |
|
|
|
|
|
|
83
|
1 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $this->matchTypes($other); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
if ($this->type === self::ANY) { |
|
|
|
|
|
|
90
|
|
|
return $this->matchIds($other); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
6 |
|
return $this->matchIds($other) && $this->matchTypes($other); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
6 |
|
protected function matchIds(TargetInterface $other) |
|
97
|
|
|
{ |
|
98
|
6 |
|
return $this->matchStrings($this->id, $other->getId()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
protected function matchTypes(TargetInterface $other) |
|
102
|
|
|
{ |
|
103
|
6 |
|
return $this->matchStrings($this->type, $other->getType()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
6 |
View Code Duplication |
protected function matchStrings($lhs, $rhs) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
6 |
|
if ($lhs === self::NONE || $rhs === self::NONE) { |
|
109
|
1 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
6 |
|
return (string) $lhs === (string) $rhs; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
public function jsonSerialize() |
|
117
|
|
|
{ |
|
118
|
|
|
return get_object_vars($this); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|