1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils\FileHelper; |
6
|
|
|
|
7
|
|
|
use AppUtils\FileHelper; |
8
|
|
|
use AppUtils\FileHelper_Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An indeterminate path is a special case, where |
12
|
|
|
* the target file or folder does not exist on disk |
13
|
|
|
* yet, and it does not have an extension, so it |
14
|
|
|
* can be either a path or a file without extension. |
15
|
|
|
* |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class IndeterminatePath extends AbstractPathInfo |
19
|
|
|
{ |
20
|
|
|
public const ERROR_INVALID_CONVERSION_TYPE = 115501; |
21
|
|
|
|
22
|
|
|
public const CONVERT_TYPE_FILE = 'file'; |
23
|
|
|
public const CONVERT_TYPE_FOLDER = 'folder'; |
24
|
|
|
|
25
|
|
|
public function getExtension(bool $lowercase = true) : string |
26
|
|
|
{ |
27
|
|
|
return ''; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getFolderPath() : string |
31
|
|
|
{ |
32
|
|
|
return $this->getPath(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function delete() : self |
36
|
|
|
{ |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function convertToFile() : FileInfo |
41
|
|
|
{ |
42
|
|
|
// Doing this manually, as FileHelper::saveFile() |
43
|
|
|
// checks if the target is a file, which will fail. |
44
|
|
|
if(file_put_contents($this->getPath(), '') === false) { |
45
|
|
|
throw new FileHelper_Exception( |
46
|
|
|
'Cannot create file', |
47
|
|
|
'', |
48
|
|
|
FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return FileInfo::factory($this->getPath()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function convertToFolder() : FolderInfo |
56
|
|
|
{ |
57
|
|
|
$path = $this->getPath(); |
58
|
|
|
|
59
|
|
|
// Doing this manually, as FileHelper::createFolder() |
60
|
|
|
// checks if the target is a folder, which will fail. |
61
|
|
|
if(!mkdir($path, 0777, true) && !is_dir($path)) |
62
|
|
|
{ |
63
|
|
|
throw new FileHelper_Exception( |
64
|
|
|
'Cannot create folder', |
65
|
|
|
'', |
66
|
|
|
FileHelper::ERROR_CANNOT_CREATE_FOLDER |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return FolderInfo::factory($path); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function convertTo(string $type) : PathInfoInterface |
74
|
|
|
{ |
75
|
|
|
if($type === self::CONVERT_TYPE_FILE) { |
76
|
|
|
return $this->convertToFile(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if($type === self::CONVERT_TYPE_FOLDER) { |
80
|
|
|
return $this->convertToFolder(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new FileHelper_Exception( |
84
|
|
|
'Invalid conversion type.', |
85
|
|
|
sprintf( |
86
|
|
|
'The specified type [%s] does not exist.', |
87
|
|
|
$type |
88
|
|
|
), |
89
|
|
|
self::ERROR_INVALID_CONVERSION_TYPE |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|