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 |
|
return $this->type . ':' . $this->id; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
2 |
|
public function equals(TargetInterface $other): bool |
64
|
|
|
{ |
65
|
2 |
|
return $this->getUniqueId() === $other->getUniqueId(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
6 |
|
public function matches(TargetInterface $other): bool |
72
|
|
|
{ |
73
|
6 |
|
return $this->checkMatches($other) || $other->checkMatches($this); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
public function checkMatches(TargetInterface $other): bool |
77
|
|
|
{ |
78
|
6 |
|
if ($this->id === self::ANY) { |
79
|
1 |
|
if ($this->type === self::ANY) { |
80
|
1 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $this->matchTypes($other); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
if ($this->type === self::ANY) { |
87
|
|
|
return $this->matchIds($other); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
return $this->matchIds($other) && $this->matchTypes($other); |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
protected function matchIds(TargetInterface $other) |
94
|
|
|
{ |
95
|
6 |
|
return $this->matchStrings($this->id, $other->getId()); |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
protected function matchTypes(TargetInterface $other) |
99
|
|
|
{ |
100
|
6 |
|
return $this->matchStrings($this->type, $other->getType()); |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
View Code Duplication |
protected function matchStrings($lhs, $rhs) |
|
|
|
|
104
|
|
|
{ |
105
|
6 |
|
if ($lhs === self::NONE || $rhs === self::NONE) { |
106
|
1 |
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
return (string) $lhs === (string) $rhs; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
public function jsonSerialize() |
114
|
|
|
{ |
115
|
|
|
return get_object_vars($this); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.