Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: ErrorStack.php 7490 2010-03-29 19:53:27Z jwage $
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.doctrine-project.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.doctrine-project.org
31
 * @since       1.0
32
 * @version     $Revision: 7490 $
33
 */
34
class Doctrine_Validator_ErrorStack extends Doctrine_Access implements Countable, IteratorAggregate
35
{
36
    /**
37
     * The errors of the error stack.
38
     *
39
     * @var array
40
     */
41
    protected $_errors = array();
42
 
43
    /**
44
     * Array of validators that failed
45
     *
46
     * @var array
47
     */
48
    protected $_validators = array();
49
 
50
    /**
51
     * Get model class name for the error stack
52
     *
53
     * @var string
54
     */
55
    protected $_className;
56
 
57
    /**
58
     * Constructor
59
     *
60
     */
61
    public function __construct($className)
62
    {
63
        $this->_className = $className;
64
    }
65
 
66
    /**
67
     * Adds an error to the stack.
68
     *
69
     * @param string $invalidFieldName
70
     * @param string $errorType
71
     */
72
    public function add($invalidFieldName, $errorCode = 'general')
73
    {
74
        if (is_object($errorCode)) {
75
            if ( ! ($errorCode instanceof Doctrine_Validator_Driver)) {
76
                throw new Doctrine_Exception('Validators must be an instance of Doctrine_Validator_Driver');
77
            }
78
            $validator = $errorCode;
79
            $this->_validators[$invalidFieldName][] = $validator;
80
            $errorCode = (string) $validator;
81
        }
82
 
83
        $this->_errors[$invalidFieldName][] = $errorCode;
84
    }
85
 
86
    /**
87
     * Removes all existing errors for the specified field from the stack.
88
     *
89
     * @param string $fieldName
90
     */
91
    public function remove($fieldName)
92
    {
93
        if (isset($this->_errors[$fieldName])) {
94
            unset($this->_errors[$fieldName]);
95
            if (isset($this->_validators[$fieldName])) {
96
                unset($this->_validators[$fieldName]);
97
            }
98
        }
99
    }
100
 
101
    /**
102
     * Get errors for field
103
     *
104
     * @param string $fieldName
105
     * @return mixed
106
     */
107
    public function get($fieldName)
108
    {
109
        return isset($this->_errors[$fieldName]) ? $this->_errors[$fieldName] : null;
110
    }
111
 
112
    /**
113
     * Alias for add()
114
     *
115
     * @param string $fieldName
116
     * @param string $errorCode
117
     * @return void
118
     */
119
    public function set($fieldName, $errorCode)
120
    {
121
        $this->add($fieldName, $errorCode);
122
    }
123
 
124
    /**
125
     * Check if a field has an error
126
     *
127
     * @param string $fieldName
128
     * @return boolean
129
     */
130
    public function contains($fieldName)
131
    {
132
        return array_key_exists($fieldName, $this->_errors);
133
    }
134
 
135
    /**
136
     * Removes all errors from the stack.
137
     *
138
     * @return void
139
     */
140
    public function clear()
141
    {
142
        $this->_errors = array();
143
        $this->_validators = array();
144
    }
145
 
146
    /**
147
     * Enter description here...
148
     *
149
     * @return unknown
150
     */
151
    public function getIterator()
152
    {
153
        return new ArrayIterator($this->_errors);
154
    }
155
 
156
    public function toArray()
157
    {
158
        return $this->_errors;
159
    }
160
 
161
    /**
162
     * Count the number of errors
163
     *
164
     * @return integer
165
     */
166
    public function count()
167
    {
168
        return count($this->_errors);
169
    }
170
 
171
    /**
172
     * Get the classname where the errors occured
173
     *
174
     * @return string
175
     */
176
    public function getClassname()
177
    {
178
        return $this->_className;
179
    }
180
 
181
    /**
182
     * Get array of failed validators
183
     *
184
     * @return array
185
     */
186
    public function getValidators()
187
    {
188
        return $this->_validators;
189
    }
190
}