Passed
Pull Request — master (#225)
by Jaime Pérez
02:23
created

CipherData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setCipherValue() 0 4 1
A __construct() 0 3 1
A fromXML() 0 10 1
A toXML() 0 9 1
A getCipherValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\xenc;
6
7
use DOMElement;
8
use SAML2\Utils;
9
use Webmozart\Assert\Assert;
10
11
/**
12
 * Class representing <xenc:CipherData>.
13
 *
14
 * @package simplesamlphp/saml2
15
 */
16
class CipherData extends AbstractXencElement
17
{
18
    /** @var string */
19
    protected $cipherValue;
20
21
22
    /**
23
     * CipherData constructor.
24
     *
25
     * @param string $cipherValue
26
     */
27
    public function __construct(string $cipherValue)
28
    {
29
        $this->setCipherValue($cipherValue);
30
    }
31
32
33
    /**
34
     * Get the string value of the <xenc:CipherValue> element inside this CipherData object.
35
     *
36
     * @return string
37
     */
38
    public function getCipherValue(): string
39
    {
40
        return $this->cipherValue;
41
    }
42
43
44
    /**
45
     * @param string $cipherValue
46
     */
47
    protected function setCipherValue(string $cipherValue)
48
    {
49
        Assert::regex($cipherValue, '/[a-zA-Z0-9_\-=\+\/]/', 'Invalid data in <xenc:CipherValue>.');
50
        $this->cipherValue = $cipherValue;
51
    }
52
53
54
    /**
55
     * @inheritDoc
56
     * @throws \Exception
57
     */
58
    public static function fromXML(DOMElement $xml): object
59
    {
60
        Assert::same($xml->localName, 'CipherData');
61
        Assert::same($xml->namespaceURI, CipherData::NS);
62
63
        $cv = Utils::xpQuery($xml, './xenc:CipherValue');
64
        Assert::notEmpty($cv, 'Missing CipherValue element in <xenc:CipherData>');
65
        Assert::count($cv, 1, 'More than one CipherValue element in <xenc:CipherData');
66
67
        return new self($cv[0]->textContent);
68
    }
69
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function toXML(DOMElement $parent = null): DOMElement
75
    {
76
        $e = $this->instantiateParentElement($parent);
77
78
        $cv = $e->ownerDocument->createElementNS($this::NS, $this::NS_PREFIX . ':CipherValue');
79
        $cv->textContent = $this->cipherValue;
80
        $e->appendChild($cv);
81
82
        return $e;
83
    }
84
}
85