1 | <?php |
||
2 | |||
3 | namespace kalanis\kw_input; |
||
4 | |||
5 | |||
6 | use kalanis\kw_input\Interfaces\ISource; |
||
7 | |||
8 | |||
9 | /** |
||
10 | * Class Inputs |
||
11 | * @package kalanis\kw_input |
||
12 | * Base class for passing info from inputs into objects |
||
13 | * Compress all inputs into single array with entries about everything |
||
14 | */ |
||
15 | class Inputs |
||
16 | { |
||
17 | /** @var Interfaces\IEntry[] */ |
||
18 | protected array $entries = []; |
||
19 | protected Interfaces\ISource $source; |
||
20 | protected Parsers\Factory $parserFactory; |
||
21 | protected Loaders\Factory $loaderFactory; |
||
22 | |||
23 | 3 | public function __construct(?Parsers\Factory $parserFactory = null, ?Loaders\Factory $loaderFactory = null) |
|
24 | { |
||
25 | 3 | $this->parserFactory = $parserFactory ?: new Parsers\Factory(); |
|
26 | 3 | $this->loaderFactory = $loaderFactory ?: new Loaders\Factory(); |
|
27 | 3 | $this->source = new Sources\Basic(); |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Setting the variable sources - from cli (argv), _GET, _POST, _SERVER, ... |
||
32 | * @param ISource|string[]|int[]|null $source |
||
33 | * @return $this |
||
34 | */ |
||
35 | 3 | public function setSource($source = null): self |
|
36 | { |
||
37 | 3 | if (!empty($source) && ($source instanceof Interfaces\ISource)) { |
|
38 | 3 | $this->source = $source; |
|
39 | 2 | } elseif (($this->source instanceof Sources\Basic) && is_array($source)) { |
|
40 | 2 | $this->source->setCli($source); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
41 | } |
||
42 | 3 | return $this; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Load entries from source into the local entries which will be accessible |
||
47 | * These two calls came usually in pair |
||
48 | * |
||
49 | * $input->setSource($argv)->loadEntries()->getAllEntries(); |
||
50 | * @return $this |
||
51 | */ |
||
52 | 3 | public function loadEntries(): self |
|
53 | { |
||
54 | 3 | $this->entries = array_merge( |
|
55 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_EXTERNAL, $this->source->external()), |
|
56 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_JSON, $this->source->inputRawPaths()), |
|
57 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_GET, $this->source->get()), |
|
58 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_POST, $this->source->post()), |
|
59 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_CLI, $this->source->cli()), |
|
60 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_COOKIE, $this->source->cookie()), |
|
61 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_SESSION, $this->source->session()), |
|
62 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_FILES, $this->source->files()), |
|
63 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_ENV, $this->source->env()), |
|
64 | 3 | $this->loadInput(Interfaces\IEntry::SOURCE_SERVER, $this->source->server()) |
|
65 | 3 | ); |
|
66 | 3 | return $this; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $source |
||
71 | * @param array<string|int, string|int|bool|null|array<string, string|int|bool|null|array<string, string|int|bool|null>>>|null $inputArray |
||
72 | * @return Interfaces\IEntry[] |
||
73 | */ |
||
74 | 3 | protected function loadInput(string $source, ?array $inputArray = null): array |
|
75 | { |
||
76 | 3 | if (empty($inputArray)) { |
|
77 | 3 | return []; |
|
78 | } |
||
79 | 3 | $parser = $this->parserFactory->getLoader($source); |
|
80 | 3 | $loader = $this->loaderFactory->getLoader($source); |
|
81 | // @phpstan-ignore-next-line |
||
82 | 3 | return $loader->loadVars($source, $parser->parseInput($inputArray)); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get all local entries |
||
87 | * @return Interfaces\IEntry[] array for foreach |
||
88 | */ |
||
89 | 3 | public function getAllEntries(): array |
|
90 | { |
||
91 | 3 | return $this->entries; |
|
92 | } |
||
93 | } |
||
94 |