X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FUtility%2FPath%2FPathUtilityBase.php;fp=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FUtility%2FPath%2FPathUtilityBase.php;h=b2ca3a8a04cdeeb2dcf6d38aacfe9f9134a3a5a2;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/drupalmoduleupgrader/src/Utility/Path/PathUtilityBase.php b/web/modules/contrib/drupalmoduleupgrader/src/Utility/Path/PathUtilityBase.php new file mode 100644 index 000000000..b2ca3a8a0 --- /dev/null +++ b/web/modules/contrib/drupalmoduleupgrader/src/Utility/Path/PathUtilityBase.php @@ -0,0 +1,140 @@ +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 (boolean) $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()); + } + +}