PageRenderTime 52ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/Validator/ErrorStack.php

https://github.com/ascorbic/doctrine-tracker
PHP | 190 lines | 69 code | 16 blank | 105 comment | 4 complexity | 96a82af8ef312d91aa0ce56e3a6896d4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * $Id$
  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. * Doctrine_Validator_ErrorStack
  23. *
  24. * @package Doctrine
  25. * @subpackage Validator
  26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  27. * @author Roman Borschel <roman@code-factory.org>
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.phpdoctrine.org
  30. * @since 1.0
  31. * @version $Revision$
  32. */
  33. class Doctrine_Validator_ErrorStack extends Doctrine_Access implements Countable, IteratorAggregate
  34. {
  35. /**
  36. * The errors of the error stack.
  37. *
  38. * @var array
  39. */
  40. protected $_errors = array();
  41. /**
  42. * Array of validators that failed
  43. *
  44. * @var array
  45. */
  46. protected $_validators = array();
  47. /**
  48. * Get model class name for the error stack
  49. *
  50. * @var string
  51. */
  52. protected $_className;
  53. /**
  54. * Constructor
  55. *
  56. */
  57. public function __construct($className)
  58. {
  59. $this->_className = $className;
  60. }
  61. /**
  62. * Adds an error to the stack.
  63. *
  64. * @param string $invalidFieldName
  65. * @param string $errorType
  66. */
  67. public function add($invalidFieldName, $errorCode = 'general')
  68. {
  69. if (is_object($errorCode)) {
  70. if ( ! ($errorCode instanceof Doctrine_Validator_Driver)) {
  71. throw new Doctrine_Exception('Validators must be an instance of Doctrine_Validator_Driver');
  72. }
  73. $validator = $errorCode;
  74. $this->_validators[$invalidFieldName][] = $validator;
  75. $errorCode = (string) $validator;
  76. }
  77. $this->_errors[$invalidFieldName][] = $errorCode;
  78. }
  79. /**
  80. * Removes all existing errors for the specified field from the stack.
  81. *
  82. * @param string $fieldName
  83. */
  84. public function remove($fieldName)
  85. {
  86. if (isset($this->_errors[$fieldName])) {
  87. unset($this->_errors[$fieldName]);
  88. if (isset($this->_validators[$fieldName])) {
  89. unset($this->_validators[$fieldName]);
  90. }
  91. }
  92. }
  93. /**
  94. * Get errors for field
  95. *
  96. * @param string $fieldName
  97. * @return mixed
  98. */
  99. public function get($fieldName)
  100. {
  101. return isset($this->_errors[$fieldName]) ? $this->_errors[$fieldName] : null;
  102. }
  103. /**
  104. * Alias for add()
  105. *
  106. * @param string $fieldName
  107. * @param string $errorCode
  108. * @return void
  109. */
  110. public function set($fieldName, $errorCode)
  111. {
  112. $this->add($fieldName, $errorCode);
  113. }
  114. /**
  115. * Check if a field has an error
  116. *
  117. * @param string $fieldName
  118. * @return boolean
  119. */
  120. public function contains($fieldName)
  121. {
  122. return array_key_exists($fieldName, $this->_errors);
  123. }
  124. /**
  125. * Removes all errors from the stack.
  126. *
  127. * @return void
  128. */
  129. public function clear()
  130. {
  131. $this->_errors = array();
  132. $this->_validators = array();
  133. }
  134. /**
  135. * Enter description here...
  136. *
  137. * @return unknown
  138. */
  139. public function getIterator()
  140. {
  141. return new ArrayIterator($this->_errors);
  142. }
  143. public function toArray()
  144. {
  145. return $this->_errors;
  146. }
  147. /**
  148. * Count the number of errors
  149. *
  150. * @return integer
  151. */
  152. public function count()
  153. {
  154. return count($this->_errors);
  155. }
  156. /**
  157. * Get the classname where the errors occured
  158. *
  159. * @return string
  160. */
  161. public function getClassname()
  162. {
  163. return $this->_className;
  164. }
  165. /**
  166. * Get array of failed validators
  167. *
  168. * @return array
  169. */
  170. public function getValidators()
  171. {
  172. return $this->_validators;
  173. }
  174. }