1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace X509\Certificate\Extension\Target; |
4
|
|
|
|
5
|
|
|
use ASN1\Type\Constructed\Sequence; |
6
|
|
|
use ASN1\Type\UnspecifiedType; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Implements <i>Targets</i> ASN.1 type as a <i>SEQUENCE OF Target</i>. |
11
|
|
|
* |
12
|
|
|
* @link https://tools.ietf.org/html/rfc5755#section-4.3.2 |
13
|
|
|
*/ |
14
|
|
|
class Targets implements \Countable, \IteratorAggregate |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Target elements. |
18
|
|
|
* |
19
|
|
|
* @var Target[] $_targets |
20
|
|
|
*/ |
21
|
|
|
protected $_targets; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param Target ...$targets |
27
|
|
|
*/ |
28
|
11 |
|
public function __construct(Target ...$targets) { |
29
|
11 |
|
$this->_targets = $targets; |
30
|
11 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initialize from ASN.1. |
34
|
|
|
* |
35
|
|
|
* @param Sequence $seq |
36
|
|
|
* @return self |
37
|
|
|
*/ |
38
|
4 |
|
public static function fromASN1(Sequence $seq) { |
39
|
4 |
|
$targets = array_map( |
40
|
|
|
function (UnspecifiedType $el) { |
41
|
4 |
|
return Target::fromASN1($el->asTagged()); |
42
|
4 |
|
}, $seq->elements()); |
43
|
4 |
|
return new self(...$targets); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all targets. |
48
|
|
|
* |
49
|
|
|
* @return Target[] |
50
|
|
|
*/ |
51
|
7 |
|
public function all() { |
52
|
7 |
|
return $this->_targets; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get all targets of given type. |
57
|
|
|
* |
58
|
|
|
* @param int $type |
59
|
|
|
* @return Target[] |
60
|
|
|
*/ |
61
|
7 |
|
protected function _allOfType($type) { |
62
|
7 |
|
return array_values( |
63
|
6 |
|
array_filter($this->_targets, |
64
|
|
|
function (Target $target) use ($type) { |
65
|
6 |
|
return $target->type() == $type; |
66
|
6 |
|
})); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all name targets. |
71
|
|
|
* |
72
|
|
|
* @return Target[] |
73
|
|
|
*/ |
74
|
1 |
|
public function nameTargets() { |
75
|
1 |
|
return $this->_allOfType(Target::TYPE_NAME); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get all group targets. |
80
|
|
|
* |
81
|
|
|
* @return Target[] |
82
|
|
|
*/ |
83
|
1 |
|
public function groupTargets() { |
84
|
1 |
|
return $this->_allOfType(Target::TYPE_GROUP); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check whether given target is present. |
89
|
|
|
* |
90
|
|
|
* @param Target $target |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
4 |
|
public function hasTarget(Target $target) { |
94
|
4 |
|
foreach ($this->_allOfType($target->type()) as $t) { |
95
|
4 |
|
if ($target->equals($t)) { |
96
|
2 |
|
return true; |
97
|
|
|
} |
98
|
2 |
|
} |
99
|
2 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generate ASN.1 structure. |
104
|
|
|
* |
105
|
|
|
* @return Sequence |
106
|
|
|
*/ |
107
|
7 |
|
public function toASN1() { |
108
|
7 |
|
$elements = array_map( |
109
|
7 |
|
function (Target $target) { |
110
|
7 |
|
return $target->toASN1(); |
111
|
7 |
|
}, $this->_targets); |
112
|
7 |
|
return new Sequence(...$elements); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* @see Countable::count() |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
3 |
|
public function count() { |
121
|
3 |
|
return count($this->_targets); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get iterator for targets. |
126
|
|
|
* |
127
|
|
|
* @see IteratorAggregate::getIterator() |
128
|
|
|
* @return \ArrayIterator |
129
|
|
|
*/ |
130
|
1 |
|
public function getIterator() { |
131
|
1 |
|
return new \ArrayIterator($this->_targets); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|