ObjectMultiCheckbox::setValue()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.25
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Form\Element;
6
7
use Laminas\Form\Element\MultiCheckbox;
8
use Laminas\Stdlib\ArrayUtils;
9
use Traversable;
10
use function array_map;
11
use function is_array;
12
13
class ObjectMultiCheckbox extends MultiCheckbox
14
{
15
    /** @var Proxy */
16
    protected $proxy;
17
18 5
    public function getProxy() : Proxy
19
    {
20 5
        if ($this->proxy === null) {
21 5
            $this->proxy = new Proxy();
22
        }
23
24 5
        return $this->proxy;
25
    }
26
27
    /**
28
     * @param mixed $options
29
     */
30
    public function setOptions($options) : self
31
    {
32
        $this->getProxy()->setOptions($options);
33
34
        return parent::setOptions($options);
35
    }
36
37
    /**
38
     * @param mixed $value
39
     * @param mixed $key
40
     */
41 1
    public function setOption($key, $value) : self
42
    {
43 1
        $this->getProxy()->setOptions([$key => $value]);
44
45 1
        return parent::setOption($key, $value);
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 2
    public function setValue($value)
52
    {
53 2
        if ($value instanceof Traversable) {
54 1
            $value = ArrayUtils::iteratorToArray($value);
55 1
        } elseif ($value === null) {
56
            return parent::setValue([]);
57 1
        } elseif (! is_array($value)) {
58
            $value = (array) $value;
59
        }
60
61 2
        return parent::setValue(array_map([$this->getProxy(), 'getValue'], $value));
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 2
    public function getValueOptions()
68
    {
69 2
        if (! empty($this->valueOptions)) {
70 1
            return $this->valueOptions;
71
        }
72
73 2
        $proxyValueOptions = $this->getProxy()->getValueOptions();
74
75 2
        if (! empty($proxyValueOptions)) {
76 1
            $this->setValueOptions($proxyValueOptions);
77
        }
78
79 2
        return $this->valueOptions;
80
    }
81
}
82