Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / mikey179 / vfsStream / src / main / php / org / bovigo / vfs / vfsStreamDirectory.php
1 <?php
2 /**
3  * This file is part of vfsStream.
4  *
5  * For the full copyright and license information, please view the LICENSE
6  * file that was distributed with this source code.
7  *
8  * @package  org\bovigo\vfs
9  */
10 namespace org\bovigo\vfs;
11 /**
12  * Directory container.
13  *
14  * @api
15  */
16 class vfsStreamDirectory extends vfsStreamAbstractContent implements vfsStreamContainer
17 {
18     /**
19      * list of directory children
20      *
21      * @type  vfsStreamContent[]
22      */
23     protected $children = array();
24
25     /**
26      * constructor
27      *
28      * @param   string  $name
29      * @param   int     $permissions  optional
30      * @throws  vfsStreamException
31      */
32     public function __construct($name, $permissions = null)
33     {
34         if (strstr($name, '/') !== false) {
35             throw new vfsStreamException('Directory name can not contain /.');
36         }
37
38         $this->type = vfsStreamContent::TYPE_DIR;
39         parent::__construct($name, $permissions);
40     }
41
42     /**
43      * returns default permissions for concrete implementation
44      *
45      * @return  int
46      * @since   0.8.0
47      */
48     protected function getDefaultPermissions()
49     {
50         return 0777;
51     }
52
53     /**
54      * returns size of directory
55      *
56      * The size of a directory is always 0 bytes. To calculate the summarized
57      * size of all children in the directory use sizeSummarized().
58      *
59      * @return  int
60      */
61     public function size()
62     {
63         return 0;
64     }
65
66     /**
67      * returns summarized size of directory and its children
68      *
69      * @return  int
70      */
71     public function sizeSummarized()
72     {
73         $size = 0;
74         foreach ($this->children as $child) {
75             if ($child->getType() === vfsStreamContent::TYPE_DIR) {
76                 $size += $child->sizeSummarized();
77             } else {
78                 $size += $child->size();
79             }
80         }
81
82         return $size;
83     }
84
85     /**
86      * renames the content
87      *
88      * @param   string  $newName
89      * @throws  vfsStreamException
90      */
91     public function rename($newName)
92     {
93         if (strstr($newName, '/') !== false) {
94             throw new vfsStreamException('Directory name can not contain /.');
95         }
96
97         parent::rename($newName);
98     }
99
100
101     /**
102      * sets parent path
103      *
104      * @param  string  $parentPath
105      * @internal  only to be set by parent
106      * @since   1.2.0
107      */
108     public function setParentPath($parentPath)
109     {
110         parent::setParentPath($parentPath);
111         foreach ($this->children as $child) {
112             $child->setParentPath($this->path());
113         }
114     }
115
116     /**
117      * adds child to the directory
118      *
119      * @param  vfsStreamContent  $child
120      */
121     public function addChild(vfsStreamContent $child)
122     {
123         $child->setParentPath($this->path());
124         $this->children[$child->getName()] = $child;
125         $this->updateModifications();
126     }
127
128     /**
129      * removes child from the directory
130      *
131      * @param   string  $name
132      * @return  bool
133      */
134     public function removeChild($name)
135     {
136         foreach ($this->children as $key => $child) {
137             if ($child->appliesTo($name)) {
138                 $child->setParentPath(null);
139                 unset($this->children[$key]);
140                 $this->updateModifications();
141                 return true;
142             }
143         }
144
145         return false;
146     }
147
148     /**
149      * updates internal timestamps
150      */
151     protected function updateModifications()
152     {
153         $time = time();
154         $this->lastAttributeModified = $time;
155         $this->lastModified          = $time;
156     }
157
158     /**
159      * checks whether the container contains a child with the given name
160      *
161      * @param   string  $name
162      * @return  bool
163      */
164     public function hasChild($name)
165     {
166         return ($this->getChild($name) !== null);
167     }
168
169     /**
170      * returns the child with the given name
171      *
172      * @param   string  $name
173      * @return  vfsStreamContent
174      */
175     public function getChild($name)
176     {
177         $childName = $this->getRealChildName($name);
178         foreach ($this->children as $child) {
179             if ($child->getName() === $childName) {
180                 return $child;
181             }
182
183             if ($child->appliesTo($childName) === true && $child->hasChild($childName) === true) {
184                 return $child->getChild($childName);
185             }
186         }
187
188         return null;
189     }
190
191     /**
192      * helper method to detect the real child name
193      *
194      * @param   string  $name
195      * @return  string
196      */
197     protected function getRealChildName($name)
198     {
199         if ($this->appliesTo($name) === true) {
200             return self::getChildName($name, $this->name);
201         }
202
203         return $name;
204     }
205
206     /**
207      * helper method to calculate the child name
208      *
209      * @param   string  $name
210      * @param   string  $ownName
211      * @return  string
212      */
213     protected static function getChildName($name, $ownName)
214     {
215         if ($name === $ownName) {
216             return $name;
217         }
218
219         return substr($name, strlen($ownName) + 1);
220     }
221
222     /**
223      * checks whether directory contains any children
224      *
225      * @return  bool
226      * @since   0.10.0
227      */
228     public function hasChildren()
229     {
230         return (count($this->children) > 0);
231     }
232
233     /**
234      * returns a list of children for this directory
235      *
236      * @return  vfsStreamContent[]
237      */
238     public function getChildren()
239     {
240         return array_values($this->children);
241     }
242
243     /**
244      * returns iterator for the children
245      *
246      * @return  vfsStreamContainerIterator
247      */
248     public function getIterator()
249     {
250         return new vfsStreamContainerIterator($this->children);
251     }
252
253     /**
254      * checks whether dir is a dot dir
255      *
256      * @return  bool
257      */
258     public function isDot()
259     {
260         if ('.' === $this->name || '..' === $this->name) {
261             return true;
262         }
263
264         return false;
265     }
266 }