|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\X509\Feature; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\X501\ASN1\Attribute; |
|
8
|
|
|
use Sop\X501\ASN1\AttributeType; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait for objects containing X.501 attributes. |
|
12
|
|
|
* |
|
13
|
|
|
* Implements methods for Countable and IteratorAggregate interfaces. |
|
14
|
|
|
*/ |
|
15
|
|
|
trait AttributeContainer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Array of attributes. |
|
19
|
|
|
* |
|
20
|
|
|
* @var Attribute[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $_attributes; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Check whether attribute is present. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name OID or attribute name |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
12 |
|
public function has(string $name): bool |
|
32
|
|
|
{ |
|
33
|
12 |
|
return null !== $this->_findFirst($name); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get first attribute by OID or attribute name. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $name OID or attribute name |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \OutOfBoundsException |
|
42
|
|
|
* |
|
43
|
|
|
* @return Attribute |
|
44
|
|
|
*/ |
|
45
|
16 |
|
public function firstOf(string $name): Attribute |
|
46
|
|
|
{ |
|
47
|
16 |
|
$attr = $this->_findFirst($name); |
|
48
|
16 |
|
if (!$attr) { |
|
49
|
1 |
|
throw new \UnexpectedValueException("No {$name} attribute."); |
|
50
|
|
|
} |
|
51
|
15 |
|
return $attr; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get all attributes of given name. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name OID or attribute name |
|
58
|
|
|
* |
|
59
|
|
|
* @return Attribute[] |
|
60
|
|
|
*/ |
|
61
|
3 |
|
public function allOf(string $name): array |
|
62
|
|
|
{ |
|
63
|
3 |
|
$oid = AttributeType::attrNameToOID($name); |
|
64
|
3 |
|
$attrs = array_filter($this->_attributes, |
|
65
|
|
|
function (Attribute $attr) use ($oid) { |
|
66
|
3 |
|
return $attr->oid() === $oid; |
|
67
|
3 |
|
}); |
|
68
|
3 |
|
return array_values($attrs); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get all attributes. |
|
73
|
|
|
* |
|
74
|
|
|
* @return Attribute[] |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function all(): array |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->_attributes; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get self with additional attributes added. |
|
83
|
|
|
* |
|
84
|
|
|
* @param Attribute ...$attribs |
|
85
|
|
|
* |
|
86
|
|
|
* @return self |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function withAdditional(Attribute ...$attribs): self |
|
89
|
|
|
{ |
|
90
|
2 |
|
$obj = clone $this; |
|
91
|
2 |
|
foreach ($attribs as $attr) { |
|
92
|
2 |
|
$obj->_attributes[] = $attr; |
|
93
|
|
|
} |
|
94
|
2 |
|
return $obj; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get self with single unique attribute added. |
|
99
|
|
|
* |
|
100
|
|
|
* All previous attributes of the same type are removed. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Attribute $attr |
|
103
|
|
|
* |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
5 |
|
public function withUnique(Attribute $attr): self |
|
107
|
|
|
{ |
|
108
|
5 |
|
$obj = clone $this; |
|
109
|
5 |
|
$obj->_attributes = array_filter($obj->_attributes, |
|
110
|
|
|
function (Attribute $a) use ($attr) { |
|
111
|
3 |
|
return $a->oid() !== $attr->oid(); |
|
112
|
5 |
|
}); |
|
113
|
5 |
|
$obj->_attributes[] = $attr; |
|
114
|
5 |
|
return $obj; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get number of attributes. |
|
119
|
|
|
* |
|
120
|
|
|
* @see \Countable::count() |
|
121
|
|
|
* |
|
122
|
|
|
* @return int |
|
123
|
|
|
*/ |
|
124
|
6 |
|
public function count(): int |
|
125
|
|
|
{ |
|
126
|
6 |
|
return count($this->_attributes); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get iterator for attributes. |
|
131
|
|
|
* |
|
132
|
|
|
* @see \IteratorAggregate::getIterator() |
|
133
|
|
|
* |
|
134
|
|
|
* @return \ArrayIterator |
|
135
|
|
|
*/ |
|
136
|
3 |
|
public function getIterator(): \ArrayIterator |
|
137
|
|
|
{ |
|
138
|
3 |
|
return new \ArrayIterator($this->_attributes); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Find first attribute of given name or OID. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* |
|
146
|
|
|
* @return null|Attribute |
|
147
|
|
|
*/ |
|
148
|
25 |
|
protected function _findFirst(string $name): ?Attribute |
|
149
|
|
|
{ |
|
150
|
25 |
|
$oid = AttributeType::attrNameToOID($name); |
|
151
|
25 |
|
foreach ($this->_attributes as $attr) { |
|
152
|
24 |
|
if ($attr->oid() === $oid) { |
|
153
|
24 |
|
return $attr; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
2 |
|
return null; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|