| 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 LogicException; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Set; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 15 |
use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue; |
| 16 |
use function sprintf; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class Attribute implements Countable, IteratorAggregate |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private readonly AttributeType $type; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private readonly array $values; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private function __construct(AttributeType $type, AttributeValue ...$values) |
| 42 |
{ |
| 43 |
|
| 44 |
foreach ($values as $value) { |
| 45 |
if ($value->oid() !== $type->oid()) { |
| 46 |
throw new LogicException('Attribute OID mismatch.'); |
| 47 |
} |
| 48 |
} |
| 49 |
$this->type = $type; |
| 50 |
$this->values = $values; |
| 51 |
} |
| 52 |
|
| 53 |
public static function create(AttributeType $type, AttributeValue ...$values): self |
| 54 |
{ |
| 55 |
return new self($type, ...$values); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static function fromASN1(Sequence $seq): self |
| 62 |
{ |
| 63 |
$type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
| 64 |
$values = array_map( |
| 65 |
static fn (UnspecifiedType $el) => AttributeValue::fromASN1ByOID($type->oid(), $el), |
| 66 |
$seq->at(1) |
| 67 |
->asSet() |
| 68 |
->elements() |
| 69 |
); |
| 70 |
return self::create($type, ...$values); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static function fromAttributeValues(AttributeValue ...$values): self |
| 79 |
{ |
| 80 |
|
| 81 |
if (count($values) === 0) { |
| 82 |
throw new LogicException('No values.'); |
| 83 |
} |
| 84 |
$oid = reset($values) |
| 85 |
->oid(); |
| 86 |
return self::create(AttributeType::create($oid), ...$values); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function first(): AttributeValue |
| 93 |
{ |
| 94 |
if (count($this->values) === 0) { |
| 95 |
throw new LogicException('Attribute contains no values.'); |
| 96 |
} |
| 97 |
return $this->values[0]; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function values(): array |
| 106 |
{ |
| 107 |
return $this->values; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function toASN1(): Sequence |
| 114 |
{ |
| 115 |
$values = array_map(static fn (AttributeValue $value) => $value->toASN1(), $this->values); |
| 116 |
$valueset = Set::create(...$values); |
| 117 |
return Sequence::create($this->type->toASN1(), $valueset->sortedSetOf()); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function castValues(string $cls): self |
| 131 |
{ |
| 132 |
|
| 133 |
if (! is_subclass_of($cls, AttributeValue::class)) { |
| 134 |
throw new LogicException(sprintf('%s must be derived from %s.', $cls, AttributeValue::class)); |
| 135 |
} |
| 136 |
$values = array_map( |
| 137 |
function (AttributeValue $value) use ($cls) { |
| 138 |
|
| 139 |
$value = $cls::fromSelf($value); |
| 140 |
if ($value->oid() !== $this->oid()) { |
| 141 |
throw new LogicException('Attribute OID mismatch.'); |
| 142 |
} |
| 143 |
return $value; |
| 144 |
}, |
| 145 |
$this->values |
| 146 |
); |
| 147 |
return self::fromAttributeValues(...$values); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function count(): int |
| 154 |
{ |
| 155 |
return count($this->values); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function getIterator(): ArrayIterator |
| 162 |
{ |
| 163 |
return new ArrayIterator($this->values); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function type(): AttributeType |
| 170 |
{ |
| 171 |
return $this->type; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function oid(): string |
| 178 |
{ |
| 179 |
return $this->type->oid(); |
| 180 |
} |
| 181 |
} |