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