Passed
Push — master ( 067151...1e051c )
by Petr
13:11
created

AbstractDocument::getDocumentElements()   A

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