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

/oiclient/data/symfony/validator/sfValidatorBase.class.php

http://openirudi.googlecode.com/
PHP | 455 lines | 186 code | 49 blank | 220 comment | 13 complexity | cacf40c40a734d36f4f8e592ccf768d4 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. * sfValidatorBase is the base class for all validators.
  11. *
  12. * It also implements the required option for all validators.
  13. *
  14. * @package symfony
  15. * @subpackage validator
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: sfValidatorBase.class.php 10893 2008-08-14 12:30:27Z hartym $
  18. */
  19. abstract class sfValidatorBase
  20. {
  21. protected static
  22. $charset = 'UTF-8';
  23. protected
  24. $requiredOptions = array(),
  25. $defaultMessages = array(),
  26. $defaultOptions = array(),
  27. $messages = array(),
  28. $options = array();
  29. /**
  30. * Constructor.
  31. *
  32. * Available options:
  33. *
  34. * * required: true if the value is required, false otherwise (default to true)
  35. * * trim: true if the value must be trimmed, false otherwise (default to false)
  36. * * empty_value: empty value when value is not required
  37. *
  38. * Available error codes:
  39. *
  40. * * required
  41. * * invalid
  42. *
  43. * @param array $options An array of options
  44. * @param array $messages An array of error messages
  45. */
  46. public function __construct($options = array(), $messages = array())
  47. {
  48. $this->options = array_merge(array('required' => true, 'trim' => false, 'empty_value' => null), $this->options);
  49. $this->messages = array_merge(array('required' => 'Required.', 'invalid' => 'Invalid.'), $this->messages);
  50. $this->configure($options, $messages);
  51. $this->setDefaultOptions($this->getOptions());
  52. $this->setDefaultMessages($this->getMessages());
  53. // check option names
  54. if ($diff = array_diff(array_keys($options), array_merge(array_keys($this->options), $this->requiredOptions)))
  55. {
  56. throw new InvalidArgumentException(sprintf('%s does not support the following options: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  57. }
  58. // check error code names
  59. if ($diff = array_diff(array_keys($messages), array_keys($this->messages)))
  60. {
  61. throw new InvalidArgumentException(sprintf('%s does not support the following error codes: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  62. }
  63. // check required options
  64. if ($diff = array_diff($this->requiredOptions, array_merge(array_keys($this->options), array_keys($options))))
  65. {
  66. throw new RuntimeException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  67. }
  68. $this->options = array_merge($this->options, $options);
  69. $this->messages = array_merge($this->messages, $messages);
  70. }
  71. /**
  72. * Configures the current validator.
  73. *
  74. * This method allows each validator to add options and error messages
  75. * during validator creation.
  76. *
  77. * If some options and messages are given in the sfValidatorBase constructor
  78. * they will take precedence over the options and messages you configure
  79. * in this method.
  80. *
  81. * @param array $options An array of options
  82. * @param array $messages An array of error messages
  83. *
  84. * @see __construct()
  85. */
  86. protected function configure($options = array(), $messages = array())
  87. {
  88. }
  89. /**
  90. * Returns an error message given an error code.
  91. *
  92. * @param string $name The error code
  93. *
  94. * @return string The error message, or the empty string if the error code does not exist
  95. */
  96. public function getMessage($name)
  97. {
  98. return isset($this->messages[$name]) ? $this->messages[$name] : '';
  99. }
  100. /**
  101. * Adds a new error code with a default error message.
  102. *
  103. * @param string $name The error code
  104. * @param string $value The error message
  105. */
  106. public function addMessage($name, $value)
  107. {
  108. $this->messages[$name] = $value;
  109. }
  110. /**
  111. * Changes an error message given the error code.
  112. *
  113. * @param string $name The error code
  114. * @param string $value The error message
  115. */
  116. public function setMessage($name, $value)
  117. {
  118. if (!in_array($name, array_keys($this->messages)))
  119. {
  120. throw new InvalidArgumentException(sprintf('%s does not support the following error code: \'%s\'.', get_class($this), $name));
  121. }
  122. $this->messages[$name] = $value;
  123. }
  124. /**
  125. * Returns an array of current error messages.
  126. *
  127. * @return array An array of messages
  128. */
  129. public function getMessages()
  130. {
  131. return $this->messages;
  132. }
  133. /**
  134. * Changes all error messages.
  135. *
  136. * @param array $values An array of error messages
  137. */
  138. public function setMessages($values)
  139. {
  140. $this->messages = $values;
  141. }
  142. /**
  143. * Gets an option value.
  144. *
  145. * @param string $name The option name
  146. *
  147. * @return mixed The option value
  148. */
  149. public function getOption($name)
  150. {
  151. return isset($this->options[$name]) ? $this->options[$name] : null;
  152. }
  153. /**
  154. * Adds a new option value with a default value.
  155. *
  156. * @param string $name The option name
  157. * @param mixed $value The default value
  158. */
  159. public function addOption($name, $value = null)
  160. {
  161. $this->options[$name] = $value;
  162. }
  163. /**
  164. * Changes an option value.
  165. *
  166. * @param string $name The option name
  167. * @param mixed $value The value
  168. */
  169. public function setOption($name, $value)
  170. {
  171. if (!in_array($name, array_merge(array_keys($this->options), $this->requiredOptions)))
  172. {
  173. throw new InvalidArgumentException(sprintf('%s does not support the following option: \'%s\'.', get_class($this), $name));
  174. }
  175. $this->options[$name] = $value;
  176. }
  177. /**
  178. * Returns true if the option exists.
  179. *
  180. * @param string $name The option name
  181. *
  182. * @return bool true if the option exists, false otherwise
  183. */
  184. public function hasOption($name)
  185. {
  186. return isset($this->options[$name]);
  187. }
  188. /**
  189. * Returns all options.
  190. *
  191. * @return array An array if options
  192. */
  193. public function getOptions()
  194. {
  195. return $this->options;
  196. }
  197. /**
  198. * Changes all options.
  199. *
  200. * @param array $values An array if options
  201. */
  202. public function setOptions($values)
  203. {
  204. $this->options = $values;
  205. }
  206. /**
  207. * Adds a required option.
  208. *
  209. * @param string $name The option name
  210. */
  211. public function addRequiredOption($name)
  212. {
  213. $this->requiredOptions[] = $name;
  214. }
  215. /**
  216. * Returns all required option names.
  217. *
  218. * @param array An array of required option names
  219. */
  220. public function getRequiredOptions()
  221. {
  222. return $this->requiredOptions;
  223. }
  224. /**
  225. * Cleans the input value.
  226. *
  227. * This method is also responsible for trimming the input value
  228. * and checking the required option.
  229. *
  230. * @param mixed $value The input value
  231. *
  232. * @return mixed The cleaned value
  233. *
  234. * @throws sfValidatorError
  235. */
  236. public function clean($value)
  237. {
  238. $clean = $value;
  239. if ($this->options['trim'] && is_string($clean))
  240. {
  241. $clean = trim($clean);
  242. }
  243. // empty value?
  244. if ($this->isEmpty($clean))
  245. {
  246. // required?
  247. if ($this->options['required'])
  248. {
  249. throw new sfValidatorError($this, 'required');
  250. }
  251. return $this->getEmptyValue();
  252. }
  253. return $this->doClean($clean);
  254. }
  255. /**
  256. * Cleans the input value.
  257. *
  258. * Every subclass must implements this method.
  259. *
  260. * @param mixed $value The input value
  261. *
  262. * @return mixed The cleaned value
  263. *
  264. * @throws sfValidatorError
  265. */
  266. abstract protected function doClean($value);
  267. /**
  268. * Sets the charset to use when validating strings.
  269. *
  270. * @param string $charset The charset
  271. */
  272. static public function setCharset($charset)
  273. {
  274. self::$charset = $charset;
  275. }
  276. /**
  277. * Returns the charset to use when validating strings.
  278. *
  279. * @return string The charset (default to UTF-8)
  280. */
  281. static public function getCharset()
  282. {
  283. return self::$charset;
  284. }
  285. /**
  286. * Returns true if the value is empty.
  287. *
  288. * @param mixed $value The input value
  289. *
  290. * @return bool true if the value is empty, false otherwise
  291. */
  292. protected function isEmpty($value)
  293. {
  294. return in_array($value, array(null, '', array()), true);
  295. }
  296. /**
  297. * Returns an empty value for this validator.
  298. *
  299. * @return mixed The empty value for this validator
  300. */
  301. protected function getEmptyValue()
  302. {
  303. return $this->getOption('empty_value');
  304. }
  305. /**
  306. * Returns an array of all error codes for this validator.
  307. *
  308. * @return array An array of possible error codes
  309. *
  310. * @see getDefaultMessages()
  311. */
  312. final public function getErrorCodes()
  313. {
  314. return array_keys($this->getDefaultMessages());
  315. }
  316. /**
  317. * Returns default messages for all possible error codes.
  318. *
  319. * @return array An array of default error codes and messages
  320. */
  321. public function getDefaultMessages()
  322. {
  323. return $this->defaultMessages;
  324. }
  325. /**
  326. * Sets default messages for all possible error codes.
  327. *
  328. * @param array $messages An array of default error codes and messages
  329. */
  330. protected function setDefaultMessages($messages)
  331. {
  332. $this->defaultMessages = $messages;
  333. }
  334. /**
  335. * Returns default option values.
  336. *
  337. * @return array An array of default option values
  338. */
  339. public function getDefaultOptions()
  340. {
  341. return $this->defaultOptions;
  342. }
  343. /**
  344. * Sets default option values.
  345. *
  346. * @param array $options An array of default option values
  347. */
  348. protected function setDefaultOptions($options)
  349. {
  350. $this->defaultOptions = $options;
  351. }
  352. /**
  353. * Returns a string representation of this validator.
  354. *
  355. * @param int $indent Indentation (number of spaces before each line)
  356. *
  357. * @return string The string representation of the validator
  358. */
  359. public function asString($indent = 0)
  360. {
  361. $options = $this->getOptionsWithoutDefaults();
  362. $messages = $this->getMessagesWithoutDefaults();
  363. return sprintf('%s%s(%s%s)',
  364. str_repeat(' ', $indent),
  365. str_replace('sfValidator', '', get_class($this)),
  366. $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
  367. $messages ? ', '.sfYamlInline::dump($messages) : ''
  368. );
  369. }
  370. /**
  371. * Returns all error messages with non default values.
  372. *
  373. * @return string A string representation of the error messages
  374. */
  375. protected function getMessagesWithoutDefaults()
  376. {
  377. $messages = $this->messages;
  378. // remove default option values
  379. foreach ($this->getDefaultMessages() as $key => $value)
  380. {
  381. if (array_key_exists($key, $messages) && $messages[$key] === $value)
  382. {
  383. unset($messages[$key]);
  384. }
  385. }
  386. return $messages;
  387. }
  388. /**
  389. * Returns all options with non default values.
  390. *
  391. * @return string A string representation of the options
  392. */
  393. protected function getOptionsWithoutDefaults()
  394. {
  395. $options = $this->options;
  396. // remove default option values
  397. foreach ($this->getDefaultOptions() as $key => $value)
  398. {
  399. if (array_key_exists($key, $options) && $options[$key] === $value)
  400. {
  401. unset($options[$key]);
  402. }
  403. }
  404. return $options;
  405. }
  406. }