|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Squareetlabs\LaravelToon\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
use Squareetlabs\LaravelToon\Services\ToonService; |
|
9
|
|
|
|
|
10
|
|
|
class ToonConvertCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
protected $signature = 'toon:convert |
|
13
|
|
|
{file : Ruta del archivo a convertir} |
|
14
|
|
|
{--format=readable : Formato de salida (readable, compact, tabular)} |
|
15
|
|
|
{--output= : Ruta del archivo de salida (opcional)} |
|
16
|
|
|
{--decode : Decodificar de TOON a JSON} |
|
17
|
|
|
{--pretty : Pretty print JSON}'; |
|
18
|
|
|
|
|
19
|
|
|
protected $description = 'Convierte archivos JSON a TOON o viceversa'; |
|
20
|
|
|
|
|
21
|
|
|
public function handle(ToonService $toonService): int |
|
22
|
|
|
{ |
|
23
|
|
|
$file = $this->argument('file'); |
|
24
|
|
|
$format = $this->option('format'); |
|
25
|
|
|
$output = $this->option('output'); |
|
26
|
|
|
$decode = $this->option('decode'); |
|
27
|
|
|
$pretty = $this->option('pretty'); |
|
28
|
|
|
|
|
29
|
|
|
if (!file_exists($file)) { |
|
30
|
|
|
$this->error("Archivo no encontrado: {$file}"); |
|
31
|
|
|
|
|
32
|
|
|
return self::FAILURE; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$content = file_get_contents($file); |
|
36
|
|
|
if (false === $content) { |
|
37
|
|
|
$this->error("No se pudo leer el archivo: {$file}"); |
|
38
|
|
|
|
|
39
|
|
|
return self::FAILURE; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->info('Procesando archivo...'); |
|
43
|
|
|
$this->newLine(); |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
if ($decode) { |
|
47
|
|
|
$result = $toonService->decode($content); |
|
48
|
|
|
if ($pretty) { |
|
49
|
|
|
$result = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
|
50
|
|
|
} else { |
|
51
|
|
|
$result = json_encode($result, JSON_THROW_ON_ERROR); |
|
52
|
|
|
} |
|
53
|
|
|
$outputFormat = 'JSON'; |
|
54
|
|
|
} else { |
|
55
|
|
|
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); |
|
56
|
|
|
$result = $toonService->convert($data, $format); |
|
57
|
|
|
$outputFormat = strtoupper($format).' TOON'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($output) { |
|
61
|
|
|
file_put_contents($output, $result); |
|
62
|
|
|
$this->info("✓ Archivo guardado: {$output}"); |
|
63
|
|
|
} else { |
|
64
|
|
|
$this->line($result); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->newLine(); |
|
68
|
|
|
$this->info("Conversión completada exitosamente a {$outputFormat}"); |
|
69
|
|
|
|
|
70
|
|
|
return self::SUCCESS; |
|
71
|
|
|
} catch (\Exception $e) { |
|
72
|
|
|
$this->error('Error durante la conversión: '.$e->getMessage()); |
|
73
|
|
|
|
|
74
|
|
|
return self::FAILURE; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|