Header::getXML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 1
nop 0
crap 2
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\Stock;
13
14
use Riesenia\Pohoda\AbstractAgenda;
15
use Riesenia\Pohoda\Common;
16
use Riesenia\Pohoda\ValueTransformer\SanitizeEncoding;
17
18
class Header extends AbstractAgenda
19
{
20
    use Common\AddParameterTrait;
21
22
    /** @var string[] */
23
    protected array $refElements = ['storage', 'typePrice', 'typeRP', 'supplier', 'typeServiceMOSS'];
24
25
    /** @var string[] */
26
    protected array $elements = ['stockType', 'code', 'EAN', 'PLU', 'isSales', 'isSerialNumber', 'isInternet', 'isBatch', 'purchasingRateVAT', 'sellingRateVAT', 'name', 'nameComplement', 'unit', 'unit2', 'unit3', 'coefficient2', 'coefficient3', 'storage', 'typePrice', 'purchasingPrice', 'purchasingPricePayVAT', 'sellingPrice', 'sellingPricePayVAT', 'limitMin', 'limitMax', 'mass', 'volume', 'supplier', 'orderName', 'orderQuantity', 'shortName', 'typeRP', 'guaranteeType', 'guarantee', 'producer', 'typeServiceMOSS', 'description', 'description2', 'note', 'intrastat', 'recyclingContrib'];
27
28
    /** @var string[] */
29
    protected array $additionalElements = ['weightedPurchasePrice', 'count', 'countIssue', 'countReceivedOrders', 'reservation', 'countIssuedOrders', 'reclamation', 'service'];
30
31
    /** @var int */
32
    protected int $imagesCounter = 0;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 20
    public function __construct(
38
        Common\NamespacesPaths $namespacesPaths,
39
        SanitizeEncoding $sanitizeEncoding,
40
        string $companyRegistrationNumber,
41
        bool $resolveOptions = true,
42
        Common\OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = new Common\OptionsResolver\Normalizers\NormalizerFactory(),
43
    ) {
44
        // init attributes
45 20
        $this->elementsAttributesMapper = [
46 20
            'purchasingPricePayVAT' => new Common\ElementAttributes('purchasingPrice', 'payVAT'),
47 20
            'sellingPricePayVAT' => new Common\ElementAttributes('sellingPrice', 'payVAT'),
48 20
        ];
49
50 20
        parent::__construct($namespacesPaths, $sanitizeEncoding, $companyRegistrationNumber, $resolveOptions, $normalizerFactory);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 20
    public function setData(array $data): parent
57
    {
58
        // process intrastat
59 20
        if (isset($data['intrastat'])) {
60 10
            $intrastat = new Intrastat($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
61 10
            $data['intrastat'] = $intrastat->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data['intrastat']);
62
        }
63
64
        // process recyclingContrib
65 20
        if (isset($data['recyclingContrib'])) {
66 10
            $recyclingContrib = new RecyclingContrib($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
67 10
            $data['recyclingContrib'] = $recyclingContrib->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data['recyclingContrib']);
68
        }
69
70 20
        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...
71
    }
72
73
    /**
74
     * Add image.
75
     *
76
     * @param string   $filepath
77
     * @param string   $description
78
     * @param int|null $order
79
     * @param bool     $default
80
     *
81
     * @return void
82
     */
83 1
    public function addImage(string $filepath, string $description = '', int $order = null, bool $default = false): void
84
    {
85 1
        if (!isset($this->data['pictures'])
86 1
            || !(
87 1
                is_array($this->data['pictures'])
88 1
                || (is_object($this->data['pictures']) && is_a($this->data['pictures'], \ArrayAccess::class))
89 1
            )
90
        ) {
91 1
            $this->data['pictures'] = [];
92
        }
93
94 1
        $picture = new Picture($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
95 1
        $this->data['pictures'][] = $picture->setDirectionalVariable($this->useOneDirectionalVariables)->setData([
96 1
            'filepath' => $filepath,
97 1
            'description' => $description,
98 1
            'order' => null === $order ? ++$this->imagesCounter : $order,
99 1
            'default' => $default,
100 1
        ]);
101
    }
102
103
    /**
104
     * Add category.
105
     *
106
     * @param int $categoryId
107
     *
108
     * @return void
109
     */
110 1
    public function addCategory(int $categoryId): void
111
    {
112 1
        if (!isset($this->data['categories'])
113 1
            || !(
114 1
                is_array($this->data['categories'])
115 1
                || (is_object($this->data['categories']) && is_a($this->data['categories'], \ArrayAccess::class))
116 1
            )
117
        ) {
118 1
            $this->data['categories'] = [];
119
        }
120
121 1
        $category = new Category($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
122 1
        $this->data['categories'][] = $category->setDirectionalVariable($this->useOneDirectionalVariables)->setData([
123 1
            'idCategory' => $categoryId,
124 1
        ]);
125
    }
126
127
    /**
128
     * Add int parameter.
129
     *
130
     * @param array<string,mixed> $data
131
     *
132
     * @return void
133
     */
134 1
    public function addIntParameter(array $data): void
135
    {
136 1
        if (!isset($this->data['intParameters'])
137 1
            || !(
138 1
                is_array($this->data['intParameters'])
139 1
                || (is_object($this->data['intParameters']) && is_a($this->data['intParameters'], \ArrayAccess::class))
140 1
            )
141
        ) {
142 1
            $this->data['intParameters'] = [];
143
        }
144
145 1
        $intParameters = new IntParameter($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
146 1
        $this->data['intParameters'][] = $intParameters->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 17
    public function getXML(): \SimpleXMLElement
153
    {
154 17
        $xml = $this->createXML()->addChild('stk:stockHeader', '', $this->namespace('stk'));
155
156 17
        $this->addElements($xml, \array_merge($this->elements, ($this->useOneDirectionalVariables ? $this->additionalElements : []), ['categories', 'pictures', 'parameters', 'intParameters']), 'stk');
157
158 17
        return $xml;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 2
    protected function configureOptions(Common\OptionsResolver $resolver): void
165
    {
166
        // available options
167 2
        $resolver->setDefined(array_merge($this->elements, ($this->useOneDirectionalVariables ? $this->additionalElements : [])));
168
169
        // validate / format options
170 2
        $resolver->setDefault('stockType', 'card');
171 2
        $resolver->setAllowedValues('stockType', ['card', 'text', 'service', 'package', 'set', 'product']);
172 2
        $resolver->setNormalizer('isSales', $this->normalizerFactory->getClosure('bool'));
173 2
        $resolver->setNormalizer('isSerialNumber', $this->normalizerFactory->getClosure('bool'));
174 2
        $resolver->setNormalizer('isInternet', $this->normalizerFactory->getClosure('bool'));
175 2
        $resolver->setNormalizer('isBatch', $this->normalizerFactory->getClosure('bool'));
176 2
        $resolver->setAllowedValues('purchasingRateVAT', ['none', 'third', 'low', 'high']);
177 2
        $resolver->setAllowedValues('sellingRateVAT', ['none', 'third', 'low', 'high']);
178 2
        $resolver->setNormalizer('name', $this->normalizerFactory->getClosure('string90'));
179 2
        $resolver->setNormalizer('nameComplement', $this->normalizerFactory->getClosure('string90'));
180 2
        $resolver->setNormalizer('unit', $this->normalizerFactory->getClosure('string10'));
181 2
        $resolver->setNormalizer('unit2', $this->normalizerFactory->getClosure('string10'));
182 2
        $resolver->setNormalizer('unit3', $this->normalizerFactory->getClosure('string10'));
183 2
        $resolver->setNormalizer('coefficient2', $this->normalizerFactory->getClosure('float'));
184 2
        $resolver->setNormalizer('coefficient3', $this->normalizerFactory->getClosure('float'));
185 2
        $resolver->setNormalizer('purchasingPrice', $this->normalizerFactory->getClosure('float'));
186 2
        $resolver->setNormalizer('purchasingPricePayVAT', $this->normalizerFactory->getClosure('bool'));
187 2
        $resolver->setNormalizer('sellingPrice', $this->normalizerFactory->getClosure('float'));
188 2
        $resolver->setNormalizer('sellingPricePayVAT', $this->normalizerFactory->getClosure('bool'));
189 2
        $resolver->setNormalizer('limitMin', $this->normalizerFactory->getClosure('float'));
190 2
        $resolver->setNormalizer('limitMax', $this->normalizerFactory->getClosure('float'));
191 2
        $resolver->setNormalizer('mass', $this->normalizerFactory->getClosure('float'));
192 2
        $resolver->setNormalizer('volume', $this->normalizerFactory->getClosure('float'));
193 2
        $resolver->setNormalizer('orderName', $this->normalizerFactory->getClosure('string90'));
194 2
        $resolver->setNormalizer('orderQuantity', $this->normalizerFactory->getClosure('float'));
195 2
        $resolver->setNormalizer('shortName', $this->normalizerFactory->getClosure('string24'));
196 2
        $resolver->setAllowedValues('guaranteeType', ['none', 'hour', 'day', 'month', 'year', 'life']);
197 2
        $resolver->setNormalizer('guarantee', $this->normalizerFactory->getClosure('int'));
198 2
        $resolver->setNormalizer('producer', $this->normalizerFactory->getClosure('string90'));
199 2
        $resolver->setNormalizer('description', $this->normalizerFactory->getClosure('string240'));
200
201 2
        if ($this->useOneDirectionalVariables) {
202 1
            $resolver->setNormalizer('weightedPurchasePrice', $this->normalizerFactory->getClosure('float'));
203 1
            $resolver->setNormalizer('count', $this->normalizerFactory->getClosure('float'));
204 1
            $resolver->setNormalizer('countIssue', $this->normalizerFactory->getClosure('float'));
205 1
            $resolver->setNormalizer('countReceivedOrders', $this->normalizerFactory->getClosure('float'));
206 1
            $resolver->setNormalizer('reservation', $this->normalizerFactory->getClosure('float'));
207 1
            $resolver->setNormalizer('countIssuedOrders', $this->normalizerFactory->getClosure('float'));
208 1
            $resolver->setNormalizer('reclamation', $this->normalizerFactory->getClosure('float'));
209 1
            $resolver->setNormalizer('service', $this->normalizerFactory->getClosure('float'));
210
        }
211
    }
212
}
213