TMultiValue::getValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace kalanis\kw_tree_controls\Controls;
4
5
6
use kalanis\kw_forms\Controls;
7
use kalanis\kw_input\Interfaces\IFileEntry;
8
9
10
/**
11
 * Trait TMultiValue
12
 * @package kalanis\kw_tree_controls\Controls
13
 */
14
trait TMultiValue
15
{
16
    use Controls\TShorterKey;
17
18
    /** @var Controls\AControl[]|Controls\Checkbox[] */
19
    protected array $inputs = [];
20
21
    /**
22
     * @return array<string|int, string|int|float|bool|IFileEntry|null>
23
     */
24 1
    public function getValues(): array
25
    {
26 1
        $array = [];
27 1
        foreach ($this->inputs as $child) {
28 1
            if (empty($child->getValue())) {
29 1
                continue;
30
            }
31 1
            $array[] = $child->getValue();
32
        }
33 1
        return $array;
34
    }
35
36
    /**
37
     * Set values to all children
38
     * !! UNDEFINED values will be SET too !!
39
     * @param array<string|int, string|int|float|bool|IFileEntry|null> $array
40
     */
41 1
    public function setValues(array $array): void
42
    {
43 1
        foreach ($this->inputs as $child) {
44
            /** @var Controls\Checkbox $child */
45 1
            $shortKey = $this->shorterKey($child->getKey());
46 1
            $child->setValue(
47 1
                isset($array[$shortKey])
48 1
                && is_array($array[$shortKey])
49 1
                && in_array($child->getOriginalValue(), $array[$shortKey])
50 1
                    ? $child->getOriginalValue()
51
                    : (
52 1
                    isset($array[$child->getKey()])
53 1
                    && is_array($array[$child->getKey()])
54 1
                    && in_array($child->getOriginalValue(), $array[$child->getKey()])
55
                        // @codeCoverageIgnoreStart
56
                        // must got custom key - not happened in usual cases
57
                        ? $child->getOriginalValue()
58
                        // @codeCoverageIgnoreEnd
59 1
                        : ''
60
                )
61
            );
62
        }
63 1
    }
64
}
65