1. sfValidatorFromDescription.class.php
  2. /** * sfValidatorFromDescription converts a string to a validator. * * @package symfony * @subpackage validator * @author Fabien Potencier * @version SVN: $Id: sfValidatorFromDescription.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfValidatorFromDescription extends sfValidatorDecorator
  4. {
  5. protected
  6. $tokens = array(),
  7. $string = '';
  8. /**
  9. * @see sfValidatorBase
  10. */
  11. public function __construct($string, $options = array(), $messages = array())
  12. {
  13. $this->string = $string;
  14. $this->tokens = $this->tokenize($string);
  15. parent::__construct($options, $messages);
  16. }
  17. /**
  18. * Returns a PHP representation for the validator.
  19. *
  20. * This PHP representation can be evaled to return the object validator.
  21. *
  22. * This is mainly useful to cache the result of the validator string parsing.
  23. *
  24. * @return string The PHP representation for the validator
  25. */
  26. public function asPhp()
  27. {
  28. return $this->reduceTokens($this->tokens, 'asPhp');
  29. }
  30. /**
  31. * @see sfValidatorDecorator
  32. */
  33. public function getValidator()
  34. {
  35. if (null === $this->validator)
  36. {
  37. $this->validator = $this->reduceTokens($this->tokens, 'getValidator');
  38. }
  39. return $this->validator;
  40. }
  41. /**
  42. * Tokenizes a validator string to a list of tokens in RPN.
  43. *
  44. * @param string $string A validator string
  45. *
  46. * @return array An array of tokens
  47. */
  48. protected function tokenize($string)
  49. {
  50. $tokens = array();
  51. $len = strlen($string);
  52. $i = 0;
  53. while ($i < $len)
  54. {
  55. if (preg_match('/^([a-z0-9_\-]+)\s*(<=|>=|<|>|==|!=)/i', substr($string, $i), $match))
  56. {
  57. // schema compare validator
  58. $i += strlen($match[0]);
  59. $leftField = $match[1];
  60. $operator = $match[2];
  61. // arguments (optional)
  62. $arguments = $this->parseArguments($string, $i);
  63. // rightField
  64. if (!preg_match('/\s*([a-z0-9_\-]+)/', substr($string, $i), $match))
  65. {
  66. throw new DomainException('Parsing problem.');
  67. }
  68. $i += strlen($match[0]);
  69. $rightField = $match[1];
  70. $tokens[] = new sfValidatorFDToken('sfValidatorSchemaCompare', array($leftField, $operator, $rightField, $arguments[0], isset($arguments[1]) ? $arguments[1] : array()));
  71. }
  72. else if (preg_match('/^(and|or)/i', substr($string, $i), $match))
  73. {
  74. // all, any validador
  75. $i += strlen($match[0]);
  76. // arguments (optional)
  77. $arguments = $this->parseArguments($string, $i);
  78. $tokens[] = new sfValidatorFDTokenOperator(strtolower($match[1]), $arguments);
  79. }
  80. else if (preg_match('/^(?:([a-z0-9_\-]+)\:)?([a-z0-9_\-]+)/i', substr($string, $i), $match))
  81. {
  82. // single validator (optionally filtered)
  83. $i += strlen($match[0]);
  84. $class = 'sfValidator'.$match[2];
  85. $arguments = $this->parseArguments($string, $i);
  86. $token = new sfValidatorFDToken($class, array($arguments[0], isset($arguments[1]) ? $arguments[1] : array()));
  87. if ($match[1])
  88. {
  89. $token = new sfValidatorFDTokenFilter($match[1], $token);
  90. }
  91. $tokens[] = $token;
  92. }
  93. else if ('(' == $string[$i])
  94. {
  95. $tokens[] = new sfValidatorFDTokenLeftBracket();
  96. ++$i;
  97. }
  98. else if (')' == $string[$i])
  99. {
  100. $tokens[] = new sfValidatorFDTokenRightBracket();
  101. ++$i;
  102. }
  103. else if (in_array($string[$i], array(' ', "\t", "\r", "\n")))
  104. {
  105. ++$i;
  106. }
  107. else
  108. {
  109. throw new DomainException(sprintf('Unable to parse string (%s).', $string));
  110. }
  111. }
  112. return $this->convertInfixToRpn($tokens);
  113. }
  114. /**
  115. * Parses validator arguments.
  116. *
  117. * @param string $string The string to parse
  118. * @param integer $i The indice to start the parsing
  119. *
  120. * @return array An array of parameters
  121. */
  122. protected function parseArguments($string, &$i)
  123. {
  124. $len = strlen($string);
  125. if ($i + 1 > $len || '(' != $string[$i])
  126. {
  127. return array(array(), array());
  128. }
  129. ++$i;
  130. $args = '';
  131. $opened = 0;
  132. while ($i < $len)
  133. {
  134. if ('(' == $string[$i])
  135. {
  136. ++$opened;
  137. }
  138. else if (')' == $string[$i])
  139. {
  140. if (!$opened)
  141. {
  142. break;
  143. }
  144. --$opened;
  145. }
  146. $args .= $string[$i++];
  147. }
  148. ++$i;
  149. return sfYamlInline::load('['.(!$args ? '{}' : $args).']');
  150. }
  151. /**
  152. * Converts a token array from an infix notation to a RPN.
  153. *
  154. * @param array $tokens An array of tokens in infix notation
  155. *
  156. * @return array An array of token in RPN
  157. */
  158. protected function convertInfixToRpn($tokens)
  159. {
  160. $outputStack = array();
  161. $operatorStack = array();
  162. $precedences = array('and' => 2, 'or' => 1, '(' => 0);
  163. // based on the shunting yard algorithm
  164. foreach ($tokens as $token)
  165. {
  166. switch (get_class($token))
  167. {
  168. case 'sfValidatorFDToken':
  169. $outputStack[] = $token;
  170. break;
  171. case 'sfValidatorFDTokenLeftBracket':
  172. $operatorStack[] = $token;
  173. break;
  174. case 'sfValidatorFDTokenRightBracket':
  175. while (!$operatorStack[count($operatorStack) - 1] instanceof sfValidatorFDTokenLeftBracket)
  176. {
  177. $outputStack[] = array_pop($operatorStack);
  178. }
  179. array_pop($operatorStack);
  180. break;
  181. case 'sfValidatorFDTokenOperator':
  182. while (count($operatorStack) && $precedences[$token->__toString()] <= $precedences[$operatorStack[count($operatorStack) - 1]->__toString()])
  183. {
  184. $outputStack[] = array_pop($operatorStack);
  185. }
  186. $operatorStack[] = $token;
  187. break;
  188. default:
  189. $outputStack[] = $token;
  190. }
  191. }
  192. while (count($operatorStack))
  193. {
  194. $token = array_pop($operatorStack);
  195. if ($token instanceof sfValidatorFDTokenLeftBracket || $token instanceof sfValidatorFDTokenRightBracket)
  196. {
  197. throw new DomainException(sprintf('Uneven parenthesis in string (%s).', $this->string));
  198. }
  199. $outputStack[] = $token;
  200. }
  201. return $outputStack;
  202. }
  203. /**
  204. * Reduces tokens to a single token and convert it with the given method.
  205. *
  206. * @param array $tokens An array of tokens
  207. * @param string $method The method name to execute on each token
  208. *
  209. * @return mixed A single validator representation
  210. */
  211. protected function reduceTokens($tokens, $method)
  212. {
  213. if (1 == count($tokens))
  214. {
  215. return $tokens[0]->$method();
  216. }
  217. // reduce to a single validator
  218. while (count($tokens) > 1)
  219. {
  220. $i = 0;
  221. while (isset($tokens[$i]) && !$tokens[$i] instanceof sfValidatorFDTokenOperator)
  222. {
  223. $i++;
  224. }
  225. $tokens[$i] = $tokens[$i]->$method($tokens[$i - 2], $tokens[$i - 1]);
  226. unset($tokens[$i - 1], $tokens[$i - 2]);
  227. $tokens = array_values($tokens);
  228. }
  229. return $tokens[0];
  230. }
  231. }

Debug toolbar