| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X501\ASN1\AttributeValue; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 10 |
use SpomkyLabs\Pki\X501\ASN1\Attribute; |
| 11 |
use SpomkyLabs\Pki\X501\ASN1\AttributeType; |
| 12 |
use SpomkyLabs\Pki\X501\ASN1\AttributeTypeAndValue; |
| 13 |
use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule; |
| 14 |
use Stringable; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
abstract class AttributeValue implements Stringable |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private const MAP_OID_TO_CLASS = [ |
| 31 |
AttributeType::OID_COMMON_NAME => CommonNameValue::class, |
| 32 |
AttributeType::OID_SURNAME => SurnameValue::class, |
| 33 |
AttributeType::OID_SERIAL_NUMBER => SerialNumberValue::class, |
| 34 |
AttributeType::OID_COUNTRY_NAME => CountryNameValue::class, |
| 35 |
AttributeType::OID_LOCALITY_NAME => LocalityNameValue::class, |
| 36 |
AttributeType::OID_STATE_OR_PROVINCE_NAME => StateOrProvinceNameValue::class, |
| 37 |
AttributeType::OID_ORGANIZATION_NAME => OrganizationNameValue::class, |
| 38 |
AttributeType::OID_ORGANIZATIONAL_UNIT_NAME => OrganizationalUnitNameValue::class, |
| 39 |
AttributeType::OID_TITLE => TitleValue::class, |
| 40 |
AttributeType::OID_DESCRIPTION => DescriptionValue::class, |
| 41 |
AttributeType::OID_NAME => NameValue::class, |
| 42 |
AttributeType::OID_GIVEN_NAME => GivenNameValue::class, |
| 43 |
AttributeType::OID_PSEUDONYM => PseudonymValue::class, |
| 44 |
]; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected function __construct( |
| 50 |
protected string $oid |
| 51 |
) { |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __toString(): string |
| 58 |
{ |
| 59 |
return $this->_transcodedString(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
abstract public function toASN1(): Element; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
abstract public function stringValue(): string; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
abstract public function equalityMatchingRule(): MatchingRule; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
abstract public function rfc2253String(): string; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
abstract public static function fromASN1(UnspecifiedType $el): self; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function fromASN1ByOID(string $oid, UnspecifiedType $el): self |
| 95 |
{ |
| 96 |
if (! array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
| 97 |
return new UnknownAttributeValue($oid, $el->asElement()); |
| 98 |
} |
| 99 |
$cls = self::MAP_OID_TO_CLASS[$oid]; |
| 100 |
return $cls::fromASN1($el); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static function fromSelf(self $obj): self |
| 112 |
{ |
| 113 |
return static::fromASN1($obj->toASN1()->asUnspecified()); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function oid(): string |
| 120 |
{ |
| 121 |
return $this->oid; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function toAttribute(): Attribute |
| 128 |
{ |
| 129 |
return Attribute::fromAttributeValues($this); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function toAttributeTypeAndValue(): AttributeTypeAndValue |
| 136 |
{ |
| 137 |
return AttributeTypeAndValue::fromAttributeValue($this); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
abstract protected function _transcodedString(): string; |
| 146 |
} |