1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SimpleSAML\XMLSecurity\XML\xenc; |
||
6 | |||
7 | use DOMElement; |
||
8 | use SimpleSAML\Assert\Assert; |
||
9 | use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; |
||
10 | use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException}; |
||
11 | |||
12 | use function array_pop; |
||
13 | |||
14 | /** |
||
15 | * Class representing <xenc:CipherData>. |
||
16 | * |
||
17 | * @package simplesamlphp/xml-security |
||
18 | */ |
||
19 | class CipherData extends AbstractXencElement implements SchemaValidatableElementInterface |
||
20 | { |
||
21 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
22 | |||
23 | /** |
||
24 | * CipherData constructor. |
||
25 | * |
||
26 | * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherValue|null $cipherValue |
||
27 | * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherReference|null $cipherReference |
||
28 | */ |
||
29 | final public function __construct( |
||
30 | protected ?CipherValue $cipherValue, |
||
31 | protected ?CipherReference $cipherReference = null, |
||
32 | ) { |
||
33 | Assert::oneOf( |
||
34 | null, |
||
35 | [$cipherValue, $cipherReference], |
||
36 | 'Can only have one of CipherValue/CipherReference', |
||
37 | ); |
||
38 | |||
39 | Assert::false( |
||
40 | is_null($cipherValue) && is_null($cipherReference), |
||
41 | 'You need either a CipherValue or a CipherReference', |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Get the value of the $cipherValue property. |
||
48 | * |
||
49 | * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherValue|null |
||
50 | */ |
||
51 | public function getCipherValue(): ?CipherValue |
||
52 | { |
||
53 | return $this->cipherValue; |
||
54 | } |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Get the CipherReference element inside this CipherData object. |
||
59 | * |
||
60 | * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherReference|null |
||
61 | */ |
||
62 | public function getCipherReference(): ?CipherReference |
||
63 | { |
||
64 | return $this->cipherReference; |
||
65 | } |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | * |
||
71 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
72 | * If the qualified name of the supplied element is wrong |
||
73 | */ |
||
74 | public static function fromXML(DOMElement $xml): static |
||
75 | { |
||
76 | Assert::same($xml->localName, 'CipherData', InvalidDOMElementException::class); |
||
77 | Assert::same($xml->namespaceURI, CipherData::NS, InvalidDOMElementException::class); |
||
78 | |||
79 | $cv = CipherValue::getChildrenOfClass($xml); |
||
80 | Assert::maxCount( |
||
81 | $cv, |
||
82 | 1, |
||
83 | 'More than one CipherValue element in <xenc:CipherData>', |
||
84 | TooManyElementsException::class, |
||
85 | ); |
||
86 | |||
87 | $cr = CipherReference::getChildrenOfClass($xml); |
||
88 | Assert::maxCount( |
||
89 | $cr, |
||
90 | 1, |
||
91 | 'More than one CipherReference element in <xenc:CipherData>', |
||
92 | TooManyElementsException::class, |
||
93 | ); |
||
94 | |||
95 | return new static( |
||
96 | empty($cv) ? null : array_pop($cv), |
||
97 | empty($cr) ? null : array_pop($cr), |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | |||
102 | /** |
||
103 | * @inheritDoc |
||
104 | */ |
||
105 | public function toXML(?DOMElement $parent = null): DOMElement |
||
106 | { |
||
107 | $e = $this->instantiateParentElement($parent); |
||
108 | |||
109 | $this->getCipherValue()?->toXML($e); |
||
110 | $this->getCipherReference()?->toXML($e); |
||
111 | |||
112 | return $e; |
||
113 | } |
||
114 | } |
||
115 |