Completed
Push — master ( e91368...26d5da )
by Anton
9s
created

Event::setParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\EventManager;
13
14
/**
15
 * Representation of an event
16
 *
17
 * @package  Bluz\EventManager
18
 */
19
class Event
20
{
21
    /**
22
     * @var string event name
23
     */
24
    protected $name;
25
26
    /**
27
     * @var string|object the event target
28
     */
29
    protected $target;
30
31
    /**
32
     * @var array|object the event parameters
33
     */
34
    protected $params = [];
35
36
    /**
37
     * Constructor
38
     *
39
     * Accept a target and its parameters.
40
     *
41
     * @param  string        $name Event name
42
     * @param  string|object $target
43
     * @param  array|object  $params
44
     */
45 11
    public function __construct($name, $target = null, $params = null)
46
    {
47 11
        $this->setName($name);
48
49 11
        if (null !== $target) {
50 4
            $this->setTarget($target);
51
        }
52
53 11
        if (null !== $params) {
54 5
            $this->setParams($params);
55
        }
56 9
    }
57
58
    /**
59
     * Get event name
60
     *
61
     * @return string
62
     */
63 8
    public function getName()
64
    {
65 8
        return $this->name;
66
    }
67
68
    /**
69
     * Get the event target
70
     *
71
     * This may be either an object, or the name of a static method.
72
     *
73
     * @return string|object
74
     */
75 8
    public function getTarget()
76
    {
77 8
        return $this->target;
78
    }
79
80
    /**
81
     * Overwrites parameters
82
     *
83
     * @param  array|object $params
84
     * @return Event
85
     * @throws EventException
86
     */
87 5
    public function setParams($params)
88
    {
89 5
        if (!is_array($params) && !is_object($params)) {
90 2
            throw new EventException(
91
                sprintf(
92 2
                    'Event parameters must be an array or object; received "%s"',
93
                    gettype($params)
94
                )
95
            );
96
        }
97
98 3
        $this->params = $params;
99 3
        return $this;
100
    }
101
102
    /**
103
     * Get all parameters
104
     *
105
     * @return array|object
106
     */
107 2
    public function getParams()
108
    {
109 2
        return $this->params;
110
    }
111
112
    /**
113
     * Get an individual parameter
114
     *
115
     * If the parameter does not exist, the $default value will be returned.
116
     *
117
     * @param  string|int $name
118
     * @param  mixed      $default
119
     * @return mixed
120
     */
121 3
    public function getParam($name, $default = null)
122
    {
123 3
        if (is_array($this->params)) {
124
            // Check in params that are arrays or implement array access
125 2
            return $this->params[$name] ?? $default;
126 1
        } elseif (is_object($this->params)) {
127
            // Check in normal objects
128 1
            return $this->params->{$name} ?? $default;
129
        } else {
130
            // Wrong type, return default value
131
            return $default;
132
        }
133
    }
134
135
    /**
136
     * Set the event name
137
     *
138
     * @param  string $name
139
     * @return Event
140
     */
141 11
    public function setName($name)
142
    {
143 11
        $this->name = (string)$name;
144 11
        return $this;
145
    }
146
147
    /**
148
     * Set the event target/context
149
     *
150
     * @param  null|string|object $target
151
     * @return Event
152
     */
153 4
    public function setTarget($target)
154
    {
155 4
        $this->target = $target;
156 4
        return $this;
157
    }
158
159
    /**
160
     * Set an individual parameter to a value
161
     *
162
     * @param  string|int $name
163
     * @param  mixed      $value
164
     * @return Event
165
     */
166 2
    public function setParam($name, $value)
167
    {
168 2
        if (is_array($this->params)) {
169
            // Arrays or objects implementing array access
170 1
            $this->params[$name] = $value;
171
        } else {
172
            // Objects
173 1
            $this->params->{$name} = $value;
174
        }
175 2
        return $this;
176
    }
177
}
178