Storage::getImportRoot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
/**
15
 * @property array{
16
 *     code: string,
17
 *     name?: string,
18
 *     subStorages?: iterable<Storage>,
19
 * } $data
20
 */
21
class Storage extends AbstractAgenda
22
{
23 1
    public function getImportRoot(): string
24
    {
25 1
        return 'lst:itemStorage';
26
    }
27
28
    /**
29
     * Add substorage.
30
     *
31
     * @param self $storage
32
     *
33
     * @return $this
34
     */
35 1
    public function addSubstorage(self $storage): self
36
    {
37 1
        if (!isset($this->data['subStorages'])
38 1
            || !(
39 1
                is_array($this->data['subStorages'])
40 1
                || (is_a($this->data['subStorages'], \ArrayAccess::class))
41 1
            )
42
        ) {
43 1
            $this->data['subStorages'] = [];
44
        }
45
46 1
        $this->data['subStorages'][] = $storage;
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function getXML(): \SimpleXMLElement
55
    {
56 2
        $xml = $this->createXML()->addChild('str:storage', '', $this->namespace('str'));
57 2
        $xml->addAttribute('version', '2.0');
58
59 2
        $this->storageXML($xml);
60
61 2
        return $xml;
62
    }
63
64
    /**
65
     * Attach storage to XML element.
66
     *
67
     * @param \SimpleXMLElement $xml
68
     *
69
     * @return void
70
     */
71 2
    public function storageXML(\SimpleXMLElement $xml): void
72
    {
73 2
        $storage = $xml->addChild('str:itemStorage', '', $this->namespace('str'));
74 2
        $storage->addAttribute('code', strval($this->data['code']));
75
76 2
        if (isset($this->data['name'])) {
77 1
            $storage->addAttribute('name', strval($this->data['name']));
78
        }
79
80 2
        if (isset($this->data['subStorages'])) {
81 1
            $subStorages = $storage->addChild('str:subStorages', '', $this->namespace('str'));
82
83 1
            foreach ($this->data['subStorages'] as $subStorage) {
84
                /** @var self $subStorage */
85 1
                $subStorage->storageXML($subStorages);
86
            }
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    protected function configureOptions(Common\OptionsResolver $resolver): void
94
    {
95
        // available options
96 1
        $resolver->setDefined(['code', 'name']);
97
98
        // validate / format options
99 1
        $resolver->setRequired('code');
100
    }
101
}
102