<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\X509\AttributeCertificate; use SpomkyLabs\Pki\X501\ASN1\Attribute; use SpomkyLabs\Pki\X501\ASN1\AttributeType; use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue; use SpomkyLabs\Pki\X501\ASN1\Collection\SequenceOfAttributes; use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\AccessIdentityAttributeValue; use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\AuthenticationInfoAttributeValue; use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\ChargingIdentityAttributeValue; use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\GroupAttributeValue; use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\RoleAttributeValue; final class Attributes extends SequenceOfAttributes { private const MAP_OID_TO_CLASS = [ AccessIdentityAttributeValue::OID => AccessIdentityAttributeValue::class, AuthenticationInfoAttributeValue::OID => AuthenticationInfoAttributeValue::class, ChargingIdentityAttributeValue::OID => ChargingIdentityAttributeValue::class, GroupAttributeValue::OID => GroupAttributeValue::class, AttributeType::OID_ROLE => RoleAttributeValue::class, ]; public static function fromAttributeValues(AttributeValue ...$values): static { return static::create(...array_map(static fn (AttributeValue $value) => $value->toAttribute(), $values)); } public function hasAccessIdentity(): bool { return $this->has(AccessIdentityAttributeValue::OID); } public function accessIdentity(): AccessIdentityAttributeValue { return $this->firstOf(AccessIdentityAttributeValue::OID)->first(); } public function hasAuthenticationInformation(): bool { return $this->has(AuthenticationInfoAttributeValue::OID); } public function authenticationInformation(): AuthenticationInfoAttributeValue { return $this->firstOf(AuthenticationInfoAttributeValue::OID)->first(); } public function hasChargingIdentity(): bool { return $this->has(ChargingIdentityAttributeValue::OID); } public function chargingIdentity(): ChargingIdentityAttributeValue { return $this->firstOf(ChargingIdentityAttributeValue::OID)->first(); } public function hasGroup(): bool { return $this->has(GroupAttributeValue::OID); } public function group(): GroupAttributeValue { return $this->firstOf(GroupAttributeValue::OID)->first(); } public function hasRole(): bool { return $this->has(AttributeType::OID_ROLE); } public function role(): RoleAttributeValue { return $this->firstOf(AttributeType::OID_ROLE)->first(); } public function roles(): array { return array_merge( [], ...array_map(static fn (Attribute $attr) => $attr->values(), $this->allOf(AttributeType::OID_ROLE)) ); } protected static function _castAttributeValues(Attribute $attribute): Attribute { $oid = $attribute->oid(); if (isset(self::MAP_OID_TO_CLASS[$oid])) { return $attribute->castValues(self::MAP_OID_TO_CLASS[$oid]); } return $attribute; } } 