You are here

final class Context in Zircon Profile 8

Same name in this branch
  1. 8 vendor/sebastian/recursion-context/src/Context.php \SebastianBergmann\RecursionContext\Context
  2. 8 core/lib/Drupal/Core/Plugin/Context/Context.php \Drupal\Core\Plugin\Context\Context
  3. 8 core/lib/Drupal/Component/Plugin/Context/Context.php \Drupal\Component\Plugin\Context\Context
  4. 8 vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Context.php \phpDocumentor\Reflection\DocBlock\Context
Same name and namespace in other branches
  1. 8.0 vendor/sebastian/recursion-context/src/Context.php \SebastianBergmann\RecursionContext\Context

A context containing previously processed arrays and objects when recursively processing a value.

Hierarchy

  • class \SebastianBergmann\RecursionContext\Context

Expanded class hierarchy of Context

1 file declares its use of Context
Exporter.php in vendor/sebastian/exporter/src/Exporter.php
3 string references to 'Context'
Page::buildOptionsForm in core/modules/views/src/Plugin/views/display/Page.php
Provide a form to edit options for this plugin.
system.schema.yml in core/modules/system/config/schema/system.schema.yml
core/modules/system/config/schema/system.schema.yml
views.display.schema.yml in core/modules/views/config/schema/views.display.schema.yml
core/modules/views/config/schema/views.display.schema.yml

File

vendor/sebastian/recursion-context/src/Context.php, line 17

Namespace

SebastianBergmann\RecursionContext
View source
final class Context {

  /**
   * @var array[]
   */
  private $arrays;

  /**
   * @var \SplObjectStorage
   */
  private $objects;

  /**
   * Initialises the context
   */
  public function __construct() {
    $this->arrays = array();
    $this->objects = new \SplObjectStorage();
  }

  /**
   * Adds a value to the context.
   *
   * @param  array|object             $value The value to add.
   * @return int|string               The ID of the stored value, either as
   *                                        a string or integer.
   * @throws InvalidArgumentException Thrown if $value is not an array or
   *                                        object
   */
  public function add(&$value) {
    if (is_array($value)) {
      return $this
        ->addArray($value);
    }
    else {
      if (is_object($value)) {
        return $this
          ->addObject($value);
      }
    }
    throw new InvalidArgumentException('Only arrays and objects are supported');
  }

  /**
   * Checks if the given value exists within the context.
   *
   * @param  array|object             $value The value to check.
   * @return int|string|false         The string or integer ID of the stored
   *                                        value if it has already been seen, or
   *                                        false if the value is not stored.
   * @throws InvalidArgumentException Thrown if $value is not an array or
   *                                        object
   */
  public function contains(&$value) {
    if (is_array($value)) {
      return $this
        ->containsArray($value);
    }
    else {
      if (is_object($value)) {
        return $this
          ->containsObject($value);
      }
    }
    throw new InvalidArgumentException('Only arrays and objects are supported');
  }

  /**
   * @param  array    $array
   * @return bool|int
   */
  private function addArray(array &$array) {
    $key = $this
      ->containsArray($array);
    if ($key !== false) {
      return $key;
    }
    $this->arrays[] =& $array;
    return count($this->arrays) - 1;
  }

  /**
   * @param  object $object
   * @return string
   */
  private function addObject($object) {
    if (!$this->objects
      ->contains($object)) {
      $this->objects
        ->attach($object);
    }
    return spl_object_hash($object);
  }

  /**
   * @param  array     $array
   * @return int|false
   */
  private function containsArray(array &$array) {
    $keys = array_keys($this->arrays, $array, true);
    $hash = '_Key_' . hash('sha512', microtime(true));
    foreach ($keys as $key) {
      $this->arrays[$key][$hash] = $hash;
      if (isset($array[$hash]) && $array[$hash] === $hash) {
        unset($this->arrays[$key][$hash]);
        return $key;
      }
      unset($this->arrays[$key][$hash]);
    }
    return false;
  }

  /**
   * @param  object       $value
   * @return string|false
   */
  private function containsObject($value) {
    if ($this->objects
      ->contains($value)) {
      return spl_object_hash($value);
    }
    return false;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Context::$arrays private property
Context::$objects private property
Context::add public function Adds a value to the context.
Context::addArray private function
Context::addObject private function
Context::contains public function Checks if the given value exists within the context.
Context::containsArray private function
Context::containsObject private function
Context::__construct public function Initialises the context