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