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