You are here

class ParameterBag in JSON-RPC 8

Same name and namespace in other branches
  1. 2.x src/Object/ParameterBag.php \Drupal\jsonrpc\Object\ParameterBag

Value class to hold multiple parameters.

Hierarchy

Expanded class hierarchy of ParameterBag

9 files declare their use of ParameterBag
AddPermissionToRole.php in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/AddPermissionToRole.php
Cache.php in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/Cache.php
ExecutableWithParamsInterface.php in src/ExecutableWithParamsInterface.php
Handler.php in src/Handler.php
ListPermissions.php in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/ListPermissions.php

... See full list

File

src/Object/ParameterBag.php, line 8

Namespace

Drupal\jsonrpc\Object
View source
class ParameterBag {

  /**
   * True if the params in the bag are positional. They have sequential keys.
   *
   * @var bool
   */
  protected $positional;

  /**
   * The parameters in the bag.
   *
   * @var mixed[]
   *   The parameters.
   */
  protected $parameters;

  /**
   * ParameterBag constructor.
   *
   * @param array $parameters
   *   The parameters.
   * @param bool $positional
   *   True if the parameters are positional.
   */
  public function __construct(array $parameters, $positional = FALSE) {
    $this->positional = $positional;
    $this->parameters = $positional ? array_values($parameters) : $parameters;
  }

  /**
   * Gets the parameter value by its key.
   *
   * @param string|int $key
   *   The parameter key.
   *
   * @return mixed
   *   The parameter.
   */
  public function get($key) {
    $this
      ->ensure($key);
    return isset($this->parameters[$key]) ? $this->parameters[$key] : NULL;
  }

  /**
   * Checks if the bag has a parameter.
   *
   * @param string|int $key
   *   The parameter key.
   *
   * @return bool
   *   True if the param is present.
   */
  public function has($key) {
    $this
      ->checkKeyIsValid($key);
    return isset($this->parameters[$key]);
  }

  /**
   * Checks if the parameter bag is empty.
   *
   * @return bool
   *   True if the bag is empty.
   */
  public function isEmpty() {
    return empty($this->parameters);
  }

  /**
   * Throw an exception if the bag does not have the parameter.
   *
   * @throws \InvalidArgumentException
   *   When the parameter is not present in the bag.
   */
  protected function ensure($key) {
    $this
      ->checkKeyIsValid($key);
  }

  /**
   * Checks if the key is valid.
   *
   * @throws \InvalidArgumentException
   *   If the key is not valid.
   */
  protected function checkKeyIsValid($key) {
    if ($this->positional && !is_int($key) && $key >= 0) {
      throw new \InvalidArgumentException('The parameters are by-position. Integer key required.');
    }
    elseif (!is_string($key)) {
      throw new \InvalidArgumentException('The parameters are by-name. String key required.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParameterBag::$parameters protected property The parameters in the bag.
ParameterBag::$positional protected property True if the params in the bag are positional. They have sequential keys.
ParameterBag::checkKeyIsValid protected function Checks if the key is valid.
ParameterBag::ensure protected function Throw an exception if the bag does not have the parameter.
ParameterBag::get public function Gets the parameter value by its key.
ParameterBag::has public function Checks if the bag has a parameter.
ParameterBag::isEmpty public function Checks if the parameter bag is empty.
ParameterBag::__construct public function ParameterBag constructor.