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
|
|
|
class TargetCollection implements TargetInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Target[] |
22
|
|
|
*/ |
23
|
|
|
protected $targets; |
24
|
|
|
|
25
|
|
|
protected $ids; |
26
|
|
|
|
27
|
|
|
protected $types; |
28
|
|
|
|
29
|
3 |
|
public function __construct(array $targets) |
30
|
|
|
{ |
31
|
3 |
|
$this->targets = $targets; |
32
|
3 |
|
$ids = []; |
33
|
3 |
|
$types = []; |
34
|
3 |
|
foreach ($targets as $target) { |
35
|
3 |
|
$ids[] = $target->getId(); |
36
|
3 |
|
$types[] = $target->getType(); |
37
|
|
|
} |
38
|
3 |
|
$this->ids = array_unique(array_filter($ids)); |
39
|
3 |
|
$this->types = array_unique(array_filter($types)); |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
1 |
|
public function getId() |
46
|
|
|
{ |
47
|
1 |
|
return $this->getTarget()->getId(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getIds() |
51
|
|
|
{ |
52
|
|
|
return $this->ids; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function getType() |
59
|
|
|
{ |
60
|
1 |
|
return $this->getTarget()->getType(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getTypes() |
64
|
|
|
{ |
65
|
|
|
return $this->types; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function getTarget() |
69
|
|
|
{ |
70
|
1 |
|
return reset($this->targets); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function getTargets() |
74
|
|
|
{ |
75
|
1 |
|
return $this->targets; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function getUniqueId() |
82
|
|
|
{ |
83
|
1 |
|
return implode(':', array_filter([$this->getType(), $this->getId()])); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
1 |
|
public function matches(TargetInterface $other): bool |
90
|
|
|
{ |
91
|
1 |
|
return $this->checkMatches($other); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function checkMatches(TargetInterface $other): bool |
95
|
|
|
{ |
96
|
1 |
|
foreach ($this->targets as $target) { |
97
|
1 |
|
if ($target->checkMatches($other) || $other->checkMatches($target)) { |
98
|
1 |
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function takeIds(TargetInterface $other) |
106
|
|
|
{ |
107
|
|
|
return $other instanceof static ? $other->ids : [$other->getId()]; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public static function takeTypes(TargetInterface $other) |
111
|
|
|
{ |
112
|
|
|
return $other instanceof static ? $other->types : [$other->getType()]; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function jsonSerialize() |
116
|
|
|
{ |
117
|
|
|
return get_object_vars($this); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: