Skip to content

Commit 67ca047

Browse files
author
Katalam
committed
Adding structure command
1 parent 84a181e commit 67ca047

File tree

1 file changed

+106
-3
lines changed

1 file changed

+106
-3
lines changed

src/Commands/JsonStructureCommand.php

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,120 @@
33
namespace Hyperlink\JsonStructure\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Http;
7+
use Illuminate\Support\Facades\Storage;
68

79
class JsonStructureCommand extends Command
810
{
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}';
1014

11-
public $description = 'My command';
15+
public $description = 'Converts a json structure to an array with the json keys';
1216

1317
public function handle(): int
1418
{
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');
1652

1753
return self::SUCCESS;
1854
}
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+
}
19122
}

0 commit comments

Comments
 (0)