-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFilesystemElement.php
More file actions
58 lines (51 loc) · 1.08 KB
/
FilesystemElement.php
File metadata and controls
58 lines (51 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace DesignPatterns\Structural\Adapter\Filesystem;
/**
* Represents either a folder or a file in a filesystem.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
class FilesystemElement
{
/**
* Absolute path.
*
* @var string
*/
protected $path;
/**
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;
}
/**
* Get the basename of current element: /var/www/{basename.ext}.
*
* @return string
*/
public function getBasename()
{
return basename($this->path);
}
/**
* Get all children of this element.
* Always returns empty array if this element is a file.
*
* @return array
*/
public function getSubElements()
{
if (!is_dir($this->path)) {
return [];
}
$children = [];
foreach (scandir($this->path) as $child) {
if (!in_array($child, ['.', '..'])) {
$children[] = $this->path.'/'.$child;
}
};
return $children;
}
}