| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X501\ASN1; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Set; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 13 |
use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue; |
| 14 |
use Stringable; |
| 15 |
use UnexpectedValueException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class RDN implements Countable, IteratorAggregate, Stringable |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private readonly array $_attribs; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private function __construct(AttributeTypeAndValue ...$attribs) |
| 35 |
{ |
| 36 |
if (count($attribs) === 0) { |
| 37 |
throw new UnexpectedValueException('RDN must have at least one AttributeTypeAndValue.'); |
| 38 |
} |
| 39 |
$this->_attribs = $attribs; |
| 40 |
} |
| 41 |
|
| 42 |
public function __toString(): string |
| 43 |
{ |
| 44 |
return $this->toString(); |
| 45 |
} |
| 46 |
|
| 47 |
public static function create(AttributeTypeAndValue ...$attribs): self |
| 48 |
{ |
| 49 |
return new self(...$attribs); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static function fromAttributeValues(AttributeValue ...$values): self |
| 58 |
{ |
| 59 |
$attribs = array_map( |
| 60 |
static fn (AttributeValue $value) => AttributeTypeAndValue::create(AttributeType::create( |
| 61 |
$value->oid() |
| 62 |
), $value), |
| 63 |
$values |
| 64 |
); |
| 65 |
return self::create(...$attribs); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static function fromASN1(Set $set): self |
| 72 |
{ |
| 73 |
$attribs = array_map( |
| 74 |
static fn (UnspecifiedType $el) => AttributeTypeAndValue::fromASN1($el->asSequence()), |
| 75 |
$set->elements() |
| 76 |
); |
| 77 |
return self::create(...$attribs); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function toASN1(): Set |
| 84 |
{ |
| 85 |
$elements = array_map(static fn (AttributeTypeAndValue $tv) => $tv->toASN1(), $this->_attribs); |
| 86 |
return Set::create(...$elements)->sortedSetOf(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function toString(): string |
| 95 |
{ |
| 96 |
$parts = array_map(static fn (AttributeTypeAndValue $tv) => $tv->toString(), $this->_attribs); |
| 97 |
return implode('+', $parts); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function equals(self $other): bool |
| 106 |
{ |
| 107 |
|
| 108 |
if (count($this) !== count($other)) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
$attribs1 = $this->_attribs; |
| 112 |
$attribs2 = $other->_attribs; |
| 113 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function all(): array |
| 134 |
{ |
| 135 |
return $this->_attribs; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function allOf(string $name): array |
| 146 |
{ |
| 147 |
$oid = AttributeType::attrNameToOID($name); |
| 148 |
$attribs = array_filter($this->_attribs, static fn (AttributeTypeAndValue $tv) => $tv->oid() === $oid); |
| 149 |
return array_values($attribs); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function count(): int |
| 156 |
{ |
| 157 |
return count($this->_attribs); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getIterator(): ArrayIterator |
| 164 |
{ |
| 165 |
return new ArrayIterator($this->_attribs); |
| 166 |
} |
| 167 |
} |