Test Failed
Push — master ( 0864dc...48ec37 )
by Petr
12:45
created

AbstractDocument::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 3
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
use Riesenia\Pohoda\Common\AddParameterToHeaderTrait;
15
use Riesenia\Pohoda\Common\OptionsResolver;
16
17
abstract class AbstractDocument extends AbstractAgenda
18
{
19
    use AddParameterToHeaderTrait;
20
21
    /**
22
     * Add document item.
23
     *
24
     * @param array<string,mixed> $data
25
     *
26
     * @return Document\AbstractPart
27 56
     */
28
    public function addItem(array $data): Document\AbstractPart
29
    {
30
        $key = $this->getDocumentName() . 'Detail';
31
32 56
        if (!isset($this->data[$key])
33
            || !(
34 56
                is_array($this->data[$key])
35
                || (is_object($this->data[$key]) && is_a($this->data[$key], \ArrayAccess::class))
36
            )
37
        ) {
38
            $this->data[$key] = [];
39
        }
40
41
        $part = $this->getDocumentPart('Item')
42
            ->setDirectionalVariable($this->useOneDirectionalVariables)
43
            ->setData($data);
44 12
        $this->data[$key][] = $part;
45
46 12
        return $part;
47
    }
48 12
49 12
    /**
50 12
     * Add document summary.
51 12
     *
52 12
     * @param array<string,mixed> $data
53
     *
54 12
     * @return $this
55
     */
56
    public function addSummary(array $data): self
57 12
    {
58 12
        $this->data['summary'] = $this->getDocumentPart('Summary')
59
            ->setDirectionalVariable($this->useOneDirectionalVariables)
60 12
            ->setData($data);
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function setData(array $data): parent
69
    {
70 11
        // pass to header
71
        if (!empty($data)) {
72 11
            $data = [
73
                'header' => $this->getDocumentPart('Header', $this->resolveOptions)
74 11
                    ->setDirectionalVariable($this->useOneDirectionalVariables)
75
                    ->setData($data),
76
            ];
77
        }
78
        return parent::setData($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setData($data) returns the type Riesenia\Pohoda\AbstractAgenda which is incompatible with the type-hinted return parent.
Loading history...
79
    }
80 54
81
    /**
82
     * {@inheritdoc}
83 54
     */
84 54
    public function getXML(): \SimpleXMLElement
85
    {
86 54
        $xml = $this->createXML()->addChild(
87
            $this->getChildKey($this->getDocumentNamespace() . ':' . $this->getDocumentName()),
88
            '',
89
            $this->namespace($this->getChildNamespacePrefix($this->getDocumentNamespace())),
90
        );
91
        $xml->addAttribute('version', '2.0');
92 46
93
        $this->addElements($xml, $this->getDocumentElements(), $this->getDocumentNamespace());
94 46
95 46
        return $xml;
96 46
    }
97 46
98 46
    /**
99 46
     * {@inheritdoc}
100
     */
101 46
    protected function configureOptions(OptionsResolver $resolver): void
102
    {
103 46
        // available options
104
        $resolver->setDefined(['header']);
105
    }
106
107
    /**
108
     * Document part factory.
109 10
     * This code is the loader for things like "Header", "Summary", "Item"
110
     *
111
     * @param string              $partName
112 10
     * @param bool                $resolveOptions
113
     *
114
     * @return Document\AbstractPart
115
     */
116
    protected function getDocumentPart(string $partName, bool $resolveOptions = true): Document\AbstractPart
117
    {
118
        $part = $this->dependenciesFactory->getDocumentPartFactory()->getPart(\get_class($this), $partName);
119
        $part->setNamespace($this->getDocumentNamespace());
120
        $part->setNodePrefix($this->getDocumentName());
121
        $part->setResolveOptions($resolveOptions);
122
        return $part;
123
    }
124 54
125
    /**
126 54
     * Get defined elements.
127 54
     *
128 54
     * @return string[]
129 54
     */
130 54
    protected function getDocumentElements(): array
131
    {
132
        return ['header', $this->getDocumentName() . 'Detail', 'summary'];
133
    }
134
135
    /**
136
     * Get document namespace.
137
     *
138 46
     * @return string
139
     */
140 46
    abstract protected function getDocumentNamespace(): string;
141
142
    /**
143
     * Get document name used in XML nodes.
144
     *
145
     * @return string
146
     */
147
    abstract protected function getDocumentName(): string;
148
}
149