@@ -15,114 +15,114 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class AttributeTypeAndValue |
| 17 | 17 | { |
| 18 | - use TypedAttribute; |
|
| 18 | + use TypedAttribute; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Attribute value. |
|
| 22 | - * |
|
| 23 | - * @var AttributeValue |
|
| 24 | - */ |
|
| 25 | - protected $_value; |
|
| 20 | + /** |
|
| 21 | + * Attribute value. |
|
| 22 | + * |
|
| 23 | + * @var AttributeValue |
|
| 24 | + */ |
|
| 25 | + protected $_value; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param AttributeType $type Attribute type |
|
| 31 | - * @param AttributeValue $value Attribute value |
|
| 32 | - */ |
|
| 33 | - public function __construct(AttributeType $type, AttributeValue $value) |
|
| 34 | - { |
|
| 35 | - $this->_type = $type; |
|
| 36 | - $this->_value = $value; |
|
| 37 | - } |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param AttributeType $type Attribute type |
|
| 31 | + * @param AttributeValue $value Attribute value |
|
| 32 | + */ |
|
| 33 | + public function __construct(AttributeType $type, AttributeValue $value) |
|
| 34 | + { |
|
| 35 | + $this->_type = $type; |
|
| 36 | + $this->_value = $value; |
|
| 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 | - * Initialize from ASN.1. |
|
| 49 | - * |
|
| 50 | - * @param Sequence $seq |
|
| 51 | - * |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public static function fromASN1(Sequence $seq): self |
|
| 55 | - { |
|
| 56 | - $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
|
| 57 | - $value = AttributeValue::fromASN1ByOID($type->oid(), $seq->at(1)); |
|
| 58 | - return new self($type, $value); |
|
| 59 | - } |
|
| 47 | + /** |
|
| 48 | + * Initialize from ASN.1. |
|
| 49 | + * |
|
| 50 | + * @param Sequence $seq |
|
| 51 | + * |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public static function fromASN1(Sequence $seq): self |
|
| 55 | + { |
|
| 56 | + $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
|
| 57 | + $value = AttributeValue::fromASN1ByOID($type->oid(), $seq->at(1)); |
|
| 58 | + return new self($type, $value); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Convenience method to initialize from attribute value. |
|
| 63 | - * |
|
| 64 | - * @param AttributeValue $value Attribute value |
|
| 65 | - * |
|
| 66 | - * @return self |
|
| 67 | - */ |
|
| 68 | - public static function fromAttributeValue(AttributeValue $value): self |
|
| 69 | - { |
|
| 70 | - return new self(new AttributeType($value->oid()), $value); |
|
| 71 | - } |
|
| 61 | + /** |
|
| 62 | + * Convenience method to initialize from attribute value. |
|
| 63 | + * |
|
| 64 | + * @param AttributeValue $value Attribute value |
|
| 65 | + * |
|
| 66 | + * @return self |
|
| 67 | + */ |
|
| 68 | + public static function fromAttributeValue(AttributeValue $value): self |
|
| 69 | + { |
|
| 70 | + return new self(new AttributeType($value->oid()), $value); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get attribute value. |
|
| 75 | - * |
|
| 76 | - * @return AttributeValue |
|
| 77 | - */ |
|
| 78 | - public function value(): AttributeValue |
|
| 79 | - { |
|
| 80 | - return $this->_value; |
|
| 81 | - } |
|
| 73 | + /** |
|
| 74 | + * Get attribute value. |
|
| 75 | + * |
|
| 76 | + * @return AttributeValue |
|
| 77 | + */ |
|
| 78 | + public function value(): AttributeValue |
|
| 79 | + { |
|
| 80 | + return $this->_value; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Generate ASN.1 structure. |
|
| 85 | - * |
|
| 86 | - * @return Sequence |
|
| 87 | - */ |
|
| 88 | - public function toASN1(): Sequence |
|
| 89 | - { |
|
| 90 | - return new Sequence($this->_type->toASN1(), $this->_value->toASN1()); |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Generate ASN.1 structure. |
|
| 85 | + * |
|
| 86 | + * @return Sequence |
|
| 87 | + */ |
|
| 88 | + public function toASN1(): Sequence |
|
| 89 | + { |
|
| 90 | + return new Sequence($this->_type->toASN1(), $this->_value->toASN1()); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get attributeTypeAndValue string conforming to RFC 2253. |
|
| 95 | - * |
|
| 96 | - * @see https://tools.ietf.org/html/rfc2253#section-2.3 |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function toString(): string |
|
| 101 | - { |
|
| 102 | - return $this->_type->typeName() . '=' . $this->_value->rfc2253String(); |
|
| 103 | - } |
|
| 93 | + /** |
|
| 94 | + * Get attributeTypeAndValue string conforming to RFC 2253. |
|
| 95 | + * |
|
| 96 | + * @see https://tools.ietf.org/html/rfc2253#section-2.3 |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function toString(): string |
|
| 101 | + { |
|
| 102 | + return $this->_type->typeName() . '=' . $this->_value->rfc2253String(); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Check whether attribute is semantically equal to other. |
|
| 107 | - * |
|
| 108 | - * @param AttributeTypeAndValue $other Object to compare to |
|
| 109 | - * |
|
| 110 | - * @return bool |
|
| 111 | - */ |
|
| 112 | - public function equals(AttributeTypeAndValue $other): bool |
|
| 113 | - { |
|
| 114 | - // check that attribute types match |
|
| 115 | - if ($this->oid() !== $other->oid()) { |
|
| 116 | - return false; |
|
| 117 | - } |
|
| 118 | - $matcher = $this->_value->equalityMatchingRule(); |
|
| 119 | - $result = $matcher->compare($this->_value->stringValue(), |
|
| 120 | - $other->_value->stringValue()); |
|
| 121 | - // match |
|
| 122 | - if ($result) { |
|
| 123 | - return true; |
|
| 124 | - } |
|
| 125 | - // no match or Undefined |
|
| 126 | - return false; |
|
| 127 | - } |
|
| 105 | + /** |
|
| 106 | + * Check whether attribute is semantically equal to other. |
|
| 107 | + * |
|
| 108 | + * @param AttributeTypeAndValue $other Object to compare to |
|
| 109 | + * |
|
| 110 | + * @return bool |
|
| 111 | + */ |
|
| 112 | + public function equals(AttributeTypeAndValue $other): bool |
|
| 113 | + { |
|
| 114 | + // check that attribute types match |
|
| 115 | + if ($this->oid() !== $other->oid()) { |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 118 | + $matcher = $this->_value->equalityMatchingRule(); |
|
| 119 | + $result = $matcher->compare($this->_value->stringValue(), |
|
| 120 | + $other->_value->stringValue()); |
|
| 121 | + // match |
|
| 122 | + if ($result) { |
|
| 123 | + return true; |
|
| 124 | + } |
|
| 125 | + // no match or Undefined |
|
| 126 | + return false; |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -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 array_sum( |
|
| 212 | - array_map( |
|
| 213 | - function (RDN $rdn) use ($oid) { |
|
| 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 array_sum( |
|
| 212 | + array_map( |
|
| 213 | + function (RDN $rdn) use ($oid) { |
|
| 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 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class LocalityNameValue extends DirectoryString |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $value String value |
|
| 21 | - * @param int $string_tag Syntax choice |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $value, |
|
| 24 | - int $string_tag = DirectoryString::UTF8) |
|
| 25 | - { |
|
| 26 | - $this->_oid = AttributeType::OID_LOCALITY_NAME; |
|
| 27 | - parent::__construct($value, $string_tag); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $value String value |
|
| 21 | + * @param int $string_tag Syntax choice |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $value, |
|
| 24 | + int $string_tag = DirectoryString::UTF8) |
|
| 25 | + { |
|
| 26 | + $this->_oid = AttributeType::OID_LOCALITY_NAME; |
|
| 27 | + parent::__construct($value, $string_tag); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class NameValue extends DirectoryString |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $value String value |
|
| 21 | - * @param int $string_tag Syntax choice |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $value, |
|
| 24 | - int $string_tag = DirectoryString::UTF8) |
|
| 25 | - { |
|
| 26 | - $this->_oid = AttributeType::OID_NAME; |
|
| 27 | - parent::__construct($value, $string_tag); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $value String value |
|
| 21 | + * @param int $string_tag Syntax choice |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $value, |
|
| 24 | + int $string_tag = DirectoryString::UTF8) |
|
| 25 | + { |
|
| 26 | + $this->_oid = AttributeType::OID_NAME; |
|
| 27 | + parent::__construct($value, $string_tag); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class OrganizationNameValue extends DirectoryString |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $value String value |
|
| 21 | - * @param int $string_tag Syntax choice |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $value, |
|
| 24 | - int $string_tag = DirectoryString::UTF8) |
|
| 25 | - { |
|
| 26 | - $this->_oid = AttributeType::OID_ORGANIZATION_NAME; |
|
| 27 | - parent::__construct($value, $string_tag); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $value String value |
|
| 21 | + * @param int $string_tag Syntax choice |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $value, |
|
| 24 | + int $string_tag = DirectoryString::UTF8) |
|
| 25 | + { |
|
| 26 | + $this->_oid = AttributeType::OID_ORGANIZATION_NAME; |
|
| 27 | + parent::__construct($value, $string_tag); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class DescriptionValue extends DirectoryString |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $value String value |
|
| 21 | - * @param int $string_tag Syntax choice |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $value, |
|
| 24 | - int $string_tag = DirectoryString::UTF8) |
|
| 25 | - { |
|
| 26 | - $this->_oid = AttributeType::OID_DESCRIPTION; |
|
| 27 | - parent::__construct($value, $string_tag); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $value String value |
|
| 21 | + * @param int $string_tag Syntax choice |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $value, |
|
| 24 | + int $string_tag = DirectoryString::UTF8) |
|
| 25 | + { |
|
| 26 | + $this->_oid = AttributeType::OID_DESCRIPTION; |
|
| 27 | + parent::__construct($value, $string_tag); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class GivenNameValue extends DirectoryString |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $value String value |
|
| 21 | - * @param int $string_tag Syntax choice |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $value, |
|
| 24 | - int $string_tag = DirectoryString::UTF8) |
|
| 25 | - { |
|
| 26 | - $this->_oid = AttributeType::OID_GIVEN_NAME; |
|
| 27 | - parent::__construct($value, $string_tag); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $value String value |
|
| 21 | + * @param int $string_tag Syntax choice |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $value, |
|
| 24 | + int $string_tag = DirectoryString::UTF8) |
|
| 25 | + { |
|
| 26 | + $this->_oid = AttributeType::OID_GIVEN_NAME; |
|
| 27 | + parent::__construct($value, $string_tag); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class OrganizationalUnitNameValue extends DirectoryString |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $value String value |
|
| 21 | - * @param int $string_tag Syntax choice |
|
| 22 | - */ |
|
| 23 | - public function __construct(string $value, |
|
| 24 | - int $string_tag = DirectoryString::UTF8) |
|
| 25 | - { |
|
| 26 | - $this->_oid = AttributeType::OID_ORGANIZATIONAL_UNIT_NAME; |
|
| 27 | - parent::__construct($value, $string_tag); |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $value String value |
|
| 21 | + * @param int $string_tag Syntax choice |
|
| 22 | + */ |
|
| 23 | + public function __construct(string $value, |
|
| 24 | + int $string_tag = DirectoryString::UTF8) |
|
| 25 | + { |
|
| 26 | + $this->_oid = AttributeType::OID_ORGANIZATIONAL_UNIT_NAME; |
|
| 27 | + parent::__construct($value, $string_tag); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -18,157 +18,157 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class AttributeValue |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Mapping from attribute type OID to attribute value class name. |
|
| 23 | - * |
|
| 24 | - * @internal |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - const MAP_OID_TO_CLASS = [ |
|
| 29 | - AttributeType::OID_COMMON_NAME => CommonNameValue::class, |
|
| 30 | - AttributeType::OID_SURNAME => SurnameValue::class, |
|
| 31 | - AttributeType::OID_SERIAL_NUMBER => SerialNumberValue::class, |
|
| 32 | - AttributeType::OID_COUNTRY_NAME => CountryNameValue::class, |
|
| 33 | - AttributeType::OID_LOCALITY_NAME => LocalityNameValue::class, |
|
| 34 | - AttributeType::OID_STATE_OR_PROVINCE_NAME => StateOrProvinceNameValue::class, |
|
| 35 | - AttributeType::OID_ORGANIZATION_NAME => OrganizationNameValue::class, |
|
| 36 | - AttributeType::OID_ORGANIZATIONAL_UNIT_NAME => OrganizationalUnitNameValue::class, |
|
| 37 | - AttributeType::OID_TITLE => TitleValue::class, |
|
| 38 | - AttributeType::OID_DESCRIPTION => DescriptionValue::class, |
|
| 39 | - AttributeType::OID_NAME => NameValue::class, |
|
| 40 | - AttributeType::OID_GIVEN_NAME => GivenNameValue::class, |
|
| 41 | - AttributeType::OID_PSEUDONYM => PseudonymValue::class, |
|
| 42 | - ]; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * OID of the attribute type. |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - protected $_oid; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Get attribute value as an UTF-8 encoded string. |
|
| 53 | - * |
|
| 54 | - * @return string |
|
| 55 | - */ |
|
| 56 | - public function __toString() |
|
| 57 | - { |
|
| 58 | - return $this->_transcodedString(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Generate ASN.1 element. |
|
| 63 | - * |
|
| 64 | - * @return Element |
|
| 65 | - */ |
|
| 66 | - abstract public function toASN1(): Element; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get attribute value as a string. |
|
| 70 | - * |
|
| 71 | - * @return string |
|
| 72 | - */ |
|
| 73 | - abstract public function stringValue(): string; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get matching rule for equality comparison. |
|
| 77 | - * |
|
| 78 | - * @return MatchingRule |
|
| 79 | - */ |
|
| 80 | - abstract public function equalityMatchingRule(): MatchingRule; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get attribute value as a string conforming to RFC 2253. |
|
| 84 | - * |
|
| 85 | - * @see https://tools.ietf.org/html/rfc2253#section-2.4 |
|
| 86 | - * |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - abstract public function rfc2253String(): string; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Initialize from ASN.1. |
|
| 93 | - * |
|
| 94 | - * @param UnspecifiedType $el |
|
| 95 | - * |
|
| 96 | - * @return self |
|
| 97 | - */ |
|
| 98 | - public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 99 | - { |
|
| 100 | - throw new \BadMethodCallException( |
|
| 101 | - 'ASN.1 parsing must be implemented in a concrete class.'); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Initialize from ASN.1 with given OID hint. |
|
| 106 | - * |
|
| 107 | - * @param string $oid Attribute's OID |
|
| 108 | - * @param UnspecifiedType $el |
|
| 109 | - * |
|
| 110 | - * @return self |
|
| 111 | - */ |
|
| 112 | - public static function fromASN1ByOID(string $oid, UnspecifiedType $el): self |
|
| 113 | - { |
|
| 114 | - if (!array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
|
| 115 | - return new UnknownAttributeValue($oid, $el->asElement()); |
|
| 116 | - } |
|
| 117 | - $cls = self::MAP_OID_TO_CLASS[$oid]; |
|
| 118 | - return $cls::fromASN1($el); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Initialize from another AttributeValue. |
|
| 123 | - * |
|
| 124 | - * This method is generally used to cast UnknownAttributeValue to |
|
| 125 | - * specific object when class is declared outside this package. |
|
| 126 | - * |
|
| 127 | - * @param self $obj Instance of AttributeValue |
|
| 128 | - * |
|
| 129 | - * @return self |
|
| 130 | - */ |
|
| 131 | - public static function fromSelf(self $obj): self |
|
| 132 | - { |
|
| 133 | - return static::fromASN1($obj->toASN1()->asUnspecified()); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Get attribute type's OID. |
|
| 138 | - * |
|
| 139 | - * @return string |
|
| 140 | - */ |
|
| 141 | - public function oid(): string |
|
| 142 | - { |
|
| 143 | - return $this->_oid; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get Attribute object with this as a single value. |
|
| 148 | - * |
|
| 149 | - * @return Attribute |
|
| 150 | - */ |
|
| 151 | - public function toAttribute(): Attribute |
|
| 152 | - { |
|
| 153 | - return Attribute::fromAttributeValues($this); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Get AttributeTypeAndValue object with this as a value. |
|
| 158 | - * |
|
| 159 | - * @return AttributeTypeAndValue |
|
| 160 | - */ |
|
| 161 | - public function toAttributeTypeAndValue(): AttributeTypeAndValue |
|
| 162 | - { |
|
| 163 | - return AttributeTypeAndValue::fromAttributeValue($this); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get attribute value as an UTF-8 string conforming to RFC 4518. |
|
| 168 | - * |
|
| 169 | - * @see https://tools.ietf.org/html/rfc4518#section-2.1 |
|
| 170 | - * |
|
| 171 | - * @return string |
|
| 172 | - */ |
|
| 173 | - abstract protected function _transcodedString(): string; |
|
| 21 | + /** |
|
| 22 | + * Mapping from attribute type OID to attribute value class name. |
|
| 23 | + * |
|
| 24 | + * @internal |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + const MAP_OID_TO_CLASS = [ |
|
| 29 | + AttributeType::OID_COMMON_NAME => CommonNameValue::class, |
|
| 30 | + AttributeType::OID_SURNAME => SurnameValue::class, |
|
| 31 | + AttributeType::OID_SERIAL_NUMBER => SerialNumberValue::class, |
|
| 32 | + AttributeType::OID_COUNTRY_NAME => CountryNameValue::class, |
|
| 33 | + AttributeType::OID_LOCALITY_NAME => LocalityNameValue::class, |
|
| 34 | + AttributeType::OID_STATE_OR_PROVINCE_NAME => StateOrProvinceNameValue::class, |
|
| 35 | + AttributeType::OID_ORGANIZATION_NAME => OrganizationNameValue::class, |
|
| 36 | + AttributeType::OID_ORGANIZATIONAL_UNIT_NAME => OrganizationalUnitNameValue::class, |
|
| 37 | + AttributeType::OID_TITLE => TitleValue::class, |
|
| 38 | + AttributeType::OID_DESCRIPTION => DescriptionValue::class, |
|
| 39 | + AttributeType::OID_NAME => NameValue::class, |
|
| 40 | + AttributeType::OID_GIVEN_NAME => GivenNameValue::class, |
|
| 41 | + AttributeType::OID_PSEUDONYM => PseudonymValue::class, |
|
| 42 | + ]; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * OID of the attribute type. |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + protected $_oid; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Get attribute value as an UTF-8 encoded string. |
|
| 53 | + * |
|
| 54 | + * @return string |
|
| 55 | + */ |
|
| 56 | + public function __toString() |
|
| 57 | + { |
|
| 58 | + return $this->_transcodedString(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Generate ASN.1 element. |
|
| 63 | + * |
|
| 64 | + * @return Element |
|
| 65 | + */ |
|
| 66 | + abstract public function toASN1(): Element; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get attribute value as a string. |
|
| 70 | + * |
|
| 71 | + * @return string |
|
| 72 | + */ |
|
| 73 | + abstract public function stringValue(): string; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get matching rule for equality comparison. |
|
| 77 | + * |
|
| 78 | + * @return MatchingRule |
|
| 79 | + */ |
|
| 80 | + abstract public function equalityMatchingRule(): MatchingRule; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get attribute value as a string conforming to RFC 2253. |
|
| 84 | + * |
|
| 85 | + * @see https://tools.ietf.org/html/rfc2253#section-2.4 |
|
| 86 | + * |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + abstract public function rfc2253String(): string; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Initialize from ASN.1. |
|
| 93 | + * |
|
| 94 | + * @param UnspecifiedType $el |
|
| 95 | + * |
|
| 96 | + * @return self |
|
| 97 | + */ |
|
| 98 | + public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 99 | + { |
|
| 100 | + throw new \BadMethodCallException( |
|
| 101 | + 'ASN.1 parsing must be implemented in a concrete class.'); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Initialize from ASN.1 with given OID hint. |
|
| 106 | + * |
|
| 107 | + * @param string $oid Attribute's OID |
|
| 108 | + * @param UnspecifiedType $el |
|
| 109 | + * |
|
| 110 | + * @return self |
|
| 111 | + */ |
|
| 112 | + public static function fromASN1ByOID(string $oid, UnspecifiedType $el): self |
|
| 113 | + { |
|
| 114 | + if (!array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
|
| 115 | + return new UnknownAttributeValue($oid, $el->asElement()); |
|
| 116 | + } |
|
| 117 | + $cls = self::MAP_OID_TO_CLASS[$oid]; |
|
| 118 | + return $cls::fromASN1($el); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Initialize from another AttributeValue. |
|
| 123 | + * |
|
| 124 | + * This method is generally used to cast UnknownAttributeValue to |
|
| 125 | + * specific object when class is declared outside this package. |
|
| 126 | + * |
|
| 127 | + * @param self $obj Instance of AttributeValue |
|
| 128 | + * |
|
| 129 | + * @return self |
|
| 130 | + */ |
|
| 131 | + public static function fromSelf(self $obj): self |
|
| 132 | + { |
|
| 133 | + return static::fromASN1($obj->toASN1()->asUnspecified()); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Get attribute type's OID. |
|
| 138 | + * |
|
| 139 | + * @return string |
|
| 140 | + */ |
|
| 141 | + public function oid(): string |
|
| 142 | + { |
|
| 143 | + return $this->_oid; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get Attribute object with this as a single value. |
|
| 148 | + * |
|
| 149 | + * @return Attribute |
|
| 150 | + */ |
|
| 151 | + public function toAttribute(): Attribute |
|
| 152 | + { |
|
| 153 | + return Attribute::fromAttributeValues($this); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Get AttributeTypeAndValue object with this as a value. |
|
| 158 | + * |
|
| 159 | + * @return AttributeTypeAndValue |
|
| 160 | + */ |
|
| 161 | + public function toAttributeTypeAndValue(): AttributeTypeAndValue |
|
| 162 | + { |
|
| 163 | + return AttributeTypeAndValue::fromAttributeValue($this); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get attribute value as an UTF-8 string conforming to RFC 4518. |
|
| 168 | + * |
|
| 169 | + * @see https://tools.ietf.org/html/rfc4518#section-2.1 |
|
| 170 | + * |
|
| 171 | + * @return string |
|
| 172 | + */ |
|
| 173 | + abstract protected function _transcodedString(): string; |
|
| 174 | 174 | } |