| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\CertificationRequest; |
| 6 |
|
| 7 |
use LogicException; |
| 8 |
use SpomkyLabs\Pki\X501\ASN1\Attribute; |
| 9 |
use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue; |
| 10 |
use SpomkyLabs\Pki\X501\ASN1\Collection\SetOfAttributes; |
| 11 |
use SpomkyLabs\Pki\X509\CertificationRequest\Attribute\ExtensionRequestValue; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
final class Attributes extends SetOfAttributes |
| 19 |
{ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private const MAP_OID_TO_CLASS = [ |
| 28 |
ExtensionRequestValue::OID => ExtensionRequestValue::class, |
| 29 |
]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static function fromAttributeValues(AttributeValue ...$values): static |
| 37 |
{ |
| 38 |
return static::create(...array_map(static fn (AttributeValue $value) => $value->toAttribute(), $values)); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function hasExtensionRequest(): bool |
| 45 |
{ |
| 46 |
return $this->has(ExtensionRequestValue::OID); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function extensionRequest(): ExtensionRequestValue |
| 53 |
{ |
| 54 |
if (! $this->hasExtensionRequest()) { |
| 55 |
throw new LogicException('No extension request attribute.'); |
| 56 |
} |
| 57 |
return $this->firstOf(ExtensionRequestValue::OID)->first(); |
| 58 |
} |
| 59 |
|
| 60 |
protected static function _castAttributeValues(Attribute $attribute): Attribute |
| 61 |
{ |
| 62 |
$oid = $attribute->oid(); |
| 63 |
if (isset(self::MAP_OID_TO_CLASS[$oid])) { |
| 64 |
return $attribute->castValues(self::MAP_OID_TO_CLASS[$oid]); |
| 65 |
} |
| 66 |
return $attribute; |
| 67 |
} |
| 68 |
} |