1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_input\Loaders; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use finfo; |
7
|
|
|
use kalanis\kw_input\Entries; |
8
|
|
|
use kalanis\kw_input\Interfaces\IEntry; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CliEntry |
13
|
|
|
* @package kalanis\kw_input\Loaders |
14
|
|
|
* Load input arrays into normalized entries - CLI entries which could be also files |
15
|
|
|
*/ |
16
|
|
|
class CliEntry extends ALoader |
17
|
|
|
{ |
18
|
|
|
protected static string $basicPath = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $path |
22
|
|
|
* @codeCoverageIgnore because must be set before test |
23
|
|
|
*/ |
24
|
|
|
public static function setBasicPath(string $path): void |
25
|
|
|
{ |
26
|
|
|
static::$basicPath = $path; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Transform input values to something more reliable |
31
|
|
|
* @param string $source |
32
|
|
|
* @param array<string|int, string|int> $array |
33
|
|
|
* @return Entries\Entry[] |
34
|
|
|
*/ |
35
|
2 |
|
public function loadVars(string $source, $array): array |
36
|
|
|
{ |
37
|
2 |
|
$result = []; |
38
|
2 |
|
$entries = new Entries\Entry(); |
39
|
2 |
|
$fileEntries = new Entries\FileEntry(); |
40
|
2 |
|
foreach ($array as $postedKey => $posted) { |
41
|
2 |
|
$fullPath = $this->checkFile($posted); |
42
|
2 |
|
if (!is_null($fullPath)) { |
43
|
2 |
|
$entry = clone $fileEntries; |
44
|
2 |
|
$entry->setEntry(IEntry::SOURCE_FILES, strval($postedKey)); |
45
|
2 |
|
$entry->setFile( |
46
|
2 |
|
strval($posted), |
47
|
2 |
|
$fullPath, |
48
|
2 |
|
$this->getType($fullPath), |
49
|
2 |
|
UPLOAD_ERR_OK, |
50
|
2 |
|
$this->getSize($fullPath) |
51
|
2 |
|
); |
52
|
2 |
|
$result[] = $entry; |
53
|
|
|
} else { |
54
|
2 |
|
$entry = clone $entries; |
55
|
2 |
|
$entry->setEntry($source, strval($postedKey), $posted); |
56
|
2 |
|
$result[] = $entry; |
57
|
|
|
} |
58
|
|
|
} |
59
|
2 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string|int|bool $path |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
2 |
|
protected function checkFile($path): ?string |
67
|
|
|
{ |
68
|
2 |
|
if (!is_string($path) || empty($path)) { |
69
|
2 |
|
return null; |
70
|
|
|
} |
71
|
2 |
|
$isFull = DIRECTORY_SEPARATOR == $path[0]; |
72
|
2 |
|
$known = realpath($isFull ? $path : static::$basicPath . DIRECTORY_SEPARATOR . $path ); |
73
|
2 |
|
return (false === $known) ? null : $known ; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
protected function getType(string $path): string |
77
|
|
|
{ |
78
|
2 |
|
$fType = 'application/octet-stream'; |
79
|
2 |
|
$fInfo = @new finfo(FILEINFO_MIME_TYPE); |
80
|
2 |
|
$fRes = @$fInfo->file($path); |
81
|
2 |
|
if (is_string($fRes) && !empty($fRes)) { |
82
|
2 |
|
$fType = $fRes; |
83
|
|
|
} |
84
|
2 |
|
return $fType; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
protected function getSize(string $path): int |
88
|
|
|
{ |
89
|
2 |
|
return intval(filesize($path)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|