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->dependenciesFactory);
56 2
        $itemUserCodes
57 2
            ->setDirectionalVariable($this->useOneDirectionalVariables)
58 2
            ->setResolveOptions($this->resolveOptions)
59 2
            ->setData($data);
60 2
        $this->data['itemUserCodes'][] = $itemUserCodes;
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function getXML(): \SimpleXMLElement
69
    {
70 2
        $xml = $this->createXML()->addChild('lst:listUserCode', '', $this->namespace('lst'));
71 2
        $xml->addAttribute('version', '1.1');
72 2
        $xml->addAttribute('code', strval($this->data['code']));
73 2
        $xml->addAttribute('name', strval($this->data['name']));
74
75 2
        if (isset($this->data['dateTimeStamp'])) {
76 1
            $xml->addAttribute('dateTimeStamp', strval($this->data['dateTimeStamp']));
77
        }
78
79 2
        if (isset($this->data['dateValidFrom'])) {
80 1
            $xml->addAttribute('dateValidFrom', strval($this->data['dateValidFrom']));
81
        }
82
83 2
        if (isset($this->data['submenu'])) {
84 1
            $xml->addAttribute('submenu', strval($this->data['submenu']));
85
        }
86
87 2
        if (isset($this->data['constants']) && 'true' == $this->data['constants']) {
88 1
            $xml->addAttribute('constants', strval($this->data['constants']));
89
        }
90
91 2
        if (isset($this->data['itemUserCodes'])) {
92 2
            foreach ($this->data['itemUserCodes'] as $itemUserCode) {
93
                /** @var ItemUserCode $itemUserCode */
94 2
                $this->appendNode($xml, $itemUserCode->getXML());
95
            }
96
        }
97
98 2
        return $xml;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    protected function configureOptions(OptionsResolver $resolver): void
105
    {
106
        // available options
107 1
        $resolver->setDefined(['code', 'name', 'constants', 'dateTimeStamp', 'dateValidFrom', 'submenu']);
108
109
        // validate / format options
110 1
        $resolver->setRequired('code');
111 1
        $resolver->setRequired('name');
112 1
        $resolver->setNormalizer('constants', $this->dependenciesFactory->getNormalizerFactory()->getClosure('bool'));
113 1
        $resolver->setNormalizer('dateTimeStamp', $this->dependenciesFactory->getNormalizerFactory()->getClosure('datetime'));
114 1
        $resolver->setNormalizer('dateValidFrom', $this->dependenciesFactory->getNormalizerFactory()->getClosure('date'));
115 1
        $resolver->setNormalizer('submenu', $this->dependenciesFactory->getNormalizerFactory()->getClosure('boolean'));
116
    }
117
}
118