[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Items/ -> ProgrammableCallback.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\Items;
   9  
  10  use InvalidArgumentException;
  11  use s9e\TextFormatter\Configurator\ConfigProvider;
  12  use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
  13  use s9e\TextFormatter\Configurator\JavaScript\Code;
  14  use s9e\TextFormatter\Configurator\JavaScript\FunctionProvider;
  15  
  16  class ProgrammableCallback implements ConfigProvider
  17  {
  18      /**
  19      * @var callback Callback
  20      */
  21      protected $callback;
  22  
  23      /**
  24      * @var string JavaScript source code for this callback
  25      */
  26      protected $js = 'returnFalse';
  27  
  28      /**
  29      * @var array List of params to be passed to the callback
  30      */
  31      protected $params = [];
  32  
  33      /**
  34      * @var array Variables associated with this instance
  35      */
  36      protected $vars = [];
  37  
  38      /**
  39      * @param callable $callback
  40      */
  41  	public function __construct($callback)
  42      {
  43          if (!is_callable($callback))
  44          {
  45              throw new InvalidArgumentException(__METHOD__ . '() expects a callback');
  46          }
  47  
  48          $this->callback = $this->normalizeCallback($callback);
  49          $this->autoloadJS();
  50      }
  51  
  52      /**
  53      * Add a parameter by value
  54      *
  55      * @param  mixed $paramValue
  56      * @return self
  57      */
  58  	public function addParameterByValue($paramValue)
  59      {
  60          $this->params[] = $paramValue;
  61  
  62          return $this;
  63      }
  64  
  65      /**
  66      * Add a parameter by name
  67      *
  68      * The value will be dynamically generated by the caller
  69      *
  70      * @param  string $paramName
  71      * @return self
  72      */
  73  	public function addParameterByName($paramName)
  74      {
  75          if (array_key_exists($paramName, $this->params))
  76          {
  77              throw new InvalidArgumentException("Parameter '" . $paramName . "' already exists");
  78          }
  79  
  80          $this->params[$paramName] = null;
  81  
  82          return $this;
  83      }
  84  
  85      /**
  86      * Get this object's callback
  87      *
  88      * @return callback
  89      */
  90  	public function getCallback()
  91      {
  92          return $this->callback;
  93      }
  94  
  95      /**
  96      * Get this callback's JavaScript
  97      *
  98      * @return string
  99      */
 100  	public function getJS()
 101      {
 102          return $this->js;
 103      }
 104  
 105      /**
 106      * Get this object's variables
 107      *
 108      * @return array
 109      */
 110  	public function getVars()
 111      {
 112          return $this->vars;
 113      }
 114  
 115      /**
 116      * Remove all the parameters
 117      *
 118      * @return self
 119      */
 120  	public function resetParameters()
 121      {
 122          $this->params = [];
 123  
 124          return $this;
 125      }
 126  
 127      /**
 128      * Set this callback's JavaScript
 129      *
 130      * @param  string $js JavaScript source code for this callback
 131      * @return self
 132      */
 133  	public function setJS($js)
 134      {
 135          $this->js = $js;
 136  
 137          return $this;
 138      }
 139  
 140      /**
 141      * Set or overwrite one of this callback's variable
 142      *
 143      * @param  string $name  Variable name
 144      * @param  string $value Variable value
 145      * @return self
 146      */
 147  	public function setVar($name, $value)
 148      {
 149          $this->vars[$name] = $value;
 150  
 151          return $this;
 152      }
 153  
 154      /**
 155      * Set all of this callback's variables at once
 156      *
 157      * @param  array $vars Associative array of values
 158      * @return self
 159      */
 160  	public function setVars(array $vars)
 161      {
 162          $this->vars = $vars;
 163  
 164          return $this;
 165      }
 166  
 167      /**
 168      * {@inheritdoc}
 169      */
 170  	public function asConfig()
 171      {
 172          $config = ['callback' => $this->callback];
 173  
 174          foreach ($this->params as $k => $v)
 175          {
 176              if (is_numeric($k))
 177              {
 178                  // By value
 179                  $config['params'][] = $v;
 180              }
 181              elseif (isset($this->vars[$k]))
 182              {
 183                  // By name, but the value is readily available in $this->vars
 184                  $config['params'][] = $this->vars[$k];
 185              }
 186              else
 187              {
 188                  // By name
 189                  $config['params'][$k] = null;
 190              }
 191          }
 192  
 193          if (isset($config['params']))
 194          {
 195              $config['params'] = ConfigHelper::toArray($config['params'], true, true);
 196          }
 197  
 198          // Add the callback's JavaScript representation
 199          $config['js'] = new Code($this->js);
 200  
 201          return $config;
 202      }
 203  
 204      /**
 205      * Try to load the JavaScript source for this callback
 206      *
 207      * @return void
 208      */
 209  	protected function autoloadJS()
 210      {
 211          if (!is_string($this->callback))
 212          {
 213              return;
 214          }
 215  
 216          try
 217          {
 218              $this->js = FunctionProvider::get($this->callback);
 219          }
 220          catch (InvalidArgumentException $e)
 221          {
 222              // Do nothing
 223          }
 224      }
 225  
 226      /**
 227      * Normalize a callback's representation
 228      *
 229      * @param  callable $callback
 230      * @return callable
 231      */
 232  	protected function normalizeCallback($callback)
 233      {
 234          // Normalize ['foo', 'bar'] to 'foo::bar'
 235          if (is_array($callback) && is_string($callback[0]))
 236          {
 237              $callback = $callback[0] . '::' . $callback[1];
 238          }
 239  
 240          // Normalize '\\foo' to 'foo' and '\\foo::bar' to 'foo::bar'
 241          if (is_string($callback))
 242          {
 243              $callback = ltrim($callback, '\\');
 244          }
 245  
 246          return $callback;
 247      }
 248  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1