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