Skip to content

Commit 1145e8e

Browse files
author
Katalam
committed
Moving functionality to a class
1 parent c6c1e10 commit 1145e8e

File tree

2 files changed

+86
-71
lines changed

2 files changed

+86
-71
lines changed

src/Actions/DecodeJsonStructure.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Hyperlink\JsonStructure\Actions;
4+
5+
class DecodeJsonStructure
6+
{
7+
public function __construct(private readonly array $array)
8+
{
9+
}
10+
11+
public function __invoke(): string
12+
{
13+
return '['.PHP_EOL.$this->implode_recursive(',', $this->array_keys_r($this->array)).PHP_EOL.']'.PHP_EOL;
14+
}
15+
16+
private function implode_recursive(string $separator, array $array, int $indent = 1): string
17+
{
18+
$string = '';
19+
foreach ($array as $i => $a) {
20+
if (is_array($a)) {
21+
$string .= (str_repeat(' ', $indent))
22+
.'\''
23+
.$i
24+
.'\''
25+
.' => ['
26+
.PHP_EOL
27+
.$this->implode_recursive($separator, $a, $indent + 1)
28+
.PHP_EOL
29+
.(str_repeat(' ', $indent))
30+
.']';
31+
$array1 = array_keys($array);
32+
if ($i !== end($array1)) {
33+
$string .= $separator.PHP_EOL;
34+
}
35+
} else {
36+
$string .= (str_repeat(' ', $indent)).'\''.$a.'\'';
37+
if ($i < count($array) - 1) {
38+
$string .= $separator.PHP_EOL;
39+
}
40+
}
41+
}
42+
43+
return $string;
44+
}
45+
46+
private function is_array_of_arrays($array): bool
47+
{
48+
if (! is_array($array)) {
49+
return false;
50+
}
51+
foreach ($array as $item) {
52+
if (! is_array($item)) {
53+
return false;
54+
}
55+
}
56+
57+
return true;
58+
}
59+
60+
private function array_keys_r($array): array
61+
{
62+
$keys = array_keys($array);
63+
$keys_array = [];
64+
65+
foreach ($array as $idx => $i) {
66+
if ($this->is_array_of_arrays($i)) {
67+
if ($index = array_search($idx, $keys, true)) {
68+
unset($keys[$index]);
69+
}
70+
$keys_array[$idx] = ['*' => $this->array_keys_r(array_values($i)[0])];
71+
72+
continue;
73+
}
74+
if (is_array($i)) {
75+
if ($index = array_search($idx, $keys, true)) {
76+
unset($keys[$index]);
77+
}
78+
$keys_array[$idx] = $this->array_keys_r($i);
79+
}
80+
}
81+
82+
return array_merge(array_values($keys), $keys_array);
83+
}
84+
}

src/Commands/JsonStructureCommand.php

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyperlink\JsonStructure\Commands;
44

5+
use Hyperlink\JsonStructure\Actions\DecodeJsonStructure;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\Http;
78
use Illuminate\Support\Facades\Storage;
@@ -20,7 +21,6 @@ public function handle(): int
2021

2122
// if ($this->argument('endpoint')) {
2223
$this->info('Getting json structure from endpoint...');
23-
2424
$response = Http::get(route($this->argument('endpoint')));
2525

2626
if ($response->failed()) {
@@ -44,7 +44,7 @@ public function handle(): int
4444
}
4545

4646
$this->info('Attempting to decode json structure...');
47-
$result = '['.PHP_EOL.$this->implode_recursive(',', $this->array_keys_r($json)).PHP_EOL.']'.PHP_EOL;
47+
$result = call_user_func(new DecodeJsonStructure($json));
4848
$this->comment('Decoding json structure successful');
4949

5050
$this->info('Writing result to file...');
@@ -53,73 +53,4 @@ public function handle(): int
5353

5454
return self::SUCCESS;
5555
}
56-
57-
private function array_keys_r($array): array
58-
{
59-
$keys = array_keys($array);
60-
$keys_array = [];
61-
62-
foreach ($array as $idx => $i) {
63-
if ($this->is_array_of_arrays($i)) {
64-
if ($index = array_search($idx, $keys, true)) {
65-
unset($keys[$index]);
66-
}
67-
$keys_array[$idx] = ['*' => $this->array_keys_r(array_values($i)[0])];
68-
69-
continue;
70-
}
71-
if (is_array($i)) {
72-
if ($index = array_search($idx, $keys, true)) {
73-
unset($keys[$index]);
74-
}
75-
$keys_array[$idx] = $this->array_keys_r($i);
76-
}
77-
}
78-
79-
return array_merge(array_values($keys), $keys_array);
80-
}
81-
82-
private function implode_recursive(string $separator, array $array, int $indent = 1): string
83-
{
84-
$string = '';
85-
foreach ($array as $i => $a) {
86-
if (is_array($a)) {
87-
$string .= (str_repeat(' ', $indent))
88-
.'\''
89-
.$i
90-
.'\''
91-
.' => ['
92-
.PHP_EOL
93-
.$this->implode_recursive($separator, $a, $indent + 1)
94-
.PHP_EOL
95-
.(str_repeat(' ', $indent))
96-
.']';
97-
$array1 = array_keys($array);
98-
if ($i !== end($array1)) {
99-
$string .= $separator.PHP_EOL;
100-
}
101-
} else {
102-
$string .= (str_repeat(' ', $indent)).'\''.$a.'\'';
103-
if ($i < count($array) - 1) {
104-
$string .= $separator.PHP_EOL;
105-
}
106-
}
107-
}
108-
109-
return $string;
110-
}
111-
112-
private function is_array_of_arrays($array): bool
113-
{
114-
if (! is_array($array)) {
115-
return false;
116-
}
117-
foreach ($array as $item) {
118-
if (! is_array($item)) {
119-
return false;
120-
}
121-
}
122-
123-
return true;
124-
}
12556
}

0 commit comments

Comments
 (0)