Hoa central
Throwable.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Praspel\Model;
38 
39 use Hoa\Iterator;
40 
49 class Throwable
50  extends Clause
51  implements Iterator\Aggregate,
52  \ArrayAccess,
53  \Countable
54 {
60  const NAME = 'throwable';
61 
67  const IDENTIFIER = 0;
68 
74  const INSTANCE_OF = 1;
75 
81  const WITH = 2;
82 
88  const DISJUNCTION = 3;
89 
95  protected $_exceptions = [];
96 
102  protected $_currentException = null;
103 
104 
105 
112  public function offsetExists($identifier)
113  {
114  return isset($this->_exceptions[$identifier]);
115  }
116 
123  public function offsetGet($identifier)
124  {
125  if (false === $this->offsetExists($identifier)) {
126  return null;
127  }
128 
129  unset($this->_currentException);
130  $this->_currentException = &$this->_exceptions[$identifier];
131 
132  return $this;
133  }
134 
142  public function offsetSet($identifier, $instanceName)
143  {
144  $old = $this->offsetGet($identifier);
145  $this->_exceptions[$identifier] = [
146  static::IDENTIFIER => $identifier,
147  static::INSTANCE_OF => $instanceName,
148  static::WITH => null,
149  static::DISJUNCTION => null
150  ];
151 
152  return $old;
153  }
154 
161  public function offsetUnset($identifier)
162  {
163  unset($this->_exceptions[$identifier]);
164 
165  return;
166  }
167 
173  public function getInstanceName()
174  {
175  if (null === $this->_currentException) {
176  return null;
177  }
178 
179  return $this->_currentException[static::INSTANCE_OF];
180  }
181 
188  public function newWith()
189  {
190  return new Ensures($this);
191  }
192 
199  public function setWith(Ensures $with)
200  {
201  if (null === $this->_currentException) {
202  return $this;
203  }
204 
205  $this->_currentException[static::WITH] = $with;
206 
207  return $this;
208  }
209 
215  public function getWith()
216  {
217  if (null === $this->_currentException) {
218  return null;
219  }
220 
221  return $this->_currentException[static::WITH];
222  }
223 
230  public function disjunctionWith($identifier)
231  {
232  if (null === $this->_currentException) {
233  return $this;
234  }
235 
236  if (false === isset($this[$identifier])) {
237  return $this;
238  }
239 
240  $_identifier = &$this->_exceptions[$identifier];
241  $this->_currentException[static::WITH] = &$_identifier[static::WITH];
242 
243  if (true === is_array($_identifier[static::DISJUNCTION])) {
244  $_identifier[static::DISJUNCTION][] =
245  $this->_currentException[static::IDENTIFIER];
246  } else {
247  $_identifier[static::DISJUNCTION] = [
248  $this->_currentException[static::IDENTIFIER]
249  ];
250  }
251 
252  $this->_currentException[static::DISJUNCTION] = $identifier;
253 
254  return $this;
255  }
256 
262  public function isDisjointed()
263  {
264  return is_string($this->getDisjunction());
265  }
266 
279  public function getDisjunction()
280  {
281  if (null === $this->_currentException) {
282  return null;
283  }
284 
285  return $this->_currentException[static::DISJUNCTION];
286  }
287 
293  public function getExceptions()
294  {
295  return $this->_exceptions;
296  }
297 
303  public function getIterator()
304  {
305  return new Iterator\Map(array_keys($this->getExceptions()));
306  }
307 
313  public function count()
314  {
315  return count($this->getExceptions());
316  }
317 }
offsetSet($identifier, $instanceName)
Definition: Throwable.php:142
disjunctionWith($identifier)
Definition: Throwable.php:230