|
3 | 3 | namespace Hyperlink\JsonStructure\Commands;
|
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command;
|
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use Illuminate\Support\Facades\Storage; |
6 | 8 |
|
7 | 9 | class JsonStructureCommand extends Command
|
8 | 10 | {
|
9 |
| - public $signature = 'laravel-json-structure'; |
| 11 | + public $signature = 'json:structure |
| 12 | + {endpoint? : The api route name to get the json structure from}'; |
| 13 | +// {array?* : The array to get the json structure from}'; |
10 | 14 |
|
11 |
| - public $description = 'My command'; |
| 15 | + public $description = 'Converts a json structure to an array with the json keys'; |
12 | 16 |
|
13 | 17 | public function handle(): int
|
14 | 18 | {
|
15 |
| - $this->comment('All done'); |
| 19 | + $this->info('Starting json structure command...'); |
| 20 | + |
| 21 | +// if ($this->argument('endpoint')) { |
| 22 | + $this->info('Getting json structure from endpoint...'); |
| 23 | + |
| 24 | + $response = Http::get(route($this->argument('endpoint'))); |
| 25 | + |
| 26 | + if ($response->failed()) { |
| 27 | + $this->error('Could not get json structure from endpoint'); |
| 28 | + |
| 29 | + return Command::FAILURE; |
| 30 | + } |
| 31 | + $this->comment('Getting json structure from endpoint successful'); |
| 32 | + |
| 33 | + $json = $response->json(); |
| 34 | +// } else { |
| 35 | +// $this->info('Getting json structure from array...'); |
| 36 | +// |
| 37 | +// $json = $this->argument('array'); |
| 38 | +// } |
| 39 | + |
| 40 | + if (is_null($json)) { |
| 41 | + $this->error('The json structure is null'); |
| 42 | + return Command::FAILURE; |
| 43 | + } |
| 44 | + |
| 45 | + $this->info('Attempting to decode json structure...'); |
| 46 | + $result = '[' . PHP_EOL . $this->implode_recursive(',', $this->array_keys_r($json)) . PHP_EOL . ']' . PHP_EOL; |
| 47 | + $this->comment('Decoding json structure successful'); |
| 48 | + |
| 49 | + $this->info('Writing result to file...'); |
| 50 | + Storage::disk('local')->put('json_structure.txt', $result); |
| 51 | + $this->comment('Writing result to file successful'); |
16 | 52 |
|
17 | 53 | return self::SUCCESS;
|
18 | 54 | }
|
| 55 | + |
| 56 | + private function array_keys_r($array): array |
| 57 | + { |
| 58 | + $keys = array_keys($array); |
| 59 | + $keys_array = []; |
| 60 | + |
| 61 | + foreach ($array as $idx => $i) { |
| 62 | + if ($this->is_array_of_arrays($i)) { |
| 63 | + if ($index = array_search($idx, $keys, true)) { |
| 64 | + unset($keys[$index]); |
| 65 | + } |
| 66 | + $keys_array[$idx] = ['*' => $this->array_keys_r(array_values($i)[0])]; |
| 67 | + continue; |
| 68 | + } |
| 69 | + if (is_array($i)) { |
| 70 | + if ($index = array_search($idx, $keys, true)) { |
| 71 | + unset($keys[$index]); |
| 72 | + } |
| 73 | + $keys_array[$idx] = $this->array_keys_r($i); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return array_merge(array_values($keys), $keys_array); |
| 78 | + } |
| 79 | + |
| 80 | + private function implode_recursive(string $separator, array $array, int $indent = 1): string |
| 81 | + { |
| 82 | + $string = ''; |
| 83 | + foreach ($array as $i => $a) { |
| 84 | + if (is_array($a)) { |
| 85 | + $string .= (str_repeat(' ', $indent)) |
| 86 | + . '\'' |
| 87 | + . $i |
| 88 | + . '\'' |
| 89 | + . ' => [' |
| 90 | + . PHP_EOL |
| 91 | + . $this->implode_recursive($separator, $a, $indent + 1) |
| 92 | + . PHP_EOL |
| 93 | + . (str_repeat(' ', $indent)) |
| 94 | + . ']'; |
| 95 | + $array1 = array_keys($array); |
| 96 | + if ($i !== end($array1)) { |
| 97 | + $string .= $separator . PHP_EOL; |
| 98 | + } |
| 99 | + } else { |
| 100 | + $string .= (str_repeat(' ', $indent)) . '\'' . $a . '\''; |
| 101 | + if ($i < count($array) - 1) { |
| 102 | + $string .= $separator . PHP_EOL; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $string; |
| 108 | + } |
| 109 | + |
| 110 | + private function is_array_of_arrays($array): bool |
| 111 | + { |
| 112 | + if (!is_array($array)) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + foreach ($array as $item) { |
| 116 | + if (!is_array($item)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + return true; |
| 121 | + } |
19 | 122 | }
|
0 commit comments