|
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, 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
|
|
|
public function __construct(array $targets) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->targets = $targets; |
|
32
|
|
|
$ids = []; |
|
33
|
|
|
$types = []; |
|
34
|
|
|
foreach ($targets as $target) { |
|
35
|
|
|
$ids[] = $target->getId(); |
|
36
|
|
|
$types[] = $target->getType(); |
|
37
|
|
|
} |
|
38
|
|
|
$this->ids = array_unique(array_filter($ids)); |
|
39
|
|
|
$this->types = array_unique(array_filter($types)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getId() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->getTarget()->getId(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getIds() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->ids; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getType() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getTarget()->getType(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getTypes() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->types; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getTarget() |
|
69
|
|
|
{ |
|
70
|
|
|
return reset($this->targets); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getUniqueId() |
|
77
|
|
|
{ |
|
78
|
|
|
return implode(':', array_filter([$this->getType(), $this->getId()])); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function equals(TargetInterface $other) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->getId() === null && $other->getId() === null |
|
87
|
|
|
? !empty(array_intersect($this->types, static::takeTypes($other))) |
|
88
|
|
|
: !empty(array_intersect($this->ids, static::takeIds($other))); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function takeIds(TargetInterface $other) |
|
92
|
|
|
{ |
|
93
|
|
|
return $other instanceof static ? $other->ids : [$other->getId()]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function takeTypes(TargetInterface $other) |
|
97
|
|
|
{ |
|
98
|
|
|
return $other instanceof static ? $other->types : [$other->getType()]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function jsonSerialize() |
|
102
|
|
|
{ |
|
103
|
|
|
return get_object_vars($this); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|