UserList::getXML()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 31
ccs 17
cts 17
cp 1
rs 8.4444
cc 8
nc 32
nop 0
crap 8
1
<?php
2
3
/**
4
 * This file is part of riesenia/pohoda package.
5
 *
6
 * Licensed under the MIT License
7
 * (c) RIESENIA.com
8
 */
9
10
declare(strict_types=1);
11
12
namespace Riesenia\Pohoda;
13
14
use Riesenia\Pohoda\Common\OptionsResolver;
15
use Riesenia\Pohoda\UserList\ItemUserCode;
16
17
/**
18
 * Definition of user-defined list
19
 *
20
 * @property array{
21
 *     code: string,
22
 *     name: string,
23
 *     constants?: bool,
24
 *     dateTimeStamp?: string|\DateTimeInterface,
25
 *     dateValidFrom?: string|\DateTimeInterface,
26
 *     submenu?: bool,
27
 *     itemUserCodes?: iterable<UserList\ItemUserCode>,
28
 * } $data
29
 */
30
class UserList extends AbstractAgenda
31
{
32 1
    public function getImportRoot(): string
33
    {
34 1
        return 'lst:listUserCode';
35
    }
36
37
    /**
38
     * Add item user code.
39
     *
40
     * @param array<string,mixed> $data
41
     *
42
     * @return $this
43
     */
44 2
    public function addItemUserCode(array $data): self
45
    {
46 2
        if (!isset($this->data['itemUserCodes'])
47 2
            || !(
48 2
                is_array($this->data['itemUserCodes'])
49 2
                || (is_a($this->data['itemUserCodes'], \ArrayAccess::class))
50 2
            )
51
        ) {
52 2
            $this->data['itemUserCodes'] = [];
53
        }
54
55 2
        $itemUserCodes = new UserList\ItemUserCode($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
56 2
        $itemUserCodes->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data);
57 2
        $this->data['itemUserCodes'][] = $itemUserCodes;
58
59 2
        return $this;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function getXML(): \SimpleXMLElement
66
    {
67 2
        $xml = $this->createXML()->addChild('lst:listUserCode', '', $this->namespace('lst'));
68 2
        $xml->addAttribute('version', '1.1');
69 2
        $xml->addAttribute('code', strval($this->data['code']));
70 2
        $xml->addAttribute('name', strval($this->data['name']));
71
72 2
        if (isset($this->data['dateTimeStamp'])) {
73 1
            $xml->addAttribute('dateTimeStamp', strval($this->data['dateTimeStamp']));
74
        }
75
76 2
        if (isset($this->data['dateValidFrom'])) {
77 1
            $xml->addAttribute('dateValidFrom', strval($this->data['dateValidFrom']));
78
        }
79
80 2
        if (isset($this->data['submenu'])) {
81 1
            $xml->addAttribute('submenu', strval($this->data['submenu']));
82
        }
83
84 2
        if (isset($this->data['constants']) && 'true' == $this->data['constants']) {
85 1
            $xml->addAttribute('constants', strval($this->data['constants']));
86
        }
87
88 2
        if (isset($this->data['itemUserCodes'])) {
89 2
            foreach ($this->data['itemUserCodes'] as $itemUserCode) {
90
                /** @var ItemUserCode $itemUserCode */
91 2
                $this->appendNode($xml, $itemUserCode->getXML());
92
            }
93
        }
94
95 2
        return $xml;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1
    protected function configureOptions(OptionsResolver $resolver): void
102
    {
103
        // available options
104 1
        $resolver->setDefined(['code', 'name', 'constants', 'dateTimeStamp', 'dateValidFrom', 'submenu']);
105
106
        // validate / format options
107 1
        $resolver->setRequired('code');
108 1
        $resolver->setRequired('name');
109 1
        $resolver->setNormalizer('constants', $this->normalizerFactory->getClosure('bool'));
110 1
        $resolver->setNormalizer('dateTimeStamp', $this->normalizerFactory->getClosure('datetime'));
111 1
        $resolver->setNormalizer('dateValidFrom', $this->normalizerFactory->getClosure('date'));
112 1
        $resolver->setNormalizer('submenu', $this->normalizerFactory->getClosure('boolean'));
113
    }
114
}
115