Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Utility / Path / PathUtilityBase.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityBase.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Utility\Path;
9
10 use Doctrine\Common\Collections\ArrayCollection;
11
12 /**
13  * Base class for PathUtilityInterface implementations.
14  */
15 abstract class PathUtilityBase extends ArrayCollection implements PathUtilityInterface {
16
17   /**
18    * The next index for getNextWildcard() to slice on.
19    *
20    * @var integer
21    */
22   protected $_wildcard = 0;
23
24   /**
25    * {@inheritdoc}
26    */
27   public function __construct($path) {
28     if (is_array($path)) {
29       foreach ($path as $component) {
30         $this->add($component);
31       }
32     }
33     elseif (is_string($path)) {
34       $this->__construct(explode('/', $path));
35     }
36     else {
37       throw new \InvalidArgumentException();
38     }
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function add($value) {
45     if ($value instanceof PathComponentInterface) {
46       parent::add($value);
47     }
48     elseif (is_scalar($value)) {
49       $this->add(static::getComponent($value));
50     }
51     else {
52       throw new \InvalidArgumentException();
53     }
54   }
55
56   /**
57    * Filters the path by a string. The filtered path will only contain
58    * components whose string representation is identical to $element.
59    *
60    * @param string $element
61    *  The string to search for.
62    *
63    * @return static
64    */
65   public function find($element) {
66     return $this
67       ->filter(function(PathComponentInterface $component) use ($element) {
68         return ($element === $component->__toString());
69       });
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function contains($element) {
76     return (boolean) $this->find($element)->count();
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function hasWildcards() {
83     return ($this->getWildcards()->count() > 0);
84   }
85
86   /**
87    * Returns every {wildcard} in the path, keyed by position.
88    *
89    * @return static
90    */
91   public function getWildcards() {
92     return $this->filter(function(PathComponentInterface $component) {
93       return $component->isWildcard();
94     });
95   }
96
97   /**
98    * Returns the next wildcard, if any.
99    *
100    * @return \Drupal\drupalmoduleupgrader\Utility\Path\PathComponentInterface|NULL
101    */
102   public function getNextWildcard() {
103     $wildcards = $this->getWildcards()->slice($this->_wildcard, 1);
104
105     if (isset($wildcards[$this->_wildcard])) {
106       return $wildcards[$this->_wildcard++];
107     }
108   }
109
110   /**
111    * Returns a copy of the collection with wildcards removed.
112    *
113    * @return static
114    */
115   public function deleteWildcards() {
116     return $this->filter(function(PathComponentInterface $component) {
117       return (! $component->isWildcard());
118     });
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function getParent() {
125     if ($this->count() > 1) {
126       return new static($this->slice(0, -1));
127     }
128     else {
129       throw new \LengthException('Cannot get parent a path with one component.');
130     }
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function __toString() {
137     return implode('/', $this->toArray());
138   }
139
140 }