forked from FoolCode/Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.php
More file actions
180 lines (156 loc) · 3.93 KB
/
Copy pathResult.php
File metadata and controls
180 lines (156 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Foolz\Plugin;
/**
* The result that is cascaded through events on the same key and returned by the hook
*
* @author Foolz <[email protected]>
* @package Foolz\Plugin
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License 2.0
*/
class Result
{
/**
* The parameters (which can be modified with setParam())
*
* @var array Array with as keys the parameter key
*/
protected $params = [];
/**
* The original parameters (can't be modified)
*
* @var array Array with as key the parameter key
*/
protected $params_original = [];
/**
* The object in which the Hook runs
*
* @var object
*/
protected $object = null;
/**
* The result
*
* @var mixed
*/
protected $result = null;
/**
* Sets the parameters and in case it's available the object
*
* @param array $params
* @param null|object $object
*/
public function __construct(array $params = array(), $object = null)
{
$this->params = $this->params_original = $params;
$this->object = $object;
$this->result = new Void();
}
/**
* Resets the object to the initial state
*
* @return \Foolz\Plugin\Result The current object
*/
public function reset()
{
$this->params = [];
$this->params_original = [];
$this->object = null;
$this->result = null;
return $this;
}
/**
* Returns the result
*
* @return mixed
*/
public function get($fallback = null)
{
if ($this->result instanceof Void && func_num_args() === 1) {
return $fallback;
}
return $this->result;
}
/**
* Sets the result
*
* @param mixed $val
* @return \Foolz\Plugin\Result
*/
public function set($val)
{
$this->result = $val;
return $this;
}
/**
* Returns the object from
*
* @return mixed the object
* @throws \OutOfBoundsException if there's no object
*/
public function getObject()
{
if ($this->object === null) {
throw new \OutOfBoundsException('No object has been set.');
}
return $this->object;
}
/**
* Returns the array of parameters
*
* @param bool $orig whether we want the original array of parameters
* @return array
*/
public function getParams($orig = false)
{
if ($orig === true) {
return $this->params_original;
}
return $this->params;
}
/**
* Returns the parameter with the key
*
* @param string $key
* @param bool $orig whether we want the original value of the parameter
* @return mixed
* @throws \OutOfBoundsException if the key is not set
*/
public function getParam($key, $orig = false)
{
if ($orig === true) {
if (!isset($this->params_original[$key])) {
throw new \OutOfBoundsException('Undefined original parameter.');
}
return $this->params_original[$key];
}
if (!isset($this->params[$key])) {
throw new \OutOfBoundsException('Undefined parameter.');
}
return $this->params[$key];
}
/**
* Updates a parameter
*
* @param string $key
* @param mixed $value
* @return \Foolz\Plugin\Result
*/
public function setParam($key, $value)
{
$this->params[$key] = $value;
return $this;
}
/**
* Updates several parameters
*
* @param array $array Array with as keys the parameter key and as value the parameter value
* @return \Foolz\Plugin\Result
*/
public function setParams($array)
{
foreach ($array as $key => $item) {
$this->params[$key] = $item;
}
return $this;
}
}