5 * Contains \Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityBase.
8 namespace Drupal\drupalmoduleupgrader\Utility\Path;
10 use Doctrine\Common\Collections\ArrayCollection;
13 * Base class for PathUtilityInterface implementations.
15 abstract class PathUtilityBase extends ArrayCollection implements PathUtilityInterface {
18 * The next index for getNextWildcard() to slice on.
22 protected $_wildcard = 0;
27 public function __construct($path) {
28 if (is_array($path)) {
29 foreach ($path as $component) {
30 $this->add($component);
33 elseif (is_string($path)) {
34 $this->__construct(explode('/', $path));
37 throw new \InvalidArgumentException();
44 public function add($value) {
45 if ($value instanceof PathComponentInterface) {
48 elseif (is_scalar($value)) {
49 $this->add(static::getComponent($value));
52 throw new \InvalidArgumentException();
57 * Filters the path by a string. The filtered path will only contain
58 * components whose string representation is identical to $element.
60 * @param string $element
61 * The string to search for.
65 public function find($element) {
67 ->filter(function(PathComponentInterface $component) use ($element) {
68 return ($element === $component->__toString());
75 public function contains($element) {
76 return (boolean) $this->find($element)->count();
82 public function hasWildcards() {
83 return ($this->getWildcards()->count() > 0);
87 * Returns every {wildcard} in the path, keyed by position.
91 public function getWildcards() {
92 return $this->filter(function(PathComponentInterface $component) {
93 return $component->isWildcard();
98 * Returns the next wildcard, if any.
100 * @return \Drupal\drupalmoduleupgrader\Utility\Path\PathComponentInterface|NULL
102 public function getNextWildcard() {
103 $wildcards = $this->getWildcards()->slice($this->_wildcard, 1);
105 if (isset($wildcards[$this->_wildcard])) {
106 return $wildcards[$this->_wildcard++];
111 * Returns a copy of the collection with wildcards removed.
115 public function deleteWildcards() {
116 return $this->filter(function(PathComponentInterface $component) {
117 return (! $component->isWildcard());
124 public function getParent() {
125 if ($this->count() > 1) {
126 return new static($this->slice(0, -1));
129 throw new \LengthException('Cannot get parent a path with one component.');
136 public function __toString() {
137 return implode('/', $this->toArray());