Completed
Push — develop ( 717e04...a5cee0 )
by Peter
01:57
created

DrawEvent::setVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2016 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Event;
12
13
use ArrayObject;
14
use WebinoDraw\Exception\UnexpectedValueException;
15
use WebinoDraw\Exception\RuntimeException;
16
use WebinoDraw\Dom\NodeList;
17
use WebinoDraw\Draw\Helper\HelperInterface;
18
use Zend\EventManager\Event;
19
use Zend\Stdlib\ArrayUtils;
20
21
/**
22
 * Class DrawEvent
23
 */
24
class DrawEvent extends Event
25
{
26
    /**
27
     * @var HelperInterface
28
     */
29
    protected $helper;
30
31
    /**
32
     * @var NodeList
33
     */
34
    protected $nodes;
35
36
    /**
37
     * @var ArrayObject
38
     */
39
    protected $spec;
40
41
    /**
42
     * @return HelperInterface
43
     * @throws RuntimeException
44
     */
45
    public function getHelper()
46
    {
47
        if (null === $this->helper) {
48
            throw new RuntimeException('Expected helper');
49
        }
50
        return $this->helper;
51
    }
52
53
    /**
54
     * @param HelperInterface $helper
55
     * @return $this
56
     */
57
    public function setHelper(HelperInterface $helper)
58
    {
59
        $this->helper = $helper;
60
        return $this;
61
    }
62
63
    /**
64
     * @return NodeList
65
     * @throws RuntimeException
66
     */
67
    public function getNodes()
68
    {
69
        if (null == $this->nodes) {
70
            throw new RuntimeException('Expected nodes');
71
        }
72
        return $this->nodes;
73
    }
74
75
    /**
76
     * @param NodeList $nodes
77
     * @return $this
78
     */
79
    public function setNodes(NodeList $nodes)
80
    {
81
        $this->setParam('nodes', $nodes);
82
        $this->nodes = $nodes;
83
        return $this;
84
    }
85
86
    /**
87
     * @return ArrayObject
88
     */
89
    public function getSpec()
90
    {
91
        if (null === $this->spec) {
92
            $this->setSpec(new ArrayObject);
93
        }
94
        return $this->spec;
95
    }
96
97
    /**
98
     * @param array|ArrayObject $spec
99
     * @return $this
100
     * @throws UnexpectedValueException
101
     */
102
    public function setSpec($spec)
103
    {
104
        if (is_array($spec)) {
105
            $this->spec = $this->getSpec();
106
            $this->setParam('spec', $this->spec);
107
108
            $this->spec->exchangeArray(
109
                array_replace_recursive(
110
                    $this->spec->getArrayCopy(),
111
                    $spec
112
                )
113
            );
114
            return $this;
115
        }
116
117
        if ($spec instanceof ArrayObject) {
118
            $this->setParam('spec', $spec);
119
            $this->spec = $spec;
120
            return $this;
121
        }
122
123
        throw new UnexpectedValueException('Expected array|ArrayObject');
124
    }
125
126
    /**
127
     * @return $this
128
     */
129
    public function clearSpec()
130
    {
131
        $this->spec = null;
132
        return $this;
133
    }
134
135
    /**
136
     * @param string|int $key
137
     * @param string|array|object $value
138
     * @return $this
139
     */
140
    public function setVar($key, $value)
141
    {
142
        $helper     = $this->getHelper();
143
        $vars       = $helper->getVars();
144
        $vars[$key] = $value;
145
        $helper->setVars($vars);
146
        return $this;
147
    }
148
149
    /**
150
     * @param array $vars
151
     * @return $this
152
     */
153
    public function setVars(array $vars)
154
    {
155
        $helper     = $this->getHelper();
156
        $helperVars = $helper->getVars();
157
        $helper->setVars(ArrayUtils::merge($helperVars, $vars));
158
        return $this;
159
    }
160
}
161