svn: /pear2/MultiErrors/trunk/src/ MultiErrors.php

From: Date: Mon, 22 Feb 2010 00:59:39 +0000
Subject: svn: /pear2/MultiErrors/trunk/src/ MultiErrors.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-9196@lists.php.net to get a copy of this message
dufuz Mon, 22 Feb 2010 00:59:39 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=295354 Log: Fix up CS, add new lines and indentation Changed paths: U pear2/MultiErrors/trunk/src/MultiErrors.php Modified: pear2/MultiErrors/trunk/src/MultiErrors.php =================================================================== --- pear2/MultiErrors/trunk/src/MultiErrors.php 2010-02-22 00:46:03 UTC (rev 295353) +++ pear2/MultiErrors/trunk/src/MultiErrors.php 2010-02-22 00:59:39 UTC (rev 295354) @@ -5,9 +5,9 @@ * This class is designed to be extended for specific use. It codifies easy * ways of aggregating error conditions that don't necessarily require an exception * to be thrown, but do need an easy way to retrieve them. - * + * * Usage: - * + * * <code> * $multi = new \pear2\MultiErrors(); * $multi->E_WARNING[] = new Exception('test'); @@ -30,9 +30,10 @@ * @license http://www.php.net/license/3_0.txt PHP License */ namespace pear2; -class MultiErrors extends \Exception implements \Iterator, \Countable, \ArrayAccess { +class MultiErrors extends \Exception implements \Iterator, \Countable, \ArrayAccess +{ + private $_allowedLevels = array('E_NOTICE' => 0, 'E_WARNING' => 1, 'E_ERROR' => 2); - private $_allowedLevels = array('E_NOTICE' => 0, 'E_WARNING' => 1, 'E_ERROR' => 2); /** * Errors are stored in the order that they are declared * @var array @@ -68,11 +69,13 @@ throw new MultiErrors\Exception('Invalid level ' . (string) $level); } } - $this->_allowedLevels = array_flip($allowed); + + $this->_allowedLevels = array_flip($allowed); $this->_requestedLevel = $mylevel; if ($mylevel) { $this->_parent = $parent; } + parent::__construct('multiple errors found'); } @@ -103,7 +106,7 @@ /** * Merge in errors from an existing \pear2\MultiErrors - * + * * This also merges in any new error levels not supported in this instance. * @param \pear2\MultiErrors $error */ @@ -113,6 +116,7 @@ if (!isset($this->_allowedLevels[$level])) { $this->_allowedLevels[$level] = 1; } + foreach ($error->$level as $e) { $this->{$level}[] = $e; } @@ -129,33 +133,37 @@ return isset($this->_errors[$offset]); } - public function offsetGet ($offset) + public function offsetGet($offset) { if (isset($this->_errors[$offset])) { return $this->_errors[$offset]; } + return null; } public function offsetSet ($offset, $value) { - if ($offset === null && !$this->_requestedLevel && - $value instanceof self) { + if ($offset === null && !$this->_requestedLevel && $value instanceof self) { $this->merge($value); return; } + if (!($value instanceof \Exception)) { throw new MultiErrors\Exception('offsetSet: $value is not an Exception object'); } + if ($this->_requestedLevel) { - if ($offset === null) { - // called with $a->E_BLAH[] = new Exception('hi'); - $offset = count($this->_errors); - } - if (!is_int($offset)) { - throw new MultiErrors\Exception('offsetSet: $offset is not an integer'); - } - $this->_errors[$offset] = $value; + if ($offset === null) { + // called with $a->E_BLAH[] = new Exception('hi'); + $offset = count($this->_errors); + } + + if (!is_int($offset)) { + throw new MultiErrors\Exception('offsetSet: $offset is not an integer'); + } + + $this->_errors[$offset] = $value; $this->_parent[$this->_requestedLevel . '-' . $offset] = $value; } else { if (!is_string($offset)) { @@ -163,17 +171,20 @@ 'to a pear2\MultiErrors with $a[] = new Exception, use an ' . ' E_* constant like $a->E_WARNING[] = new Exception'); } + $offset = explode('-', $offset); - $level = $offset[0]; + $level = $offset[0]; $offset = $offset[1]; // this is called when the "$this->_parent[] = $value" line is executed. if (!isset($this->_subMulti[$level]) || - $this->_subMulti[$level][$offset] !== $value) { - // must be in a child or it'll throw off the whole thingy + $this->_subMulti[$level][$offset] !== $value + ) { + // must be in a child or it'll throw off the whole thingy throw new MultiErrors\Exception('Cannot add an error directly ' . 'to a pear2\MultiErrors with $a[] = new Exception, use an ' . ' E_* constant like $a->E_WARNING[] = new Exception'); } + $this->_errors[] = $value; } } @@ -190,17 +201,20 @@ if ($level === 'levels') { return array_keys($this->_allowedLevels); } + if (!count($this->_allowedLevels)) { throw new MultiErrors\Exception('Cannot nest requests ' . '(like $multi->E_WARNING->E_ERROR[] = new Exception(\'\');)'); } + if (isset($this->_allowedLevels[$level])) { if (!isset($this->_subMulti[$level])) { - $this->_subMulti[$level] = new self($level, - array(), $this); + $this->_subMulti[$level] = new self($level, array(), $this); } + return $this->_subMulti[$level]; } + throw new MultiErrors\Exception('Requested error level must be one of ' . implode(', ', array_keys($this->_allowedLevels))); } @@ -209,5 +223,4 @@ { return $this->_errors; } -} -?> \ No newline at end of file +} \ No newline at end of file

« previous php.pear.core (#9196) next »