Parameter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 31
dl 0
loc 64
ccs 31
cts 31
cp 1
rs 10
c 3
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configureOptions() 0 35 6
A getXML() 0 19 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