[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1