You are here

abstract class PathUtilityBase in Drupal 7 to 8/9 Module Upgrader 8

Base class for PathUtilityInterface implementations.

Hierarchy

Expanded class hierarchy of PathUtilityBase

2 files declare their use of PathUtilityBase
PathUtility.php in src/Utility/Path/Drupal7/PathUtility.php
PathUtility.php in src/Utility/Path/Drupal8/PathUtility.php

File

src/Utility/Path/PathUtilityBase.php, line 10

Namespace

Drupal\drupalmoduleupgrader\Utility\Path
View source
abstract class PathUtilityBase extends ArrayCollection implements PathUtilityInterface {

  /**
   * The next index for getNextWildcard() to slice on.
   *
   * @var int
   */
  protected $_wildcard = 0;

  /**
   * {@inheritdoc}
   */
  public function __construct($path) {
    if (is_array($path)) {
      foreach ($path as $component) {
        $this
          ->add($component);
      }
    }
    elseif (is_string($path)) {
      $this
        ->__construct(explode('/', $path));
    }
    else {
      throw new \InvalidArgumentException();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function add($value) {
    if ($value instanceof PathComponentInterface) {
      parent::add($value);
    }
    elseif (is_scalar($value)) {
      $this
        ->add(static::getComponent($value));
    }
    else {
      throw new \InvalidArgumentException();
    }
  }

  /**
   * Filters the path by a string. The filtered path will only contain
   * components whose string representation is identical to $element.
   *
   * @param string $element
   *   The string to search for.
   *
   * @return static
   */
  public function find($element) {
    return $this
      ->filter(function (PathComponentInterface $component) use ($element) {
      return $element === $component
        ->__toString();
    });
  }

  /**
   * {@inheritdoc}
   */
  public function contains($element) {
    return (bool) $this
      ->find($element)
      ->count();
  }

  /**
   * {@inheritdoc}
   */
  public function hasWildcards() {
    return $this
      ->getWildcards()
      ->count() > 0;
  }

  /**
   * Returns every {wildcard} in the path, keyed by position.
   *
   * @return static
   */
  public function getWildcards() {
    return $this
      ->filter(function (PathComponentInterface $component) {
      return $component
        ->isWildcard();
    });
  }

  /**
   * Returns the next wildcard, if any.
   *
   * @return \Drupal\drupalmoduleupgrader\Utility\Path\PathComponentInterface|null
   */
  public function getNextWildcard() {
    $wildcards = $this
      ->getWildcards()
      ->slice($this->_wildcard, 1);
    if (isset($wildcards[$this->_wildcard])) {
      return $wildcards[$this->_wildcard++];
    }
  }

  /**
   * Returns a copy of the collection with wildcards removed.
   *
   * @return static
   */
  public function deleteWildcards() {
    return $this
      ->filter(function (PathComponentInterface $component) {
      return !$component
        ->isWildcard();
    });
  }

  /**
   * {@inheritdoc}
   */
  public function getParent() {
    if ($this
      ->count() > 1) {
      return new static($this
        ->slice(0, -1));
    }
    else {
      throw new \LengthException('Cannot get parent a path with one component.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return implode('/', $this
      ->toArray());
  }

  /**
   * After PHP 7.2+ for count() E_WARNING will now be emitted when attempting to count() non-countable types.
   * Ref: https://secure.php.net/manual/en/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types
   *
   * Possible solutions will be to check is_countable() to check for variable is instanceof Countable, but
   * is_countable() is available from 7.3.
   *
   * To make compatible for lower version we need to suppress the warning for now because we know elements will
   * always be of array type.
   *
   * {@inheritdoc}
   */
  public function count() {
    return @count($this
      ->toArray());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathUtilityBase::$_wildcard protected property The next index for getNextWildcard() to slice on.
PathUtilityBase::add public function
PathUtilityBase::contains public function
PathUtilityBase::count public function After PHP 7.2+ for count() E_WARNING will now be emitted when attempting to count() non-countable types. Ref: https://secure.php.net/manual/en/migration72.incompatible.php#migration7...
PathUtilityBase::deleteWildcards public function Returns a copy of the collection with wildcards removed.
PathUtilityBase::find public function Filters the path by a string. The filtered path will only contain components whose string representation is identical to $element.
PathUtilityBase::getNextWildcard public function Returns the next wildcard, if any.
PathUtilityBase::getParent public function Returns a PathUtilityInterface for the parent path. Overrides PathUtilityInterface::getParent
PathUtilityBase::getWildcards public function Returns every {wildcard} in the path, keyed by position.
PathUtilityBase::hasWildcards public function Returns if there are wildcards in the path. Overrides PathUtilityInterface::hasWildcards
PathUtilityBase::__construct public function Constructs a path utility. Overrides PathUtilityInterface::__construct
PathUtilityBase::__toString public function Collapses the path into a string. Overrides PathUtilityInterface::__toString
PathUtilityInterface::getComponent public static function Returns a new path component wrapping a value. 2