| 1 | <?php |
| 2 | /* |
| 3 | * $Id: ErrorStack.php 3157 2007-11-14 21:06:30Z zYne $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Validator_ErrorStack |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Validator |
| 27 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 28 | * @author Roman Borschel <roman@code-factory.org> |
| 29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 30 | * @link www.phpdoctrine.org |
| 31 | * @since 1.0 |
| 32 | * @version $Revision: 3157 $ |
| 33 | */ |
| 34 | class Doctrine_Validator_ErrorStack extends Doctrine_Access implements Countable, IteratorAggregate |
| 35 | { |
| 36 | |
| 37 | /** |
| 38 | * The errors of the error stack. |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | protected $errors = array(); |
| 43 | |
| 44 | protected $classname = ""; |
| 45 | |
| 46 | /** |
| 47 | * Constructor |
| 48 | * |
| 49 | */ |
| 50 | public function __construct($classname = "") |
| 51 | { |
| 52 | $this->classname = $classname; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Adds an error to the stack. |
| 57 | * |
| 58 | * @param string $invalidFieldName |
| 59 | * @param string $errorType |
| 60 | */ |
| 61 | public function add($invalidFieldName, $errorCode = 'general') |
| 62 | { |
| 63 | $this->errors[$invalidFieldName][] = $errorCode; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Removes all existing errors for the specified field from the stack. |
| 68 | * |
| 69 | * @param string $fieldName |
| 70 | */ |
| 71 | public function remove($fieldName) |
| 72 | { |
| 73 | if (isset($this->errors[$fieldName])) { |
| 74 | unset($this->errors[$fieldName]); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Enter description here... |
| 80 | * |
| 81 | * @param unknown_type $name |
| 82 | * @return unknown |
| 83 | */ |
| 84 | public function get($fieldName) |
| 85 | { |
| 86 | return isset($this->errors[$fieldName]) ? $this->errors[$fieldName] : null; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Enter description here... |
| 91 | * |
| 92 | * @param unknown_type $name |
| 93 | */ |
| 94 | public function set($fieldName, $errorCode) |
| 95 | { |
| 96 | $this->add($fieldName, $errorCode); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Enter description here... |
| 101 | * |
| 102 | * @return unknown |
| 103 | */ |
| 104 | public function contains($fieldName) |
| 105 | { |
| 106 | return array_key_exists($fieldName, $this->errors); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Removes all errors from the stack. |
| 111 | */ |
| 112 | public function clear() |
| 113 | { |
| 114 | $this->errors = array(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Enter description here... |
| 119 | * |
| 120 | * @return unknown |
| 121 | */ |
| 122 | public function getIterator() |
| 123 | { |
| 124 | return new ArrayIterator($this->errors); |
| 125 | } |
| 126 | |
| 127 | public function toArray() |
| 128 | { |
| 129 | return $this->errors; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Enter description here... |
| 134 | * |
| 135 | * @return unknown |
| 136 | */ |
| 137 | public function count() |
| 138 | { |
| 139 | return count($this->errors); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the classname where the errors occured |
| 144 | * |
| 145 | */ |
| 146 | public function getClassname(){ |
| 147 | return $this->classname; |
| 148 | } |
| 149 | } |