vendor/symfony/config/Definition/Builder/ExprBuilder.php line 38

Open in your IDE?
  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.     public function __construct(NodeDefinition $node)
  24.     {
  25.         $this->node = $node;
  26.     }
  27.     /**
  28.      * Marks the expression as being always used.
  29.      *
  30.      * @return $this
  31.      */
  32.     public function always(\Closure $then = null): static
  33.     {
  34.         $this->ifPart = function () { return true; };
  35.         if (null !== $then) {
  36.             $this->thenPart = $then;
  37.         }
  38.         return $this;
  39.     }
  40.     /**
  41.      * Sets a closure to use as tests.
  42.      *
  43.      * The default one tests if the value is true.
  44.      *
  45.      * @return $this
  46.      */
  47.     public function ifTrue(\Closure $closure = null): static
  48.     {
  49.         if (null === $closure) {
  50.             $closure = function ($v) { return true === $v; };
  51.         }
  52.         $this->ifPart = $closure;
  53.         return $this;
  54.     }
  55.     /**
  56.      * Tests if the value is a string.
  57.      *
  58.      * @return $this
  59.      */
  60.     public function ifString(): static
  61.     {
  62.         $this->ifPart = function ($v) { return \is_string($v); };
  63.         return $this;
  64.     }
  65.     /**
  66.      * Tests if the value is null.
  67.      *
  68.      * @return $this
  69.      */
  70.     public function ifNull(): static
  71.     {
  72.         $this->ifPart = function ($v) { return null === $v; };
  73.         return $this;
  74.     }
  75.     /**
  76.      * Tests if the value is empty.
  77.      */
  78.     public function ifEmpty(): self
  79.     {
  80.         $this->ifPart = function ($v) { return empty($v); };
  81.         return $this;
  82.     }
  83.     /**
  84.      * Tests if the value is an array.
  85.      *
  86.      * @return $this
  87.      */
  88.     public function ifArray(): static
  89.     {
  90.         $this->ifPart = function ($v) { return \is_array($v); };
  91.         return $this;
  92.     }
  93.     /**
  94.      * Tests if the value is in an array.
  95.      *
  96.      * @return $this
  97.      */
  98.     public function ifInArray(array $array): static
  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.      * @return $this
  107.      */
  108.     public function ifNotInArray(array $array): static
  109.     {
  110.         $this->ifPart = function ($v) use ($array) { return !\in_array($v, $array, true); };
  111.         return $this;
  112.     }
  113.     /**
  114.      * Transforms variables of any type into an array.
  115.      *
  116.      * @return $this
  117.      */
  118.     public function castToArray(): static
  119.     {
  120.         $this->ifPart = function ($v) { return !\is_array($v); };
  121.         $this->thenPart = function ($v) { return [$v]; };
  122.         return $this;
  123.     }
  124.     /**
  125.      * Sets the closure to run if the test pass.
  126.      *
  127.      * @return $this
  128.      */
  129.     public function then(\Closure $closure): static
  130.     {
  131.         $this->thenPart = $closure;
  132.         return $this;
  133.     }
  134.     /**
  135.      * Sets a closure returning an empty array.
  136.      *
  137.      * @return $this
  138.      */
  139.     public function thenEmptyArray(): static
  140.     {
  141.         $this->thenPart = function () { return []; };
  142.         return $this;
  143.     }
  144.     /**
  145.      * Sets a closure marking the value as invalid at processing time.
  146.      *
  147.      * if you want to add the value of the node in your message just use a %s placeholder.
  148.      *
  149.      * @return $this
  150.      *
  151.      * @throws \InvalidArgumentException
  152.      */
  153.     public function thenInvalid(string $message): static
  154.     {
  155.         $this->thenPart = function ($v) use ($message) { throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
  156.         return $this;
  157.     }
  158.     /**
  159.      * Sets a closure unsetting this key of the array at processing time.
  160.      *
  161.      * @return $this
  162.      *
  163.      * @throws UnsetKeyException
  164.      */
  165.     public function thenUnset(): static
  166.     {
  167.         $this->thenPart = function () { throw new UnsetKeyException('Unsetting key.'); };
  168.         return $this;
  169.     }
  170.     /**
  171.      * Returns the related node.
  172.      *
  173.      * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
  174.      *
  175.      * @throws \RuntimeException
  176.      */
  177.     public function end(): NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
  178.     {
  179.         if (null === $this->ifPart) {
  180.             throw new \RuntimeException('You must specify an if part.');
  181.         }
  182.         if (null === $this->thenPart) {
  183.             throw new \RuntimeException('You must specify a then part.');
  184.         }
  185.         return $this->node;
  186.     }
  187.     /**
  188.      * Builds the expressions.
  189.      *
  190.      * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
  191.      */
  192.     public static function buildExpressions(array $expressions): array
  193.     {
  194.         foreach ($expressions as $k => $expr) {
  195.             if ($expr instanceof self) {
  196.                 $if = $expr->ifPart;
  197.                 $then = $expr->thenPart;
  198.                 $expressions[$k] = function ($v) use ($if, $then) {
  199.                     return $if($v) ? $then($v) : $v;
  200.                 };
  201.             }
  202.         }
  203.         return $expressions;
  204.     }
  205. }