Item::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
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\Order;
13
14
use Riesenia\Pohoda\Common;
15
use Riesenia\Pohoda\Document\AbstractItem;
16
use Riesenia\Pohoda\ValueTransformer\SanitizeEncoding;
17
18
class Item extends AbstractItem
19
{
20
    /** @var string[] */
21
    protected array $refElements = ['typeServiceMOSS', 'centre', 'activity', 'contract'];
22
23
    /** @var string[] */
24
    protected array $elements = ['text', 'quantity', 'delivered', 'unit', 'coefficient', 'payVAT', 'rateVAT', 'rateVatValue', 'percentVAT', 'discountPercentage', 'homeCurrency', 'foreignCurrency', 'typeServiceMOSS', 'note', 'code', 'stockItem', 'centre', 'activity', 'contract', 'PDP'];
25
26
    /** @var string[] */
27
    protected array $additionalElements = ['id'];
28
29 5
    public function __construct(
30
        Common\NamespacesPaths $namespacesPaths,
31
        SanitizeEncoding $sanitizeEncoding,
32
        string $companyRegistrationNumber,
33
        bool $resolveOptions = true,
34
        Common\OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = new Common\OptionsResolver\Normalizers\NormalizerFactory(),
35
    ) {
36
        // init attributes
37 5
        $this->elementsAttributesMapper = [
38 5
            'rateVatValue' => new Common\ElementAttributes('rateVAT', 'value'),
39 5
        ];
40
41 5
        parent::__construct($namespacesPaths, $sanitizeEncoding, $companyRegistrationNumber, $resolveOptions, $normalizerFactory);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 5
    public function getXML(): \SimpleXMLElement
48
    {
49 5
        if (is_null($this->namespace)) {
50 1
            throw new \LogicException('Namespace not set.');
51
        }
52
53 4
        if (is_null($this->nodePrefix)) {
54 1
            throw new \LogicException('Node name prefix not set.');
55
        }
56
57 3
        $xml = $this->createXML()->addChild($this->namespace . ':' . $this->nodePrefix . 'Item', '', $this->namespace($this->namespace));
58
59 3
        $this->addElements($xml, \array_merge($this->elements, ($this->useOneDirectionalVariables ? $this->additionalElements : []), ['parameters']), $this->namespace);
60
61 3
        return $xml;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    protected function configureOptions(Common\OptionsResolver $resolver): void
68
    {
69 2
        $resolver->setDefined(array_merge($this->elements, ($this->useOneDirectionalVariables ? $this->additionalElements : [])));
70
71
        // validate / format options
72 2
        $resolver->setNormalizer('text', $this->normalizerFactory->getClosure('string90'));
73 2
        $resolver->setNormalizer('quantity', $this->normalizerFactory->getClosure('float'));
74 2
        $resolver->setNormalizer('delivered', $this->normalizerFactory->getClosure('float'));
75 2
        $resolver->setNormalizer('unit', $this->normalizerFactory->getClosure('string10'));
76 2
        $resolver->setNormalizer('coefficient', $this->normalizerFactory->getClosure('float'));
77 2
        $resolver->setNormalizer('payVAT', $this->normalizerFactory->getClosure('bool'));
78 2
        $resolver->setAllowedValues('rateVAT', ['none', 'high', 'low', 'third', 'historyHigh', 'historyLow', 'historyThird']);
79 2
        $resolver->setNormalizer('rateVatValue', $this->normalizerFactory->getClosure('float'));
80 2
        $resolver->setNormalizer('percentVAT', $this->normalizerFactory->getClosure('float'));
81 2
        $resolver->setNormalizer('discountPercentage', $this->normalizerFactory->getClosure('float'));
82 2
        $resolver->setNormalizer('note', $this->normalizerFactory->getClosure('string90'));
83 2
        $resolver->setNormalizer('code', $this->normalizerFactory->getClosure('string64'));
84 2
        $resolver->setNormalizer('PDP', $this->normalizerFactory->getClosure('bool'));
85
86 2
        if ($this->useOneDirectionalVariables) {
87 1
            $resolver->setNormalizer('id', $this->normalizerFactory->getClosure('int'));
88
        }
89
    }
90
}
91