|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_files; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\Interfaces\ITypes; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Node |
|
11
|
|
|
* @package kalanis\kw_files |
|
12
|
|
|
* File/dir object - similar to the SplFileInfo, but for passing as class over storages implemented externally |
|
13
|
|
|
* Passed paths are in following style: |
|
14
|
|
|
* - empty for current root node from lookup |
|
15
|
|
|
* - array containing path to nodes "somewhere" in tree |
|
16
|
|
|
* - then it's possible to reconstruct the path just by using array_merge() |
|
17
|
|
|
*/ |
|
18
|
|
|
class Node |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var array<string> */ |
|
21
|
|
|
protected array $path = []; |
|
22
|
|
|
protected int $size = 0; |
|
23
|
|
|
protected string $type = ITypes::TYPE_UNKNOWN; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string[] $path only from wanted dir, not full path |
|
27
|
|
|
* @param int $size |
|
28
|
|
|
* @param string $type |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
16 |
|
public function setData(array $path = [], int $size = 0, string $type = ITypes::TYPE_UNKNOWN): self |
|
32
|
|
|
{ |
|
33
|
16 |
|
$this->path = $path; |
|
34
|
16 |
|
$this->size = $size; |
|
35
|
16 |
|
$this->type = $type; |
|
36
|
16 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Gets the full path |
|
41
|
|
|
* @return array<string> the path to the file. |
|
42
|
|
|
*/ |
|
43
|
16 |
|
public function getPath(): array |
|
44
|
|
|
{ |
|
45
|
16 |
|
return $this->path; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Gets file size |
|
50
|
|
|
* @return int The filesize in bytes for files, the number of items for dirs |
|
51
|
|
|
*/ |
|
52
|
13 |
|
public function getSize(): int |
|
53
|
|
|
{ |
|
54
|
13 |
|
return $this->size; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Gets file type |
|
59
|
|
|
* @return string A string representing the type of the entry. |
|
60
|
|
|
* May be one of file, link or dir |
|
61
|
|
|
*/ |
|
62
|
16 |
|
public function getType(): string |
|
63
|
|
|
{ |
|
64
|
16 |
|
return $this->type; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Tells if the object references a regular file |
|
69
|
|
|
* @return bool true if the file exists and is a regular file (not a link), false otherwise. |
|
70
|
|
|
*/ |
|
71
|
7 |
|
public function isFile(): bool |
|
72
|
|
|
{ |
|
73
|
7 |
|
return ITypes::TYPE_FILE === $this->getType(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Tells if the file is a directory |
|
78
|
|
|
* @return bool true if a directory, false otherwise. |
|
79
|
|
|
*/ |
|
80
|
8 |
|
public function isDir(): bool |
|
81
|
|
|
{ |
|
82
|
8 |
|
return ITypes::TYPE_DIR === $this->getType(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|