| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X501\ASN1\Collection; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Structure; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 13 |
use SpomkyLabs\Pki\X501\ASN1\Attribute; |
| 14 |
use SpomkyLabs\Pki\X501\ASN1\AttributeType; |
| 15 |
use UnexpectedValueException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
abstract class AttributeCollection implements Countable, IteratorAggregate |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected array $_attributes; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
final private function __construct(Attribute ...$attribs) |
| 37 |
{ |
| 38 |
$this->_attributes = $attribs; |
| 39 |
} |
| 40 |
|
| 41 |
public static function create(Attribute ...$attribs): static |
| 42 |
{ |
| 43 |
return new static(...$attribs); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function has(string $name): bool |
| 52 |
{ |
| 53 |
return $this->_findFirst($name) !== null; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function firstOf(string $name): Attribute |
| 62 |
{ |
| 63 |
$attr = $this->_findFirst($name); |
| 64 |
if ($attr === null) { |
| 65 |
throw new UnexpectedValueException("No {$name} attribute."); |
| 66 |
} |
| 67 |
return $attr; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function allOf(string $name): array |
| 78 |
{ |
| 79 |
$oid = AttributeType::attrNameToOID($name); |
| 80 |
return array_values(array_filter($this->_attributes, fn (Attribute $attr) => $attr->oid() === $oid)); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function all(): array |
| 89 |
{ |
| 90 |
return $this->_attributes; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function withAdditional(Attribute ...$attribs): self |
| 99 |
{ |
| 100 |
$obj = clone $this; |
| 101 |
foreach ($attribs as $attr) { |
| 102 |
$obj->_attributes[] = $attr; |
| 103 |
} |
| 104 |
return $obj; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function withUnique(Attribute $attr): static |
| 115 |
{ |
| 116 |
$attribs = array_values(array_filter($this->_attributes, fn (Attribute $a) => $a->oid() !== $attr->oid())); |
| 117 |
$attribs[] = $attr; |
| 118 |
$obj = clone $this; |
| 119 |
$obj->_attributes = $attribs; |
| 120 |
return $obj; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function count(): int |
| 129 |
{ |
| 130 |
return count($this->_attributes); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getIterator(): ArrayIterator |
| 140 |
{ |
| 141 |
return new ArrayIterator($this->_attributes); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
protected function _findFirst(string $name): ?Attribute |
| 150 |
{ |
| 151 |
$oid = AttributeType::attrNameToOID($name); |
| 152 |
foreach ($this->_attributes as $attr) { |
| 153 |
if ($attr->oid() === $oid) { |
| 154 |
return $attr; |
| 155 |
} |
| 156 |
} |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected static function _fromASN1Structure(Structure $struct): static |
| 166 |
{ |
| 167 |
return static::create(...array_map( |
| 168 |
static fn (UnspecifiedType $el) => static::_castAttributeValues( |
| 169 |
Attribute::fromASN1($el->asSequence()) |
| 170 |
), |
| 171 |
$struct->elements() |
| 172 |
)); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
protected static function _castAttributeValues(Attribute $attribute): Attribute |
| 183 |
{ |
| 184 |
|
| 185 |
return $attribute; |
| 186 |
} |
| 187 |
} |