Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move to namespaces

  • Loading branch information...
commit 0adf0dc6c630ff8f2cd984bd17a97aed9f76ba9a 1 parent 944c0d5
@cellog cellog authored
View
19 README
@@ -1,5 +1,4 @@
-PEAR2_MultiErrors provides a simple and effective mechanism for
-aggregating many errors and organizing them by severity.
+PEAR2_MultiErrors provides a simple and effective mechanism for aggregating many errors and organizing them by severity.
Errors are represented by PHP's built in exception object, and
severity levels are represented by PHP's pre-defined error levels
@@ -9,10 +8,10 @@ be done with iteration, and count() can be used to determine whether errors
were added.
<?php
-include '/path/to/PEAR2/Autoload.php';
-$a = new PEAR2_MultiErrors;
+include '/path/to/pear2/Autoload.php';
+$a = new pear2\MultiErrors;
$a->E_ERROR[] = new Exception('message one');
-$a->E_WARNING[] = new PEAR2_BlahException('message two');
+$a->E_WARNING[] = new pear2\Blah\Exception('message two');
// iterate over all errors
foreach ($a as $error) {...}
// iterate over E_ERROR severity
@@ -23,13 +22,13 @@ count($a->E_WARNING); // 1
count(E_NOTICE); // 0
?>
-Used in conjuction with PEAR2_Exception, it can also be used as a "cause"
+Used in conjuction with pear2\Exception, it can also be used as a "cause"
of an Exception:
<?php
-include '/path/to/PEAR2/Autoload.php';
-$a = new PEAR2_MultiErrors;
+include '/path/to/pear2/Autoload.php';
+$a = new pear2\MultiErrors;
$a->E_ERROR[] = new Exception('message one');
-$a->E_ERROR[] = new PEAR2_BlahException('message two');
-throw new PEAR2_Exception('task failed', $a);
+$a->E_ERROR[] = new pear2\Blah\Exception('message two');
+throw new pear2\My\Exception('task failed', $a);
?>
View
28 package.xml
@@ -2,10 +2,9 @@
<package version="2.1" xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd">
<name>PEAR2_MultiErrors</name>
<channel>pear2.php.net</channel>
- <summary>PEAR2_MultiErrors provides a simple and effective mechanism for
+ <summary>PEAR2_MultiErrors provides a simple and effective mechanism for aggregating many errors and organizing them by severity.
</summary>
- <description>aggregating many errors and organizing them by severity.
-
+ <description>
Errors are represented by PHP's built in exception object, and
severity levels are represented by PHP's pre-defined error levels
E_ERROR, E_WARNING, or E_NOTICE. Adding errors to the PEAR2_MultiErrors
@@ -14,10 +13,10 @@ be done with iteration, and count() can be used to determine whether errors
were added.
&lt;?php
-include '/path/to/PEAR2/Autoload.php';
-$a = new PEAR2_MultiErrors;
+include '/path/to/pear2/Autoload.php';
+$a = new pear2\MultiErrors;
$a-&gt;E_ERROR[] = new Exception('message one');
-$a-&gt;E_WARNING[] = new PEAR2_BlahException('message two');
+$a-&gt;E_WARNING[] = new pear2\Blah\Exception('message two');
// iterate over all errors
foreach ($a as $error) {...}
// iterate over E_ERROR severity
@@ -28,15 +27,15 @@ count($a-&gt;E_WARNING); // 1
count(E_NOTICE); // 0
?&gt;
-Used in conjuction with PEAR2_Exception, it can also be used as a &quot;cause&quot;
+Used in conjuction with pear2\Exception, it can also be used as a &quot;cause&quot;
of an Exception:
&lt;?php
-include '/path/to/PEAR2/Autoload.php';
-$a = new PEAR2_MultiErrors;
+include '/path/to/pear2/Autoload.php';
+$a = new pear2\MultiErrors;
$a-&gt;E_ERROR[] = new Exception('message one');
-$a-&gt;E_ERROR[] = new PEAR2_BlahException('message two');
-throw new PEAR2_Exception('task failed', $a);
+$a-&gt;E_ERROR[] = new pear2\Blah\Exception('message two');
+throw new pear2\My\Exception('task failed', $a);
?&gt;</description>
<lead>
<name>Gregory Beaver</name>
@@ -44,20 +43,21 @@ throw new PEAR2_Exception('task failed', $a);
<email>cellog@php.net</email>
<active>yes</active>
</lead>
- <date>2007-10-05</date>
+ <date>2009-07-07</date>
+ <time>23:21:48</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
</version>
<stability>
- <release>devel</release>
+ <release>alpha</release>
<api>alpha</api>
</stability>
<license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
<notes>Initial Release</notes>
<contents>
<dir name="/">
- <dir name="src" baseinstalldir="PEAR2">
+ <dir name="src" baseinstalldir="pear2">
<dir name="MultiErrors">
<file role="php" name="Exception.php"/>
</dir>
View
80 package_compatible.xml
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package version="2.1" xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd">
+ <name>PEAR2_MultiErrors</name>
+ <channel>pear2.php.net</channel>
+ <summary>PEAR2_MultiErrors provides a simple and effective mechanism for aggregating many errors and organizing them by severity.
+</summary>
+ <description>
+Errors are represented by PHP's built in exception object, and
+severity levels are represented by PHP's pre-defined error levels
+E_ERROR, E_WARNING, or E_NOTICE. Adding errors to the PEAR2_MultiErrors
+object is simple, and done like adding to an array. Retrieving errors can
+be done with iteration, and count() can be used to determine whether errors
+were added.
+
+&lt;?php
+include '/path/to/pear2/Autoload.php';
+$a = new pear2\MultiErrors;
+$a-&gt;E_ERROR[] = new Exception('message one');
+$a-&gt;E_WARNING[] = new pear2\Blah\Exception('message two');
+// iterate over all errors
+foreach ($a as $error) {...}
+// iterate over E_ERROR severity
+foreach ($a-&gt;E_ERROR as $error) {...}
+count($a); // 2
+count($a-&gt;E_ERROR); // 1
+count($a-&gt;E_WARNING); // 1
+count(E_NOTICE); // 0
+?&gt;
+
+Used in conjuction with pear2\Exception, it can also be used as a &quot;cause&quot;
+of an Exception:
+
+&lt;?php
+include '/path/to/pear2/Autoload.php';
+$a = new pear2\MultiErrors;
+$a-&gt;E_ERROR[] = new Exception('message one');
+$a-&gt;E_ERROR[] = new pear2\Blah\Exception('message two');
+throw new pear2\My\Exception('task failed', $a);
+?&gt;</description>
+ <lead>
+ <name>Gregory Beaver</name>
+ <user>cellog</user>
+ <email>cellog@php.net</email>
+ <active>yes</active>
+ </lead>
+ <date>2009-07-07</date>
+ <time>23:21:48</time>
+ <version>
+ <release>0.1.0</release>
+ <api>0.1.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
+ <notes>Initial Release</notes>
+ <contents>
+ <dir name="/">
+ <file role="php" name="php/pear2/MultiErrors/Exception.php"/>
+ <file role="php" name="php/pear2/MultiErrors.php"/>
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>5.2.0</min>
+ </php>
+ <pearinstaller>
+ <min>1.4.8</min>
+ </pearinstaller>
+ </required>
+ </dependencies>
+ <phprelease>
+ <filelist>
+ <install name="php/pear2/MultiErrors.php" as="pear2/MultiErrors.php"/>
+ <install name="php/pear2/MultiErrors/Exception.php" as="pear2/MultiErrors/Exception.php"/>
+ </filelist>
+ </phprelease>
+</package>
View
45 src/MultiErrors.php
@@ -9,7 +9,7 @@
* Usage:
*
* <code>
- * $multi = new PEAR2_MultiErrors();
+ * $multi = new \pear2\MultiErrors();
* $multi->E_WARNING[] = new Exception('test');
* $multi->E_ERROR[] = new Exception('test 2');
* foreach ($multi as $error) {
@@ -22,14 +22,15 @@
* echo $error;
* }
* if (count($multi->E_ERROR)) {
- * throw new \PEAR2_Exception('Failure to do something', $multi);
+ * throw new \pear2\Exception('Failure to do something', $multi);
* }
* </code>
* @copyright 2007 Gregory Beaver
- * @package PEAR2_MultiErrors
+ * @package \pear2\MultiErrors
* @license http://www.php.net/license/3_0.txt PHP License
*/
-class PEAR2_MultiErrors implements Iterator, Countable, ArrayAccess {
+namespace pear2;
+class MultiErrors implements \Iterator, \Countable, \ArrayAccess {
private $_allowedLevels = array('E_NOTICE' => 0, 'E_WARNING' => 1, 'E_ERROR' => 2);
/**
@@ -52,19 +53,19 @@ class PEAR2_MultiErrors implements Iterator, Countable, ArrayAccess {
private $_subMulti = array();
/**
- * Parent PEAR2_MultiErrors for an error level tracker
+ * Parent \pear2\MultiErrors for an error level tracker
*
- * @var PEAR2_MultiErrors
+ * @var \pear2\MultiErrors
*/
private $_parent;
public function __construct($mylevel = false,
array $allowed = array('E_NOTICE', 'E_WARNING', 'E_ERROR'),
- PEAR2_MultiErrors $parent = null)
+ MultiErrors $parent = null)
{
foreach ($allowed as $level) {
if (!is_string($level) || strpos($level, 'E_') !== 0) {
- throw new PEAR2_MultiErrors_Exception('Invalid level ' . (string) $level);
+ throw new MultiErrors\Exception('Invalid level ' . (string) $level);
}
}
$this->_allowedLevels = array_flip($allowed);
@@ -100,12 +101,12 @@ public function valid()
}
/**
- * Merge in errors from an existing PEAR2_MultiErrors
+ * 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
+ * @param \pear2\MultiErrors $error
*/
- public function merge(PEAR2_MultiErrors $error)
+ public function merge(MultiErrors $error)
{
foreach ($error->levels as $level) {
if (!isset($this->_allowedLevels[$level])) {
@@ -138,12 +139,12 @@ public function offsetGet ($offset)
public function offsetSet ($offset, $value)
{
if ($offset === null && !$this->_requestedLevel &&
- $value instanceof PEAR2_MultiErrors ) {
+ $value instanceof self) {
$this->merge($value);
return;
}
- if (!($value instanceof Exception)) {
- throw new PEAR2_MultiErrors_Exception('offsetSet: $value is not an Exception object');
+ if (!($value instanceof \Exception)) {
+ throw new MultiErrors\Exception('offsetSet: $value is not an Exception object');
}
if ($this->_requestedLevel) {
if ($offset === null) {
@@ -151,14 +152,14 @@ public function offsetSet ($offset, $value)
$offset = count($this->_errors);
}
if (!is_int($offset)) {
- throw new PEAR2_MultiErrors_Exception('offsetSet: $offset is not an integer');
+ 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)) {
- throw new PEAR2_MultiErrors_Exception('Cannot add an error directly ' .
- 'to a PEAR2_MultiErrors with $a[] = new Exception, use an ' .
+ 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');
}
$offset = explode('-', $offset);
@@ -168,8 +169,8 @@ public function offsetSet ($offset, $value)
if (!isset($this->_subMulti[$level]) ||
$this->_subMulti[$level][$offset] !== $value) {
// must be in a child or it'll throw off the whole thingy
- throw new PEAR2_MultiErrors_Exception('Cannot add an error directly ' .
- 'to a PEAR2_MultiErrors with $a[] = new Exception, use an ' .
+ 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;
@@ -189,17 +190,17 @@ public function __get($level)
return array_keys($this->_allowedLevels);
}
if (!count($this->_allowedLevels)) {
- throw new PEAR2_MultiErrors_Exception('Cannot nest requests ' .
+ 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 PEAR2_MultiErrors($level,
+ $this->_subMulti[$level] = new self($level,
array(), $this);
}
return $this->_subMulti[$level];
}
- throw new PEAR2_MultiErrors_Exception('Requested error level must be one of ' .
+ throw new MultiErrors\Exception('Requested error level must be one of ' .
implode(', ', array_keys($this->_allowedLevels)));
}
View
3  src/MultiErrors/Exception.php
@@ -1,2 +1,3 @@
<?php
-class PEAR2_MultiErrors_Exception extends \PEAR2_Exception {}
+namespace pear2\MultiErrors;
+class Exception extends \pear2\Exception {}
Please sign in to comment.
Something went wrong with that request. Please try again.