1 | <?php |
||
24 | class Context |
||
25 | { |
||
26 | const NAMESPACE_DEFAULT = 'default'; |
||
27 | |||
28 | const NAMESPACE_ENTITY = 'entity'; |
||
29 | |||
30 | /** |
||
31 | * Properties which will be stored as state data. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $properties = array(); |
||
36 | |||
37 | /** |
||
38 | * Transition payload. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $payload = array(); |
||
43 | |||
44 | /** |
||
45 | * Error collection. |
||
46 | * |
||
47 | * @var ErrorCollection |
||
48 | */ |
||
49 | private $errorCollection; |
||
50 | |||
51 | /** |
||
52 | * Construct. |
||
53 | * |
||
54 | * @param array $properties The properties to be stored. |
||
55 | * @param array $payload The given parameters. |
||
56 | * @param ErrorCollection|null $errorCollection Error collection. |
||
57 | */ |
||
58 | public function __construct(array $properties = [], array $payload = [], ErrorCollection $errorCollection = null) |
||
64 | |||
65 | /** |
||
66 | * Set a property value. |
||
67 | * |
||
68 | * @param string $name Name of the property. |
||
69 | * @param mixed $value Value of the property. |
||
70 | * @param string $namespace Namespace the property belongs to. |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function setProperty(string $name, $value, string $namespace = self::NAMESPACE_DEFAULT): self |
||
80 | |||
81 | /** |
||
82 | * Get the property value. |
||
83 | * |
||
84 | * @param string $name Property name. |
||
85 | * @param string $namespace Namespace the property belongs to. |
||
86 | * |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public function getProperty(string $name, string $namespace = self::NAMESPACE_DEFAULT) |
||
97 | |||
98 | /** |
||
99 | * Consider if property is set. |
||
100 | * |
||
101 | * @param string $name Property name. |
||
102 | * @param string $namespace Namespace the property belongs to. |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function hasProperty(string $name, string $namespace = self::NAMESPACE_DEFAULT): bool |
||
110 | |||
111 | /** |
||
112 | * Get all properties. If namespace isset only properties of a namespace. |
||
113 | * |
||
114 | * @param string $namespace Namespace the property belongs to. |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getProperties(?string $namespace = null): array |
||
130 | |||
131 | /** |
||
132 | * Set a parameter. |
||
133 | * |
||
134 | * @param string $name Param name. |
||
135 | * @param mixed $value Param value. |
||
136 | * @param string $namespace Namespace the param belongs to. |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setParam(string $name, $value, string $namespace = self::NAMESPACE_DEFAULT): self |
||
146 | |||
147 | /** |
||
148 | * Consider if a param isset. |
||
149 | * |
||
150 | * @param string $name Param name. |
||
151 | * @param string $namespace Namespace the param belongs to. |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function hasParam(string $name, string $namespace = self::NAMESPACE_DEFAULT): bool |
||
159 | |||
160 | /** |
||
161 | * Get all params. |
||
162 | * |
||
163 | * If namespace is given only a specific namespace is returned. Otherwise all namespaces are returned. |
||
164 | * |
||
165 | * @param string|null $namespace Optional namespace. |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | public function getPayload(?string $namespace = null): array |
||
181 | |||
182 | /** |
||
183 | * Get a param by name. |
||
184 | * |
||
185 | * @param string $name Param name. |
||
186 | * @param string $namespace Namespace the param belongs to. |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | public function getParam(string $name, ?string $namespace = self::NAMESPACE_DEFAULT) |
||
198 | |||
199 | /** |
||
200 | * Set multiple params. |
||
201 | * |
||
202 | * @param array $params Array of params. |
||
203 | * @param string $namespace Namespace the params belongs to. |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setPayload(array $params, ?string $namespace = null): self |
||
217 | |||
218 | /** |
||
219 | * Get error collection. |
||
220 | * |
||
221 | * @return ErrorCollection |
||
222 | */ |
||
223 | public function getErrorCollection(): ErrorCollection |
||
227 | |||
228 | /** |
||
229 | * Add an error. |
||
230 | * |
||
231 | * @param string $message Error message. |
||
232 | * @param array $params Params for the error message. |
||
233 | * @param ErrorCollection $collection Option. Child collection of the error. |
||
234 | * |
||
235 | * @return self |
||
236 | */ |
||
237 | public function addError(string $message, array $params = array(), ErrorCollection $collection = null): self |
||
243 | |||
244 | /** |
||
245 | * Get a new context with an empty error collection. |
||
246 | * |
||
247 | * @return Context |
||
248 | */ |
||
249 | public function withEmptyErrorCollection(): Context |
||
253 | } |
||
254 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: