PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/validator/sfValidatorSchema.class.php

http://opac-sbweb.googlecode.com/
PHP | 365 lines | 188 code | 39 blank | 138 comment | 15 complexity | 08e8edb5d0cfe75788d23461429b5c37 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorSchema represents an array of fields.
  11. *
  12. * A field is a named validator.
  13. *
  14. * @package symfony
  15. * @subpackage validator
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: sfValidatorSchema.class.php 10836 2008-08-13 12:06:35Z fabien $
  18. */
  19. class sfValidatorSchema extends sfValidatorBase implements ArrayAccess
  20. {
  21. protected
  22. $fields = array(),
  23. $preValidator = null,
  24. $postValidator = null;
  25. /**
  26. * Constructor.
  27. *
  28. * The first argument can be:
  29. *
  30. * * null
  31. * * an array of named sfValidatorBase instances
  32. *
  33. * @param mixed $fields Initial fields
  34. * @param array $options An array of options
  35. * @param array $messages An array of error messages
  36. *
  37. * @see sfValidatorBase
  38. */
  39. public function __construct($fields = null, $options = array(), $messages = array())
  40. {
  41. if (is_array($fields))
  42. {
  43. foreach ($fields as $name => $validator)
  44. {
  45. $this[$name] = $validator;
  46. }
  47. }
  48. else if (!is_null($fields))
  49. {
  50. throw new InvalidArgumentException('sfValidatorSchema constructor takes an array of sfValidatorBase objects.');
  51. }
  52. parent::__construct($options, $messages);
  53. }
  54. /**
  55. * Configures the validator.
  56. *
  57. * Available options:
  58. *
  59. * * allow_extra_fields: if false, the validator adds an error if extra fields are given in the input array of values (default to false)
  60. * * filter_extra_fields: if true, the validator filters extra fields from the returned array of cleaned values (default to true)
  61. *
  62. * Available error codes:
  63. *
  64. * * extra_fields
  65. *
  66. * @param array $options An array of options
  67. * @param array $messages An array of error messages
  68. *
  69. * @see sfValidatorBase
  70. */
  71. protected function configure($options = array(), $messages = array())
  72. {
  73. $this->addOption('allow_extra_fields', false);
  74. $this->addOption('filter_extra_fields', true);
  75. $this->addMessage('extra_fields', 'Unexpected extra form field named "%field%".');
  76. }
  77. /**
  78. * @see sfValidatorBase
  79. */
  80. public function clean($values)
  81. {
  82. return $this->doClean($values);
  83. }
  84. /**
  85. * @see sfValidatorBase
  86. */
  87. protected function doClean($values)
  88. {
  89. if (is_null($values))
  90. {
  91. $values = array();
  92. }
  93. if (!is_array($values))
  94. {
  95. throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
  96. }
  97. $clean = array();
  98. $unused = array_keys($this->fields);
  99. $errorSchema = new sfValidatorErrorSchema($this);
  100. // pre validator
  101. try
  102. {
  103. $this->preClean($values);
  104. }
  105. catch (sfValidatorErrorSchema $e)
  106. {
  107. $errorSchema->addErrors($e);
  108. }
  109. catch (sfValidatorError $e)
  110. {
  111. $errorSchema->addError($e);
  112. }
  113. // validate given values
  114. foreach ($values as $name => $value)
  115. {
  116. // field exists in our schema?
  117. if (!array_key_exists($name, $this->fields))
  118. {
  119. if (!$this->options['allow_extra_fields'])
  120. {
  121. $errorSchema->addError(new sfValidatorError($this, 'extra_fields', array('field' => $name)));
  122. }
  123. else if (!$this->options['filter_extra_fields'])
  124. {
  125. $clean[$name] = $value;
  126. }
  127. continue;
  128. }
  129. unset($unused[array_search($name, $unused, true)]);
  130. // validate value
  131. try
  132. {
  133. $clean[$name] = $this->fields[$name]->clean($value);
  134. }
  135. catch (sfValidatorError $e)
  136. {
  137. $clean[$name] = null;
  138. $errorSchema->addError($e, (string) $name);
  139. }
  140. }
  141. // are non given values required?
  142. foreach ($unused as $name)
  143. {
  144. // validate value
  145. try
  146. {
  147. $clean[$name] = $this->fields[$name]->clean(null);
  148. }
  149. catch (sfValidatorError $e)
  150. {
  151. $clean[$name] = null;
  152. $errorSchema->addError($e, (string) $name);
  153. }
  154. }
  155. // post validator
  156. try
  157. {
  158. $clean = $this->postClean($clean);
  159. }
  160. catch (sfValidatorErrorSchema $e)
  161. {
  162. $errorSchema->addErrors($e);
  163. }
  164. catch (sfValidatorError $e)
  165. {
  166. $errorSchema->addError($e);
  167. }
  168. if (count($errorSchema))
  169. {
  170. throw $errorSchema;
  171. }
  172. return $clean;
  173. }
  174. /**
  175. * Cleans the input values.
  176. *
  177. * This method is the first validator executed by doClean().
  178. *
  179. * It executes the validator returned by getPreValidator()
  180. * on the global array of values.
  181. *
  182. * @param array $values The input values
  183. *
  184. * @throws sfValidatorError
  185. */
  186. public function preClean($values)
  187. {
  188. if (is_null($validator = $this->getPreValidator()))
  189. {
  190. return;
  191. }
  192. $validator->clean($values);
  193. }
  194. /**
  195. * Cleans the input values.
  196. *
  197. * This method is the last validator executed by doClean().
  198. *
  199. * It executes the validator returned by getPostValidator()
  200. * on the global array of cleaned values.
  201. *
  202. * @param array $values The input values
  203. *
  204. * @throws sfValidatorError
  205. */
  206. public function postClean($values)
  207. {
  208. if (is_null($validator = $this->getPostValidator()))
  209. {
  210. return $values;
  211. }
  212. return $validator->clean($values);
  213. }
  214. /**
  215. * Sets the pre validator.
  216. *
  217. * @param sfValidatorBase $validator An sfValidatorBase instance
  218. */
  219. public function setPreValidator(sfValidatorBase $validator)
  220. {
  221. $this->preValidator = clone $validator;
  222. }
  223. /**
  224. * Returns the pre validator.
  225. *
  226. * @return sfValidatorBase A sfValidatorBase instance
  227. */
  228. public function getPreValidator()
  229. {
  230. return $this->preValidator;
  231. }
  232. /**
  233. * Sets the post validator.
  234. *
  235. * @param sfValidatorBase $validator An sfValidatorBase instance
  236. */
  237. public function setPostValidator(sfValidatorBase $validator)
  238. {
  239. $this->postValidator = clone $validator;
  240. }
  241. /**
  242. * Returns the post validator.
  243. *
  244. * @return sfValidatorBase An sfValidatorBase instance
  245. */
  246. public function getPostValidator()
  247. {
  248. return $this->postValidator;
  249. }
  250. /**
  251. * Returns true if the schema has a field with the given name (implements the ArrayAccess interface).
  252. *
  253. * @param string $name The field name
  254. *
  255. * @return bool true if the schema has a field with the given name, false otherwise
  256. */
  257. public function offsetExists($name)
  258. {
  259. return isset($this->fields[$name]);
  260. }
  261. /**
  262. * Gets the field associated with the given name (implements the ArrayAccess interface).
  263. *
  264. * @param string $name The field name
  265. *
  266. * @return sfValidatorBase The sfValidatorBase instance associated with the given name, null if it does not exist
  267. */
  268. public function offsetGet($name)
  269. {
  270. return isset($this->fields[$name]) ? $this->fields[$name] : null;
  271. }
  272. /**
  273. * Sets a field (implements the ArrayAccess interface).
  274. *
  275. * @param string $name The field name
  276. * @param sfValidatorBase $validator An sfValidatorBase instance
  277. */
  278. public function offsetSet($name, $validator)
  279. {
  280. if (!$validator instanceof sfValidatorBase)
  281. {
  282. throw new InvalidArgumentException('A field must be an instance of sfValidatorBase.');
  283. }
  284. $this->fields[$name] = clone $validator;
  285. }
  286. /**
  287. * Removes a field by name (implements the ArrayAccess interface).
  288. *
  289. * @param string $name
  290. */
  291. public function offsetUnset($name)
  292. {
  293. unset($this->fields[$name]);
  294. }
  295. /**
  296. * Returns an array of fields.
  297. *
  298. * @return sfValidatorBase An array of sfValidatorBase instances
  299. */
  300. public function getFields()
  301. {
  302. return $this->fields;
  303. }
  304. /**
  305. * @see sfValidatorBase
  306. */
  307. public function asString($indent = 0)
  308. {
  309. throw new Exception('Unable to convert a sfValidatorSchema to string.');
  310. }
  311. public function __clone()
  312. {
  313. foreach ($this->fields as $name => $field)
  314. {
  315. $this->fields[$name] = clone $field;
  316. }
  317. if (!is_null($this->preValidator))
  318. {
  319. $this->preValidator = clone $this->preValidator;
  320. }
  321. if (!is_null($this->postValidator))
  322. {
  323. $this->postValidator = clone $this->postValidator;
  324. }
  325. }
  326. }