Parameter::getXML()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4
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\Type;
13
14
use Riesenia\Pohoda\AbstractAgenda;
15
use Riesenia\Pohoda\Common\OptionsResolver;
16
17
class Parameter extends AbstractAgenda
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 17
    public function getXML(): \SimpleXMLElement
23
    {
24 17
        $xml = $this->createXML()->addChild('typ:parameter', '', $this->namespace('typ'));
25
26 17
        $child = $this->data['name'] ?? null;
27 17
        $xml->addChild('typ:name', is_null($child) ? null : strval($child));
28
29 17
        if ('list' == $this->data['type']) {
30 13
            $this->addRefElement($xml, 'typ:listValueRef', $this->data['value']);
31
32 13
            if (isset($this->data['list'])) {
33 13
                $this->addRefElement($xml, 'typ:list', $this->data['list']);
34
            }
35
36
        } else {
37 15
            $xml->addChild('typ:' . $this->data['type'] . 'Value', \htmlspecialchars(strval($this->data['value'])));
38
        }
39
40 17
        return $xml;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 17
    protected function configureOptions(OptionsResolver $resolver): void
47
    {
48
        // available options
49 1
        $resolver->setDefined(['name', 'type', 'value', 'list']);
50
51
        // validate / format options
52 1
        $resolver->setRequired('name');
53 1
        $resolver->setNormalizer('name', function (OptionsResolver $options, mixed $value): string {
54 17
            $prefix = 'VPr';
55 17
            $value = \strval($value);
56
57 17
            if ('list' == $options['type']) {
58 13
                $prefix = 'RefVPr';
59
            }
60
61 17
            if (str_starts_with($value, $prefix)) {
62 14
                return $value;
63
            }
64
65 14
            return $prefix . $value;
66 1
        });
67 1
        $resolver->setRequired('type');
68 1
        $resolver->setAllowedValues('type', ['text', 'memo', 'currency', 'boolean', 'number', 'datetime', 'integer', 'list']);
69 1
        $resolver->setNormalizer('value', function ($options, $value) {
70 17
            $normalizer = $options['type'];
71
72
            // date for datetime
73 17
            if ('datetime' == $normalizer) {
74 1
                $normalizer = 'date';
75
            }
76
77
            try {
78 17
                return \call_user_func($this->normalizerFactory->getClosure($normalizer), [], $value);
79 14
            } catch (\Exception) {
80 14
                return \is_array($value) ? $value : \strval($value);
81
            }
82 1
        });
83
    }
84
}
85