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