[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/config/Definition/Builder/ -> ExprBuilder.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1