1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X501\ASN1; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
8
|
|
|
use Sop\X501\ASN1\AttributeValue\AttributeValue; |
9
|
|
|
use Sop\X501\ASN1\Feature\TypedAttribute; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implements *AttributeTypeAndValue* ASN.1 type. |
13
|
|
|
* |
14
|
|
|
* @see https://www.itu.int/ITU-T/formal-language/itu-t/x/x501/2012/InformationFramework.html#InformationFramework.AttributeTypeAndValue |
15
|
|
|
*/ |
16
|
|
|
class AttributeTypeAndValue |
17
|
|
|
{ |
18
|
|
|
use TypedAttribute; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Attribute value. |
22
|
|
|
* |
23
|
|
|
* @var AttributeValue |
24
|
|
|
*/ |
25
|
|
|
protected $_value; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor. |
29
|
|
|
* |
30
|
|
|
* @param AttributeType $type Attribute type |
31
|
|
|
* @param AttributeValue $value Attribute value |
32
|
|
|
*/ |
33
|
42 |
|
public function __construct(AttributeType $type, AttributeValue $value) |
34
|
|
|
{ |
35
|
42 |
|
$this->_type = $type; |
36
|
42 |
|
$this->_value = $value; |
37
|
42 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
1 |
|
public function __toString() |
43
|
|
|
{ |
44
|
1 |
|
return $this->toString(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize from ASN.1. |
49
|
|
|
* |
50
|
|
|
* @param Sequence $seq |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
5 |
|
public static function fromASN1(Sequence $seq): self |
55
|
|
|
{ |
56
|
5 |
|
$type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
57
|
5 |
|
$value = AttributeValue::fromASN1ByOID($type->oid(), $seq->at(1)); |
58
|
5 |
|
return new self($type, $value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Convenience method to initialize from attribute value. |
63
|
|
|
* |
64
|
|
|
* @param AttributeValue $value Attribute value |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
2 |
|
public static function fromAttributeValue(AttributeValue $value): self |
69
|
|
|
{ |
70
|
2 |
|
return new self(new AttributeType($value->oid()), $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get attribute value. |
75
|
|
|
* |
76
|
|
|
* @return AttributeValue |
77
|
|
|
*/ |
78
|
2 |
|
public function value(): AttributeValue |
79
|
|
|
{ |
80
|
2 |
|
return $this->_value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Generate ASN.1 structure. |
85
|
|
|
* |
86
|
|
|
* @return Sequence |
87
|
|
|
*/ |
88
|
5 |
|
public function toASN1(): Sequence |
89
|
|
|
{ |
90
|
5 |
|
return new Sequence($this->_type->toASN1(), $this->_value->toASN1()); |
91
|
|
|
} |
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
|
17 |
|
public function toString(): string |
101
|
|
|
{ |
102
|
17 |
|
return $this->_type->typeName() . '=' . $this->_value->rfc2253String(); |
103
|
|
|
} |
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
|
21 |
|
public function equals(AttributeTypeAndValue $other): bool |
113
|
|
|
{ |
114
|
|
|
// check that attribute types match |
115
|
21 |
|
if ($this->oid() !== $other->oid()) { |
116
|
2 |
|
return false; |
117
|
|
|
} |
118
|
20 |
|
$matcher = $this->_value->equalityMatchingRule(); |
119
|
20 |
|
$result = $matcher->compare($this->_value->stringValue(), |
120
|
20 |
|
$other->_value->stringValue()); |
121
|
|
|
// match |
122
|
20 |
|
if ($result) { |
123
|
16 |
|
return true; |
124
|
|
|
} |
125
|
|
|
// no match or Undefined |
126
|
6 |
|
return false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|