ExprBuilder.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Definition\Builder;
  11. use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
  12. /**
  13. * This class builds an if expression.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. * @author Christophe Coevoet <stof@notk.org>
  17. */
  18. class ExprBuilder
  19. {
  20. protected $node;
  21. public $ifPart;
  22. public $thenPart;
  23. /**
  24. * Constructor
  25. *
  26. * @param NodeDefinition $node The related node
  27. */
  28. public function __construct(NodeDefinition $node)
  29. {
  30. $this->node = $node;
  31. }
  32. /**
  33. * Mark the expression as being always used.
  34. *
  35. * @return ExprBuilder
  36. */
  37. public function always(\Closure $then = null)
  38. {
  39. $this->ifPart = function($v) { return true; };
  40. if (null !== $then) {
  41. $this->thenPart = $then;
  42. }
  43. return $this;
  44. }
  45. /**
  46. * Sets a closure to use as tests.
  47. *
  48. * The default one tests if the value is true.
  49. *
  50. * @param \Closure $closure
  51. * @return ExprBuilder
  52. */
  53. public function ifTrue(\Closure $closure = null)
  54. {
  55. if (null === $closure) {
  56. $closure = function($v) { return true === $v; };
  57. }
  58. $this->ifPart = $closure;
  59. return $this;
  60. }
  61. /**
  62. * Tests if the value is a string.
  63. *
  64. * @return ExprBuilder
  65. */
  66. public function ifString()
  67. {
  68. $this->ifPart = function($v) { return is_string($v); };
  69. return $this;
  70. }
  71. /**
  72. * Tests if the value is null.
  73. *
  74. * @return ExprBuilder
  75. */
  76. public function ifNull()
  77. {
  78. $this->ifPart = function($v) { return null === $v; };
  79. return $this;
  80. }
  81. /**
  82. * Tests if the value is an array.
  83. *
  84. * @return ExprBuilder
  85. */
  86. public function ifArray()
  87. {
  88. $this->ifPart = function($v) { return is_array($v); };
  89. return $this;
  90. }
  91. /**
  92. * Tests if the value is in an array.
  93. *
  94. * @param array $array
  95. *
  96. * @return ExprBuilder
  97. */
  98. public function ifInArray(array $array)
  99. {
  100. $this->ifPart = function($v) use ($array) { return in_array($v, $array, true); };
  101. return $this;
  102. }
  103. /**
  104. * Tests if the value is not in an array.
  105. *
  106. * @param array $array
  107. *
  108. * @return ExprBuilder
  109. */
  110. public function ifNotInArray(array $array)
  111. {
  112. $this->ifPart = function($v) use ($array) { return !in_array($v, $array, true); };
  113. return $this;
  114. }
  115. /**
  116. * Sets the closure to run if the test pass.
  117. *
  118. * @param \Closure $closure
  119. *
  120. * @return ExprBuilder
  121. */
  122. public function then(\Closure $closure)
  123. {
  124. $this->thenPart = $closure;
  125. return $this;
  126. }
  127. /**
  128. * Sets a closure returning an empty array.
  129. *
  130. * @return ExprBuilder
  131. */
  132. public function thenEmptyArray()
  133. {
  134. $this->thenPart = function($v) { return array(); };
  135. return $this;
  136. }
  137. /**
  138. * Sets a closure marking the value as invalid at validation time.
  139. *
  140. * if you want to add the value of the node in your message just use a %s placeholder.
  141. *
  142. * @param string $message
  143. *
  144. * @return ExprBuilder
  145. */
  146. public function thenInvalid($message)
  147. {
  148. $this->thenPart = function ($v) use ($message) {throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
  149. return $this;
  150. }
  151. /**
  152. * Sets a closure unsetting this key of the array at validation time.
  153. *
  154. * @return ExprBuilder
  155. */
  156. public function thenUnset()
  157. {
  158. $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
  159. return $this;
  160. }
  161. /**
  162. * Returns the related node
  163. *
  164. * @return NodeDefinition
  165. */
  166. public function end()
  167. {
  168. if (null === $this->ifPart) {
  169. throw new \RuntimeException('You must specify an if part.');
  170. }
  171. if (null === $this->thenPart) {
  172. throw new \RuntimeException('You must specify a then part.');
  173. }
  174. return $this->node;
  175. }
  176. /**
  177. * Builds the expressions.
  178. *
  179. * @param array $expressions An array of ExprBuilder instances to build
  180. *
  181. * @return array
  182. */
  183. public static function buildExpressions(array $expressions)
  184. {
  185. foreach ($expressions as $k => $expr) {
  186. if ($expr instanceof ExprBuilder) {
  187. $expressions[$k] = function($v) use($expr) {
  188. return call_user_func($expr->ifPart, $v) ? call_user_func($expr->thenPart, $v) : $v;
  189. };
  190. }
  191. }
  192. return $expressions;
  193. }
  194. }