UserList::addItemUserCode()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 2
nop 1
crap 5
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
class UserList extends AbstractAgenda
21
{
22 1
    public function getImportRoot(): string
23
    {
24 1
        return 'lst:listUserCode';
25
    }
26
27
    /**
28
     * Add item user code.
29
     *
30
     * @param array<string,mixed> $data
31
     *
32
     * @return $this
33
     */
34 2
    public function addItemUserCode(array $data): self
35
    {
36 2
        if (!isset($this->data['itemUserCodes'])
37 2
            || !(
38 2
                is_array($this->data['itemUserCodes'])
39 2
                || (is_object($this->data['itemUserCodes']) && is_a($this->data['itemUserCodes'], \ArrayAccess::class))
40 2
            )
41
        ) {
42 2
            $this->data['itemUserCodes'] = [];
43
        }
44
45 2
        $itemUserCodes = new ItemUserCode($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
46 2
        $this->data['itemUserCodes'][] = $itemUserCodes->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
47
48 2
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function getXML(): \SimpleXMLElement
55
    {
56 2
        $xml = $this->createXML()->addChild('lst:listUserCode', '', $this->namespace('lst'));
57 2
        $xml->addAttribute('version', '1.1');
58 2
        $xml->addAttribute('code', strval($this->data['code']));
59 2
        $xml->addAttribute('name', strval($this->data['name']));
60
61 2
        if (isset($this->data['dateTimeStamp'])) {
62 1
            $xml->addAttribute('dateTimeStamp', strval($this->data['dateTimeStamp']));
63
        }
64
65 2
        if (isset($this->data['dateValidFrom'])) {
66 1
            $xml->addAttribute('dateValidFrom', strval($this->data['dateValidFrom']));
67
        }
68
69 2
        if (isset($this->data['submenu'])) {
70 1
            $xml->addAttribute('submenu', strval($this->data['submenu']));
71
        }
72
73 2
        if (isset($this->data['constants']) && 'true' == $this->data['constants']) {
74 1
            $xml->addAttribute('constants', strval($this->data['constants']));
75
        }
76
77 2
        if (isset($this->data['itemUserCodes']) && is_iterable($this->data['itemUserCodes'])) {
78 2
            foreach ($this->data['itemUserCodes'] as $itemUserCode) {
79
                /** @var ItemUserCode $itemUserCode */
80 2
                $this->appendNode($xml, $itemUserCode->getXML());
81
            }
82
        }
83
84 2
        return $xml;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    protected function configureOptions(OptionsResolver $resolver): void
91
    {
92
        // available options
93 1
        $resolver->setDefined(['code', 'name', 'constants', 'dateTimeStamp', 'dateValidFrom', 'submenu']);
94
95
        // validate / format options
96 1
        $resolver->setRequired('code');
97 1
        $resolver->setRequired('name');
98 1
        $resolver->setNormalizer('constants', $this->normalizerFactory->getClosure('bool'));
99 1
        $resolver->setNormalizer('dateTimeStamp', $this->normalizerFactory->getClosure('datetime'));
100 1
        $resolver->setNormalizer('dateValidFrom', $this->normalizerFactory->getClosure('date'));
101 1
        $resolver->setNormalizer('submenu', $this->normalizerFactory->getClosure('boolean'));
102
    }
103
}
104