<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\X501\ASN1\AttributeValue; use SpomkyLabs\Pki\ASN1\Element; use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; use SpomkyLabs\Pki\X501\ASN1\Attribute; use SpomkyLabs\Pki\X501\ASN1\AttributeType; use SpomkyLabs\Pki\X501\ASN1\AttributeTypeAndValue; use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule; use Stringable; use function array_key_exists; abstract class AttributeValue implements Stringable { private const MAP_OID_TO_CLASS = [ AttributeType::OID_COMMON_NAME => CommonNameValue::class, AttributeType::OID_SURNAME => SurnameValue::class, AttributeType::OID_SERIAL_NUMBER => SerialNumberValue::class, AttributeType::OID_COUNTRY_NAME => CountryNameValue::class, AttributeType::OID_LOCALITY_NAME => LocalityNameValue::class, AttributeType::OID_STATE_OR_PROVINCE_NAME => StateOrProvinceNameValue::class, AttributeType::OID_ORGANIZATION_NAME => OrganizationNameValue::class, AttributeType::OID_ORGANIZATIONAL_UNIT_NAME => OrganizationalUnitNameValue::class, AttributeType::OID_TITLE => TitleValue::class, AttributeType::OID_DESCRIPTION => DescriptionValue::class, AttributeType::OID_NAME => NameValue::class, AttributeType::OID_GIVEN_NAME => GivenNameValue::class, AttributeType::OID_PSEUDONYM => PseudonymValue::class, ]; protected function __construct( protected string $oid ) { } public function __toString(): string { return $this->_transcodedString(); } abstract public function toASN1(): Element; abstract public function stringValue(): string; abstract public function equalityMatchingRule(): MatchingRule; abstract public function rfc2253String(): string; abstract public static function fromASN1(UnspecifiedType $el): self; public static function fromASN1ByOID(string $oid, UnspecifiedType $el): self { if (! array_key_exists($oid, self::MAP_OID_TO_CLASS)) { return new UnknownAttributeValue($oid, $el->asElement()); } $cls = self::MAP_OID_TO_CLASS[$oid]; return $cls::fromASN1($el); } public static function fromSelf(self $obj): self { return static::fromASN1($obj->toASN1()->asUnspecified()); } public function oid(): string { return $this->oid; } public function toAttribute(): Attribute { return Attribute::fromAttributeValues($this); } public function toAttributeTypeAndValue(): AttributeTypeAndValue { return AttributeTypeAndValue::fromAttributeValue($this); } abstract protected function _transcodedString(): string; } 