Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/ASN1/AttributeValue/UnknownAttributeValue.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X501\ASN1\AttributeValue;
6
7 use BadMethodCallException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
10 use SpomkyLabs\Pki\X501\DN\DNParser;
11 use SpomkyLabs\Pki\X501\MatchingRule\BinaryMatch;
12 use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule;
13 use SpomkyLabs\Pki\X501\StringPrep\TranscodeStep;
14
15 /**
16 * Class to hold ASN.1 structure of an unimplemented attribute value.
17 */
18 final class UnknownAttributeValue extends AttributeValue
19 {
20 /**
21 * @param Element $_element ASN.1 element.
22 */
23 protected function __construct(
24 string $oid,
25 protected Element $_element
26 ) {
27 parent::__construct($oid);
28 $this->oid = $oid;
29 }
30
31 public static function create(string $oid, Element $_element): self
32 {
33 return new self($oid, $_element);
34 }
35
36 public function toASN1(): Element
37 {
38 return $this->_element;
39 }
40
41 public function stringValue(): string
42 {
43 // if value is encoded as a string type
44 if ($this->_element->isType(Element::TYPE_STRING)) {
45 return $this->_element->asUnspecified()
46 ->asString()
47 ->string();
48 }
49 // return DER encoding as a hexstring (see RFC2253 section 2.4)
50 return '#' . bin2hex($this->_element->toDER());
51 }
52
53 public function equalityMatchingRule(): MatchingRule
54 {
55 return new BinaryMatch();
56 }
57
58 public function rfc2253String(): string
59 {
60 $str = $this->_transcodedString();
61 // if value has a string representation
62 if ($this->_element->isType(Element::TYPE_STRING)) {
63 $str = DNParser::escapeString($str);
64 }
65 return $str;
66 }
67
68 public static function fromASN1(UnspecifiedType $el): AttributeValue
69 {
70 throw new BadMethodCallException('ASN.1 parsing must be implemented in a concrete class.');
71 }
72
73 protected function _transcodedString(): string
74 {
75 // if transcoding is defined for the value type
76 if (TranscodeStep::isTypeSupported($this->_element->tag())) {
77 $step = TranscodeStep::create($this->_element->tag());
78 return $step->apply($this->stringValue());
79 }
80 return $this->stringValue();
81 }
82 }