@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\MatchingRule; |
| 6 | 6 | |
@@ -11,11 +11,11 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class BinaryMatch extends MatchingRule |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * {@inheritdoc} |
|
| 16 | - */ |
|
| 17 | - public function compare($assertion, $value): ?bool |
|
| 18 | - { |
|
| 19 | - return 0 === strcmp($assertion, $value); |
|
| 20 | - } |
|
| 14 | + /** |
|
| 15 | + * {@inheritdoc} |
|
| 16 | + */ |
|
| 17 | + public function compare($assertion, $value): ?bool |
|
| 18 | + { |
|
| 19 | + return 0 === strcmp($assertion, $value); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\MatchingRule; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\ASN1\AttributeValue; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\ASN1\AttributeValue; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\ASN1; |
| 6 | 6 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | public static function fromASN1(Sequence $seq): self |
| 56 | 56 | { |
| 57 | 57 | $rdns = array_map( |
| 58 | - function (UnspecifiedType $el) { |
|
| 58 | + function(UnspecifiedType $el) { |
|
| 59 | 59 | return RDN::fromASN1($el->asSet()); |
| 60 | 60 | }, $seq->elements()); |
| 61 | 61 | return new self(...$rdns); |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | public function toASN1(): Sequence |
| 101 | 101 | { |
| 102 | 102 | $elements = array_map( |
| 103 | - function (RDN $rdn) { |
|
| 103 | + function(RDN $rdn) { |
|
| 104 | 104 | return $rdn->toASN1(); |
| 105 | 105 | }, $this->_rdns); |
| 106 | 106 | return new Sequence(...$elements); |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | public function toString(): string |
| 117 | 117 | { |
| 118 | 118 | $parts = array_map( |
| 119 | - function (RDN $rdn) { |
|
| 119 | + function(RDN $rdn) { |
|
| 120 | 120 | return $rdn->toString(); |
| 121 | 121 | }, array_reverse($this->_rdns)); |
| 122 | 122 | return implode(',', $parts); |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $oid = AttributeType::attrNameToOID($name); |
| 211 | 211 | return (int) array_sum( |
| 212 | 212 | array_map( |
| 213 | - function (RDN $rdn) use ($oid): int { |
|
| 213 | + function(RDN $rdn) use ($oid): int { |
|
| 214 | 214 | return count($rdn->allOf($oid)); |
| 215 | 215 | }, $this->_rdns)); |
| 216 | 216 | } |
@@ -20,208 +20,208 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Name implements \Countable, \IteratorAggregate |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Relative distinguished name components. |
|
| 25 | - * |
|
| 26 | - * @var RDN[] |
|
| 27 | - */ |
|
| 28 | - protected $_rdns; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param RDN ...$rdns RDN components |
|
| 34 | - */ |
|
| 35 | - public function __construct(RDN ...$rdns) |
|
| 36 | - { |
|
| 37 | - $this->_rdns = $rdns; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function __toString() |
|
| 44 | - { |
|
| 45 | - return $this->toString(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Initialize from ASN.1. |
|
| 50 | - * |
|
| 51 | - * @param Sequence $seq |
|
| 52 | - * |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromASN1(Sequence $seq): self |
|
| 56 | - { |
|
| 57 | - $rdns = array_map( |
|
| 58 | - function (UnspecifiedType $el) { |
|
| 59 | - return RDN::fromASN1($el->asSet()); |
|
| 60 | - }, $seq->elements()); |
|
| 61 | - return new self(...$rdns); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Initialize from distinguished name string. |
|
| 66 | - * |
|
| 67 | - * @see https://tools.ietf.org/html/rfc1779 |
|
| 68 | - * |
|
| 69 | - * @param string $str |
|
| 70 | - * |
|
| 71 | - * @return self |
|
| 72 | - */ |
|
| 73 | - public static function fromString(string $str): self |
|
| 74 | - { |
|
| 75 | - $rdns = []; |
|
| 76 | - foreach (DNParser::parseString($str) as $nameComponent) { |
|
| 77 | - $attribs = []; |
|
| 78 | - foreach ($nameComponent as [$name, $val]) { |
|
| 79 | - $type = AttributeType::fromName($name); |
|
| 80 | - // hexstrings are parsed to ASN.1 elements |
|
| 81 | - if ($val instanceof Element) { |
|
| 82 | - $el = $val; |
|
| 83 | - } else { |
|
| 84 | - $el = AttributeType::asn1StringForType($type->oid(), $val); |
|
| 85 | - } |
|
| 86 | - $value = AttributeValue::fromASN1ByOID($type->oid(), |
|
| 87 | - $el->asUnspecified()); |
|
| 88 | - $attribs[] = new AttributeTypeAndValue($type, $value); |
|
| 89 | - } |
|
| 90 | - $rdns[] = new RDN(...$attribs); |
|
| 91 | - } |
|
| 92 | - return new self(...$rdns); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Generate ASN.1 structure. |
|
| 97 | - * |
|
| 98 | - * @return Sequence |
|
| 99 | - */ |
|
| 100 | - public function toASN1(): Sequence |
|
| 101 | - { |
|
| 102 | - $elements = array_map( |
|
| 103 | - function (RDN $rdn) { |
|
| 104 | - return $rdn->toASN1(); |
|
| 105 | - }, $this->_rdns); |
|
| 106 | - return new Sequence(...$elements); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get distinguised name string conforming to RFC 2253. |
|
| 111 | - * |
|
| 112 | - * @see https://tools.ietf.org/html/rfc2253#section-2.1 |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function toString(): string |
|
| 117 | - { |
|
| 118 | - $parts = array_map( |
|
| 119 | - function (RDN $rdn) { |
|
| 120 | - return $rdn->toString(); |
|
| 121 | - }, array_reverse($this->_rdns)); |
|
| 122 | - return implode(',', $parts); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Whether name is semantically equal to other. |
|
| 127 | - * |
|
| 128 | - * Comparison conforms to RFC 4518 string preparation algorithm. |
|
| 129 | - * |
|
| 130 | - * @see https://tools.ietf.org/html/rfc4518 |
|
| 131 | - * |
|
| 132 | - * @param Name $other Object to compare to |
|
| 133 | - * |
|
| 134 | - * @return bool |
|
| 135 | - */ |
|
| 136 | - public function equals(Name $other): bool |
|
| 137 | - { |
|
| 138 | - // if RDN count doesn't match |
|
| 139 | - if (count($this) !== count($other)) { |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - for ($i = count($this) - 1; $i >= 0; --$i) { |
|
| 143 | - $rdn1 = $this->_rdns[$i]; |
|
| 144 | - $rdn2 = $other->_rdns[$i]; |
|
| 145 | - if (!$rdn1->equals($rdn2)) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - return true; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get all RDN objects. |
|
| 154 | - * |
|
| 155 | - * @return RDN[] |
|
| 156 | - */ |
|
| 157 | - public function all(): array |
|
| 158 | - { |
|
| 159 | - return $this->_rdns; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the first AttributeValue of given type. |
|
| 164 | - * |
|
| 165 | - * Relative name components shall be traversed in encoding order, which is |
|
| 166 | - * reversed in regards to the string representation. |
|
| 167 | - * Multi-valued RDN with multiple attributes of the requested type is |
|
| 168 | - * ambiguous and shall throw an exception. |
|
| 169 | - * |
|
| 170 | - * @param string $name Attribute OID or name |
|
| 171 | - * |
|
| 172 | - * @throws \RuntimeException If attribute cannot be resolved |
|
| 173 | - * |
|
| 174 | - * @return AttributeValue |
|
| 175 | - */ |
|
| 176 | - public function firstValueOf(string $name): AttributeValue |
|
| 177 | - { |
|
| 178 | - $oid = AttributeType::attrNameToOID($name); |
|
| 179 | - foreach ($this->_rdns as $rdn) { |
|
| 180 | - $tvs = $rdn->allOf($oid); |
|
| 181 | - if (count($tvs) > 1) { |
|
| 182 | - throw new \RangeException("RDN with multiple {$name} attributes."); |
|
| 183 | - } |
|
| 184 | - if (1 === count($tvs)) { |
|
| 185 | - return $tvs[0]->value(); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - throw new \RangeException("Attribute {$name} not found."); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @see \Countable::count() |
|
| 193 | - * |
|
| 194 | - * @return int |
|
| 195 | - */ |
|
| 196 | - public function count(): int |
|
| 197 | - { |
|
| 198 | - return count($this->_rdns); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Get the number of attributes of given type. |
|
| 203 | - * |
|
| 204 | - * @param string $name Attribute OID or name |
|
| 205 | - * |
|
| 206 | - * @return int |
|
| 207 | - */ |
|
| 208 | - public function countOfType(string $name): int |
|
| 209 | - { |
|
| 210 | - $oid = AttributeType::attrNameToOID($name); |
|
| 211 | - return (int) array_sum( |
|
| 212 | - array_map( |
|
| 213 | - function (RDN $rdn) use ($oid): int { |
|
| 214 | - return count($rdn->allOf($oid)); |
|
| 215 | - }, $this->_rdns)); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @see \IteratorAggregate::getIterator() |
|
| 220 | - * |
|
| 221 | - * @return \ArrayIterator |
|
| 222 | - */ |
|
| 223 | - public function getIterator(): \ArrayIterator |
|
| 224 | - { |
|
| 225 | - return new \ArrayIterator($this->_rdns); |
|
| 226 | - } |
|
| 23 | + /** |
|
| 24 | + * Relative distinguished name components. |
|
| 25 | + * |
|
| 26 | + * @var RDN[] |
|
| 27 | + */ |
|
| 28 | + protected $_rdns; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param RDN ...$rdns RDN components |
|
| 34 | + */ |
|
| 35 | + public function __construct(RDN ...$rdns) |
|
| 36 | + { |
|
| 37 | + $this->_rdns = $rdns; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function __toString() |
|
| 44 | + { |
|
| 45 | + return $this->toString(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Initialize from ASN.1. |
|
| 50 | + * |
|
| 51 | + * @param Sequence $seq |
|
| 52 | + * |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromASN1(Sequence $seq): self |
|
| 56 | + { |
|
| 57 | + $rdns = array_map( |
|
| 58 | + function (UnspecifiedType $el) { |
|
| 59 | + return RDN::fromASN1($el->asSet()); |
|
| 60 | + }, $seq->elements()); |
|
| 61 | + return new self(...$rdns); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Initialize from distinguished name string. |
|
| 66 | + * |
|
| 67 | + * @see https://tools.ietf.org/html/rfc1779 |
|
| 68 | + * |
|
| 69 | + * @param string $str |
|
| 70 | + * |
|
| 71 | + * @return self |
|
| 72 | + */ |
|
| 73 | + public static function fromString(string $str): self |
|
| 74 | + { |
|
| 75 | + $rdns = []; |
|
| 76 | + foreach (DNParser::parseString($str) as $nameComponent) { |
|
| 77 | + $attribs = []; |
|
| 78 | + foreach ($nameComponent as [$name, $val]) { |
|
| 79 | + $type = AttributeType::fromName($name); |
|
| 80 | + // hexstrings are parsed to ASN.1 elements |
|
| 81 | + if ($val instanceof Element) { |
|
| 82 | + $el = $val; |
|
| 83 | + } else { |
|
| 84 | + $el = AttributeType::asn1StringForType($type->oid(), $val); |
|
| 85 | + } |
|
| 86 | + $value = AttributeValue::fromASN1ByOID($type->oid(), |
|
| 87 | + $el->asUnspecified()); |
|
| 88 | + $attribs[] = new AttributeTypeAndValue($type, $value); |
|
| 89 | + } |
|
| 90 | + $rdns[] = new RDN(...$attribs); |
|
| 91 | + } |
|
| 92 | + return new self(...$rdns); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Generate ASN.1 structure. |
|
| 97 | + * |
|
| 98 | + * @return Sequence |
|
| 99 | + */ |
|
| 100 | + public function toASN1(): Sequence |
|
| 101 | + { |
|
| 102 | + $elements = array_map( |
|
| 103 | + function (RDN $rdn) { |
|
| 104 | + return $rdn->toASN1(); |
|
| 105 | + }, $this->_rdns); |
|
| 106 | + return new Sequence(...$elements); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get distinguised name string conforming to RFC 2253. |
|
| 111 | + * |
|
| 112 | + * @see https://tools.ietf.org/html/rfc2253#section-2.1 |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function toString(): string |
|
| 117 | + { |
|
| 118 | + $parts = array_map( |
|
| 119 | + function (RDN $rdn) { |
|
| 120 | + return $rdn->toString(); |
|
| 121 | + }, array_reverse($this->_rdns)); |
|
| 122 | + return implode(',', $parts); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Whether name is semantically equal to other. |
|
| 127 | + * |
|
| 128 | + * Comparison conforms to RFC 4518 string preparation algorithm. |
|
| 129 | + * |
|
| 130 | + * @see https://tools.ietf.org/html/rfc4518 |
|
| 131 | + * |
|
| 132 | + * @param Name $other Object to compare to |
|
| 133 | + * |
|
| 134 | + * @return bool |
|
| 135 | + */ |
|
| 136 | + public function equals(Name $other): bool |
|
| 137 | + { |
|
| 138 | + // if RDN count doesn't match |
|
| 139 | + if (count($this) !== count($other)) { |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + for ($i = count($this) - 1; $i >= 0; --$i) { |
|
| 143 | + $rdn1 = $this->_rdns[$i]; |
|
| 144 | + $rdn2 = $other->_rdns[$i]; |
|
| 145 | + if (!$rdn1->equals($rdn2)) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + return true; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get all RDN objects. |
|
| 154 | + * |
|
| 155 | + * @return RDN[] |
|
| 156 | + */ |
|
| 157 | + public function all(): array |
|
| 158 | + { |
|
| 159 | + return $this->_rdns; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the first AttributeValue of given type. |
|
| 164 | + * |
|
| 165 | + * Relative name components shall be traversed in encoding order, which is |
|
| 166 | + * reversed in regards to the string representation. |
|
| 167 | + * Multi-valued RDN with multiple attributes of the requested type is |
|
| 168 | + * ambiguous and shall throw an exception. |
|
| 169 | + * |
|
| 170 | + * @param string $name Attribute OID or name |
|
| 171 | + * |
|
| 172 | + * @throws \RuntimeException If attribute cannot be resolved |
|
| 173 | + * |
|
| 174 | + * @return AttributeValue |
|
| 175 | + */ |
|
| 176 | + public function firstValueOf(string $name): AttributeValue |
|
| 177 | + { |
|
| 178 | + $oid = AttributeType::attrNameToOID($name); |
|
| 179 | + foreach ($this->_rdns as $rdn) { |
|
| 180 | + $tvs = $rdn->allOf($oid); |
|
| 181 | + if (count($tvs) > 1) { |
|
| 182 | + throw new \RangeException("RDN with multiple {$name} attributes."); |
|
| 183 | + } |
|
| 184 | + if (1 === count($tvs)) { |
|
| 185 | + return $tvs[0]->value(); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + throw new \RangeException("Attribute {$name} not found."); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @see \Countable::count() |
|
| 193 | + * |
|
| 194 | + * @return int |
|
| 195 | + */ |
|
| 196 | + public function count(): int |
|
| 197 | + { |
|
| 198 | + return count($this->_rdns); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Get the number of attributes of given type. |
|
| 203 | + * |
|
| 204 | + * @param string $name Attribute OID or name |
|
| 205 | + * |
|
| 206 | + * @return int |
|
| 207 | + */ |
|
| 208 | + public function countOfType(string $name): int |
|
| 209 | + { |
|
| 210 | + $oid = AttributeType::attrNameToOID($name); |
|
| 211 | + return (int) array_sum( |
|
| 212 | + array_map( |
|
| 213 | + function (RDN $rdn) use ($oid): int { |
|
| 214 | + return count($rdn->allOf($oid)); |
|
| 215 | + }, $this->_rdns)); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @see \IteratorAggregate::getIterator() |
|
| 220 | + * |
|
| 221 | + * @return \ArrayIterator |
|
| 222 | + */ |
|
| 223 | + public function getIterator(): \ArrayIterator |
|
| 224 | + { |
|
| 225 | + return new \ArrayIterator($this->_rdns); |
|
| 226 | + } |
|
| 227 | 227 | } |
@@ -15,173 +15,173 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class RDN implements \Countable, \IteratorAggregate |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Attributes. |
|
| 20 | - * |
|
| 21 | - * @var AttributeTypeAndValue[] |
|
| 22 | - */ |
|
| 23 | - protected $_attribs; |
|
| 18 | + /** |
|
| 19 | + * Attributes. |
|
| 20 | + * |
|
| 21 | + * @var AttributeTypeAndValue[] |
|
| 22 | + */ |
|
| 23 | + protected $_attribs; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Constructor. |
|
| 27 | - * |
|
| 28 | - * @param AttributeTypeAndValue ...$attribs One or more attributes |
|
| 29 | - */ |
|
| 30 | - public function __construct(AttributeTypeAndValue ...$attribs) |
|
| 31 | - { |
|
| 32 | - if (!count($attribs)) { |
|
| 33 | - throw new \UnexpectedValueException( |
|
| 34 | - 'RDN must have at least one AttributeTypeAndValue.'); |
|
| 35 | - } |
|
| 36 | - $this->_attribs = $attribs; |
|
| 37 | - } |
|
| 25 | + /** |
|
| 26 | + * Constructor. |
|
| 27 | + * |
|
| 28 | + * @param AttributeTypeAndValue ...$attribs One or more attributes |
|
| 29 | + */ |
|
| 30 | + public function __construct(AttributeTypeAndValue ...$attribs) |
|
| 31 | + { |
|
| 32 | + if (!count($attribs)) { |
|
| 33 | + throw new \UnexpectedValueException( |
|
| 34 | + 'RDN must have at least one AttributeTypeAndValue.'); |
|
| 35 | + } |
|
| 36 | + $this->_attribs = $attribs; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function __toString() |
|
| 43 | - { |
|
| 44 | - return $this->toString(); |
|
| 45 | - } |
|
| 39 | + /** |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function __toString() |
|
| 43 | + { |
|
| 44 | + return $this->toString(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Convenience method to initialize RDN from AttributeValue objects. |
|
| 49 | - * |
|
| 50 | - * @param AttributeValue ...$values One or more attributes |
|
| 51 | - * |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public static function fromAttributeValues(AttributeValue ...$values): self |
|
| 55 | - { |
|
| 56 | - $attribs = array_map( |
|
| 57 | - function (AttributeValue $value) { |
|
| 58 | - return new AttributeTypeAndValue( |
|
| 59 | - new AttributeType($value->oid()), $value); |
|
| 60 | - }, $values); |
|
| 61 | - return new self(...$attribs); |
|
| 62 | - } |
|
| 47 | + /** |
|
| 48 | + * Convenience method to initialize RDN from AttributeValue objects. |
|
| 49 | + * |
|
| 50 | + * @param AttributeValue ...$values One or more attributes |
|
| 51 | + * |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public static function fromAttributeValues(AttributeValue ...$values): self |
|
| 55 | + { |
|
| 56 | + $attribs = array_map( |
|
| 57 | + function (AttributeValue $value) { |
|
| 58 | + return new AttributeTypeAndValue( |
|
| 59 | + new AttributeType($value->oid()), $value); |
|
| 60 | + }, $values); |
|
| 61 | + return new self(...$attribs); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Initialize from ASN.1. |
|
| 66 | - * |
|
| 67 | - * @param Set $set |
|
| 68 | - * |
|
| 69 | - * @return self |
|
| 70 | - */ |
|
| 71 | - public static function fromASN1(Set $set): self |
|
| 72 | - { |
|
| 73 | - $attribs = array_map( |
|
| 74 | - function (UnspecifiedType $el) { |
|
| 75 | - return AttributeTypeAndValue::fromASN1($el->asSequence()); |
|
| 76 | - }, $set->elements()); |
|
| 77 | - return new self(...$attribs); |
|
| 78 | - } |
|
| 64 | + /** |
|
| 65 | + * Initialize from ASN.1. |
|
| 66 | + * |
|
| 67 | + * @param Set $set |
|
| 68 | + * |
|
| 69 | + * @return self |
|
| 70 | + */ |
|
| 71 | + public static function fromASN1(Set $set): self |
|
| 72 | + { |
|
| 73 | + $attribs = array_map( |
|
| 74 | + function (UnspecifiedType $el) { |
|
| 75 | + return AttributeTypeAndValue::fromASN1($el->asSequence()); |
|
| 76 | + }, $set->elements()); |
|
| 77 | + return new self(...$attribs); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Generate ASN.1 structure. |
|
| 82 | - * |
|
| 83 | - * @return Set |
|
| 84 | - */ |
|
| 85 | - public function toASN1(): Set |
|
| 86 | - { |
|
| 87 | - $elements = array_map( |
|
| 88 | - function (AttributeTypeAndValue $tv) { |
|
| 89 | - return $tv->toASN1(); |
|
| 90 | - }, $this->_attribs); |
|
| 91 | - $set = new Set(...$elements); |
|
| 92 | - return $set->sortedSetOf(); |
|
| 93 | - } |
|
| 80 | + /** |
|
| 81 | + * Generate ASN.1 structure. |
|
| 82 | + * |
|
| 83 | + * @return Set |
|
| 84 | + */ |
|
| 85 | + public function toASN1(): Set |
|
| 86 | + { |
|
| 87 | + $elements = array_map( |
|
| 88 | + function (AttributeTypeAndValue $tv) { |
|
| 89 | + return $tv->toASN1(); |
|
| 90 | + }, $this->_attribs); |
|
| 91 | + $set = new Set(...$elements); |
|
| 92 | + return $set->sortedSetOf(); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Get name-component string conforming to RFC 2253. |
|
| 97 | - * |
|
| 98 | - * @see https://tools.ietf.org/html/rfc2253#section-2.2 |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function toString(): string |
|
| 103 | - { |
|
| 104 | - $parts = array_map( |
|
| 105 | - function (AttributeTypeAndValue $tv) { |
|
| 106 | - return $tv->toString(); |
|
| 107 | - }, $this->_attribs); |
|
| 108 | - return implode('+', $parts); |
|
| 109 | - } |
|
| 95 | + /** |
|
| 96 | + * Get name-component string conforming to RFC 2253. |
|
| 97 | + * |
|
| 98 | + * @see https://tools.ietf.org/html/rfc2253#section-2.2 |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function toString(): string |
|
| 103 | + { |
|
| 104 | + $parts = array_map( |
|
| 105 | + function (AttributeTypeAndValue $tv) { |
|
| 106 | + return $tv->toString(); |
|
| 107 | + }, $this->_attribs); |
|
| 108 | + return implode('+', $parts); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Check whether RDN is semantically equal to other. |
|
| 113 | - * |
|
| 114 | - * @param RDN $other Object to compare to |
|
| 115 | - * |
|
| 116 | - * @return bool |
|
| 117 | - */ |
|
| 118 | - public function equals(RDN $other): bool |
|
| 119 | - { |
|
| 120 | - // if attribute count doesn't match |
|
| 121 | - if (count($this) !== count($other)) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - $attribs1 = $this->_attribs; |
|
| 125 | - $attribs2 = $other->_attribs; |
|
| 126 | - // if there's multiple attributes, sort using SET OF rules |
|
| 127 | - if (count($attribs1) > 1) { |
|
| 128 | - $attribs1 = self::fromASN1($this->toASN1())->_attribs; |
|
| 129 | - $attribs2 = self::fromASN1($other->toASN1())->_attribs; |
|
| 130 | - } |
|
| 131 | - for ($i = count($attribs1) - 1; $i >= 0; --$i) { |
|
| 132 | - $tv1 = $attribs1[$i]; |
|
| 133 | - $tv2 = $attribs2[$i]; |
|
| 134 | - if (!$tv1->equals($tv2)) { |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - return true; |
|
| 139 | - } |
|
| 111 | + /** |
|
| 112 | + * Check whether RDN is semantically equal to other. |
|
| 113 | + * |
|
| 114 | + * @param RDN $other Object to compare to |
|
| 115 | + * |
|
| 116 | + * @return bool |
|
| 117 | + */ |
|
| 118 | + public function equals(RDN $other): bool |
|
| 119 | + { |
|
| 120 | + // if attribute count doesn't match |
|
| 121 | + if (count($this) !== count($other)) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + $attribs1 = $this->_attribs; |
|
| 125 | + $attribs2 = $other->_attribs; |
|
| 126 | + // if there's multiple attributes, sort using SET OF rules |
|
| 127 | + if (count($attribs1) > 1) { |
|
| 128 | + $attribs1 = self::fromASN1($this->toASN1())->_attribs; |
|
| 129 | + $attribs2 = self::fromASN1($other->toASN1())->_attribs; |
|
| 130 | + } |
|
| 131 | + for ($i = count($attribs1) - 1; $i >= 0; --$i) { |
|
| 132 | + $tv1 = $attribs1[$i]; |
|
| 133 | + $tv2 = $attribs2[$i]; |
|
| 134 | + if (!$tv1->equals($tv2)) { |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + return true; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Get all AttributeTypeAndValue objects. |
|
| 143 | - * |
|
| 144 | - * @return AttributeTypeAndValue[] |
|
| 145 | - */ |
|
| 146 | - public function all(): array |
|
| 147 | - { |
|
| 148 | - return $this->_attribs; |
|
| 149 | - } |
|
| 141 | + /** |
|
| 142 | + * Get all AttributeTypeAndValue objects. |
|
| 143 | + * |
|
| 144 | + * @return AttributeTypeAndValue[] |
|
| 145 | + */ |
|
| 146 | + public function all(): array |
|
| 147 | + { |
|
| 148 | + return $this->_attribs; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Get all AttributeTypeAndValue objects of the given attribute type. |
|
| 153 | - * |
|
| 154 | - * @param string $name Attribute OID or name |
|
| 155 | - * |
|
| 156 | - * @return AttributeTypeAndValue[] |
|
| 157 | - */ |
|
| 158 | - public function allOf(string $name): array |
|
| 159 | - { |
|
| 160 | - $oid = AttributeType::attrNameToOID($name); |
|
| 161 | - $attribs = array_filter($this->_attribs, |
|
| 162 | - function (AttributeTypeAndValue $tv) use ($oid) { |
|
| 163 | - return $tv->oid() === $oid; |
|
| 164 | - }); |
|
| 165 | - return array_values($attribs); |
|
| 166 | - } |
|
| 151 | + /** |
|
| 152 | + * Get all AttributeTypeAndValue objects of the given attribute type. |
|
| 153 | + * |
|
| 154 | + * @param string $name Attribute OID or name |
|
| 155 | + * |
|
| 156 | + * @return AttributeTypeAndValue[] |
|
| 157 | + */ |
|
| 158 | + public function allOf(string $name): array |
|
| 159 | + { |
|
| 160 | + $oid = AttributeType::attrNameToOID($name); |
|
| 161 | + $attribs = array_filter($this->_attribs, |
|
| 162 | + function (AttributeTypeAndValue $tv) use ($oid) { |
|
| 163 | + return $tv->oid() === $oid; |
|
| 164 | + }); |
|
| 165 | + return array_values($attribs); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @see \Countable::count() |
|
| 170 | - * |
|
| 171 | - * @return int |
|
| 172 | - */ |
|
| 173 | - public function count(): int |
|
| 174 | - { |
|
| 175 | - return count($this->_attribs); |
|
| 176 | - } |
|
| 168 | + /** |
|
| 169 | + * @see \Countable::count() |
|
| 170 | + * |
|
| 171 | + * @return int |
|
| 172 | + */ |
|
| 173 | + public function count(): int |
|
| 174 | + { |
|
| 175 | + return count($this->_attribs); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * @see \IteratorAggregate::getIterator() |
|
| 180 | - * |
|
| 181 | - * @return \ArrayIterator |
|
| 182 | - */ |
|
| 183 | - public function getIterator(): \ArrayIterator |
|
| 184 | - { |
|
| 185 | - return new \ArrayIterator($this->_attribs); |
|
| 186 | - } |
|
| 178 | + /** |
|
| 179 | + * @see \IteratorAggregate::getIterator() |
|
| 180 | + * |
|
| 181 | + * @return \ArrayIterator |
|
| 182 | + */ |
|
| 183 | + public function getIterator(): \ArrayIterator |
|
| 184 | + { |
|
| 185 | + return new \ArrayIterator($this->_attribs); |
|
| 186 | + } |
|
| 187 | 187 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\ASN1; |
| 6 | 6 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | public static function fromAttributeValues(AttributeValue ...$values): self |
| 55 | 55 | { |
| 56 | 56 | $attribs = array_map( |
| 57 | - function (AttributeValue $value) { |
|
| 57 | + function(AttributeValue $value) { |
|
| 58 | 58 | return new AttributeTypeAndValue( |
| 59 | 59 | new AttributeType($value->oid()), $value); |
| 60 | 60 | }, $values); |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | public static function fromASN1(Set $set): self |
| 72 | 72 | { |
| 73 | 73 | $attribs = array_map( |
| 74 | - function (UnspecifiedType $el) { |
|
| 74 | + function(UnspecifiedType $el) { |
|
| 75 | 75 | return AttributeTypeAndValue::fromASN1($el->asSequence()); |
| 76 | 76 | }, $set->elements()); |
| 77 | 77 | return new self(...$attribs); |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | public function toASN1(): Set |
| 86 | 86 | { |
| 87 | 87 | $elements = array_map( |
| 88 | - function (AttributeTypeAndValue $tv) { |
|
| 88 | + function(AttributeTypeAndValue $tv) { |
|
| 89 | 89 | return $tv->toASN1(); |
| 90 | 90 | }, $this->_attribs); |
| 91 | 91 | $set = new Set(...$elements); |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | public function toString(): string |
| 103 | 103 | { |
| 104 | 104 | $parts = array_map( |
| 105 | - function (AttributeTypeAndValue $tv) { |
|
| 105 | + function(AttributeTypeAndValue $tv) { |
|
| 106 | 106 | return $tv->toString(); |
| 107 | 107 | }, $this->_attribs); |
| 108 | 108 | return implode('+', $parts); |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | { |
| 160 | 160 | $oid = AttributeType::attrNameToOID($name); |
| 161 | 161 | $attribs = array_filter($this->_attribs, |
| 162 | - function (AttributeTypeAndValue $tv) use ($oid) { |
|
| 162 | + function(AttributeTypeAndValue $tv) use ($oid) { |
|
| 163 | 163 | return $tv->oid() === $oid; |
| 164 | 164 | }); |
| 165 | 165 | return array_values($attribs); |
@@ -17,161 +17,161 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Attribute implements \Countable, \IteratorAggregate |
| 19 | 19 | { |
| 20 | - use TypedAttribute; |
|
| 20 | + use TypedAttribute; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Attribute values. |
|
| 24 | - * |
|
| 25 | - * @var AttributeValue[] |
|
| 26 | - */ |
|
| 27 | - protected $_values; |
|
| 22 | + /** |
|
| 23 | + * Attribute values. |
|
| 24 | + * |
|
| 25 | + * @var AttributeValue[] |
|
| 26 | + */ |
|
| 27 | + protected $_values; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param AttributeType $type Attribute type |
|
| 33 | - * @param AttributeValue ...$values Attribute values |
|
| 34 | - */ |
|
| 35 | - public function __construct(AttributeType $type, AttributeValue ...$values) |
|
| 36 | - { |
|
| 37 | - // check that attribute values have correct oid |
|
| 38 | - array_walk($values, |
|
| 39 | - function (AttributeValue $value) use ($type) { |
|
| 40 | - if ($value->oid() !== $type->oid()) { |
|
| 41 | - throw new \LogicException('Attribute OID mismatch.'); |
|
| 42 | - } |
|
| 43 | - }); |
|
| 44 | - $this->_type = $type; |
|
| 45 | - $this->_values = $values; |
|
| 46 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param AttributeType $type Attribute type |
|
| 33 | + * @param AttributeValue ...$values Attribute values |
|
| 34 | + */ |
|
| 35 | + public function __construct(AttributeType $type, AttributeValue ...$values) |
|
| 36 | + { |
|
| 37 | + // check that attribute values have correct oid |
|
| 38 | + array_walk($values, |
|
| 39 | + function (AttributeValue $value) use ($type) { |
|
| 40 | + if ($value->oid() !== $type->oid()) { |
|
| 41 | + throw new \LogicException('Attribute OID mismatch.'); |
|
| 42 | + } |
|
| 43 | + }); |
|
| 44 | + $this->_type = $type; |
|
| 45 | + $this->_values = $values; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Initialize from ASN.1. |
|
| 50 | - * |
|
| 51 | - * @param Sequence $seq |
|
| 52 | - * |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromASN1(Sequence $seq): self |
|
| 56 | - { |
|
| 57 | - $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
|
| 58 | - $values = array_map( |
|
| 59 | - function (UnspecifiedType $el) use ($type) { |
|
| 60 | - return AttributeValue::fromASN1ByOID($type->oid(), $el); |
|
| 61 | - }, $seq->at(1)->asSet()->elements()); |
|
| 62 | - return new self($type, ...$values); |
|
| 63 | - } |
|
| 48 | + /** |
|
| 49 | + * Initialize from ASN.1. |
|
| 50 | + * |
|
| 51 | + * @param Sequence $seq |
|
| 52 | + * |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromASN1(Sequence $seq): self |
|
| 56 | + { |
|
| 57 | + $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
|
| 58 | + $values = array_map( |
|
| 59 | + function (UnspecifiedType $el) use ($type) { |
|
| 60 | + return AttributeValue::fromASN1ByOID($type->oid(), $el); |
|
| 61 | + }, $seq->at(1)->asSet()->elements()); |
|
| 62 | + return new self($type, ...$values); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Convenience method to initialize from attribute values. |
|
| 67 | - * |
|
| 68 | - * @param AttributeValue ...$values One or more values |
|
| 69 | - * |
|
| 70 | - * @throws \LogicException |
|
| 71 | - * |
|
| 72 | - * @return self |
|
| 73 | - */ |
|
| 74 | - public static function fromAttributeValues(AttributeValue ...$values): self |
|
| 75 | - { |
|
| 76 | - // we need at least one value to determine OID |
|
| 77 | - if (!count($values)) { |
|
| 78 | - throw new \LogicException('No values.'); |
|
| 79 | - } |
|
| 80 | - $oid = reset($values)->oid(); |
|
| 81 | - return new self(new AttributeType($oid), ...$values); |
|
| 82 | - } |
|
| 65 | + /** |
|
| 66 | + * Convenience method to initialize from attribute values. |
|
| 67 | + * |
|
| 68 | + * @param AttributeValue ...$values One or more values |
|
| 69 | + * |
|
| 70 | + * @throws \LogicException |
|
| 71 | + * |
|
| 72 | + * @return self |
|
| 73 | + */ |
|
| 74 | + public static function fromAttributeValues(AttributeValue ...$values): self |
|
| 75 | + { |
|
| 76 | + // we need at least one value to determine OID |
|
| 77 | + if (!count($values)) { |
|
| 78 | + throw new \LogicException('No values.'); |
|
| 79 | + } |
|
| 80 | + $oid = reset($values)->oid(); |
|
| 81 | + return new self(new AttributeType($oid), ...$values); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Get first value of the attribute. |
|
| 86 | - * |
|
| 87 | - * @throws \LogicException |
|
| 88 | - * |
|
| 89 | - * @return AttributeValue |
|
| 90 | - */ |
|
| 91 | - public function first(): AttributeValue |
|
| 92 | - { |
|
| 93 | - if (!count($this->_values)) { |
|
| 94 | - throw new \LogicException('Attribute contains no values.'); |
|
| 95 | - } |
|
| 96 | - return $this->_values[0]; |
|
| 97 | - } |
|
| 84 | + /** |
|
| 85 | + * Get first value of the attribute. |
|
| 86 | + * |
|
| 87 | + * @throws \LogicException |
|
| 88 | + * |
|
| 89 | + * @return AttributeValue |
|
| 90 | + */ |
|
| 91 | + public function first(): AttributeValue |
|
| 92 | + { |
|
| 93 | + if (!count($this->_values)) { |
|
| 94 | + throw new \LogicException('Attribute contains no values.'); |
|
| 95 | + } |
|
| 96 | + return $this->_values[0]; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Get all values. |
|
| 101 | - * |
|
| 102 | - * @return AttributeValue[] |
|
| 103 | - */ |
|
| 104 | - public function values(): array |
|
| 105 | - { |
|
| 106 | - return $this->_values; |
|
| 107 | - } |
|
| 99 | + /** |
|
| 100 | + * Get all values. |
|
| 101 | + * |
|
| 102 | + * @return AttributeValue[] |
|
| 103 | + */ |
|
| 104 | + public function values(): array |
|
| 105 | + { |
|
| 106 | + return $this->_values; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Generate ASN.1 structure. |
|
| 111 | - * |
|
| 112 | - * @return Sequence |
|
| 113 | - */ |
|
| 114 | - public function toASN1(): Sequence |
|
| 115 | - { |
|
| 116 | - $values = array_map( |
|
| 117 | - function (AttributeValue $value) { |
|
| 118 | - return $value->toASN1(); |
|
| 119 | - }, $this->_values); |
|
| 120 | - $valueset = new Set(...$values); |
|
| 121 | - return new Sequence($this->_type->toASN1(), $valueset->sortedSetOf()); |
|
| 122 | - } |
|
| 109 | + /** |
|
| 110 | + * Generate ASN.1 structure. |
|
| 111 | + * |
|
| 112 | + * @return Sequence |
|
| 113 | + */ |
|
| 114 | + public function toASN1(): Sequence |
|
| 115 | + { |
|
| 116 | + $values = array_map( |
|
| 117 | + function (AttributeValue $value) { |
|
| 118 | + return $value->toASN1(); |
|
| 119 | + }, $this->_values); |
|
| 120 | + $valueset = new Set(...$values); |
|
| 121 | + return new Sequence($this->_type->toASN1(), $valueset->sortedSetOf()); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Cast attribute values to another AttributeValue class. |
|
| 126 | - * |
|
| 127 | - * This method is generally used to cast UnknownAttributeValue values |
|
| 128 | - * to specific objects when class is declared outside this package. |
|
| 129 | - * |
|
| 130 | - * The new class must be derived from AttributeValue and have the same OID |
|
| 131 | - * as current attribute values. |
|
| 132 | - * |
|
| 133 | - * @param string $cls AttributeValue class name |
|
| 134 | - * |
|
| 135 | - * @throws \LogicException |
|
| 136 | - * |
|
| 137 | - * @return self |
|
| 138 | - */ |
|
| 139 | - public function castValues(string $cls): self |
|
| 140 | - { |
|
| 141 | - $refl = new \ReflectionClass($cls); |
|
| 142 | - if (!$refl->isSubclassOf(AttributeValue::class)) { |
|
| 143 | - throw new \LogicException( |
|
| 144 | - "{$cls} must be derived from " . AttributeValue::class . '.'); |
|
| 145 | - } |
|
| 146 | - $oid = $this->oid(); |
|
| 147 | - $values = array_map( |
|
| 148 | - function (AttributeValue $value) use ($cls, $oid) { |
|
| 149 | - $value = $cls::fromSelf($value); |
|
| 150 | - if ($value->oid() !== $oid) { |
|
| 151 | - throw new \LogicException('Attribute OID mismatch.'); |
|
| 152 | - } |
|
| 153 | - return $value; |
|
| 154 | - }, $this->_values); |
|
| 155 | - return self::fromAttributeValues(...$values); |
|
| 156 | - } |
|
| 124 | + /** |
|
| 125 | + * Cast attribute values to another AttributeValue class. |
|
| 126 | + * |
|
| 127 | + * This method is generally used to cast UnknownAttributeValue values |
|
| 128 | + * to specific objects when class is declared outside this package. |
|
| 129 | + * |
|
| 130 | + * The new class must be derived from AttributeValue and have the same OID |
|
| 131 | + * as current attribute values. |
|
| 132 | + * |
|
| 133 | + * @param string $cls AttributeValue class name |
|
| 134 | + * |
|
| 135 | + * @throws \LogicException |
|
| 136 | + * |
|
| 137 | + * @return self |
|
| 138 | + */ |
|
| 139 | + public function castValues(string $cls): self |
|
| 140 | + { |
|
| 141 | + $refl = new \ReflectionClass($cls); |
|
| 142 | + if (!$refl->isSubclassOf(AttributeValue::class)) { |
|
| 143 | + throw new \LogicException( |
|
| 144 | + "{$cls} must be derived from " . AttributeValue::class . '.'); |
|
| 145 | + } |
|
| 146 | + $oid = $this->oid(); |
|
| 147 | + $values = array_map( |
|
| 148 | + function (AttributeValue $value) use ($cls, $oid) { |
|
| 149 | + $value = $cls::fromSelf($value); |
|
| 150 | + if ($value->oid() !== $oid) { |
|
| 151 | + throw new \LogicException('Attribute OID mismatch.'); |
|
| 152 | + } |
|
| 153 | + return $value; |
|
| 154 | + }, $this->_values); |
|
| 155 | + return self::fromAttributeValues(...$values); |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * @see \Countable::count() |
|
| 160 | - * |
|
| 161 | - * @return int |
|
| 162 | - */ |
|
| 163 | - public function count(): int |
|
| 164 | - { |
|
| 165 | - return count($this->_values); |
|
| 166 | - } |
|
| 158 | + /** |
|
| 159 | + * @see \Countable::count() |
|
| 160 | + * |
|
| 161 | + * @return int |
|
| 162 | + */ |
|
| 163 | + public function count(): int |
|
| 164 | + { |
|
| 165 | + return count($this->_values); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @see \IteratorAggregate::getIterator() |
|
| 170 | - * |
|
| 171 | - * @return \ArrayIterator |
|
| 172 | - */ |
|
| 173 | - public function getIterator(): \ArrayIterator |
|
| 174 | - { |
|
| 175 | - return new \ArrayIterator($this->_values); |
|
| 176 | - } |
|
| 168 | + /** |
|
| 169 | + * @see \IteratorAggregate::getIterator() |
|
| 170 | + * |
|
| 171 | + * @return \ArrayIterator |
|
| 172 | + */ |
|
| 173 | + public function getIterator(): \ArrayIterator |
|
| 174 | + { |
|
| 175 | + return new \ArrayIterator($this->_values); |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\ASN1; |
| 6 | 6 | |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | { |
| 37 | 37 | // check that attribute values have correct oid |
| 38 | 38 | array_walk($values, |
| 39 | - function (AttributeValue $value) use ($type) { |
|
| 39 | + function(AttributeValue $value) use ($type) { |
|
| 40 | 40 | if ($value->oid() !== $type->oid()) { |
| 41 | 41 | throw new \LogicException('Attribute OID mismatch.'); |
| 42 | 42 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | { |
| 57 | 57 | $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
| 58 | 58 | $values = array_map( |
| 59 | - function (UnspecifiedType $el) use ($type) { |
|
| 59 | + function(UnspecifiedType $el) use ($type) { |
|
| 60 | 60 | return AttributeValue::fromASN1ByOID($type->oid(), $el); |
| 61 | 61 | }, $seq->at(1)->asSet()->elements()); |
| 62 | 62 | return new self($type, ...$values); |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | public function toASN1(): Sequence |
| 115 | 115 | { |
| 116 | 116 | $values = array_map( |
| 117 | - function (AttributeValue $value) { |
|
| 117 | + function(AttributeValue $value) { |
|
| 118 | 118 | return $value->toASN1(); |
| 119 | 119 | }, $this->_values); |
| 120 | 120 | $valueset = new Set(...$values); |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | } |
| 146 | 146 | $oid = $this->oid(); |
| 147 | 147 | $values = array_map( |
| 148 | - function (AttributeValue $value) use ($cls, $oid) { |
|
| 148 | + function(AttributeValue $value) use ($cls, $oid) { |
|
| 149 | 149 | $value = $cls::fromSelf($value); |
| 150 | 150 | if ($value->oid() !== $oid) { |
| 151 | 151 | throw new \LogicException('Attribute OID mismatch.'); |