|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Squareetlabs\LaravelToon\Services; |
|
6
|
|
|
|
|
7
|
|
|
use Squareetlabs\LaravelToon\Toon\EncodeOptions; |
|
8
|
|
|
use Squareetlabs\LaravelToon\Toon\Toon; |
|
9
|
|
|
|
|
10
|
|
|
class ToonService |
|
11
|
|
|
{ |
|
12
|
|
|
public function encode(mixed $data, ?EncodeOptions $options = null): string |
|
13
|
|
|
{ |
|
14
|
|
|
return Toon::encode($data, $options ?? new EncodeOptions()); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function encodeCompact(mixed $data): string |
|
18
|
|
|
{ |
|
19
|
|
|
return Toon::encodeCompact($data); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function encodeReadable(mixed $data): string |
|
23
|
|
|
{ |
|
24
|
|
|
return Toon::encodeReadable($data); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function encodeTabular(mixed $data): string |
|
28
|
|
|
{ |
|
29
|
|
|
return Toon::encodeTabular($data); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function decode(string $toon): mixed |
|
33
|
|
|
{ |
|
34
|
|
|
return Toon::decode($toon); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function convert(mixed $data, string $format = 'readable'): string |
|
38
|
|
|
{ |
|
39
|
|
|
return match ($format) { |
|
40
|
|
|
'compact' => $this->encodeCompact($data), |
|
41
|
|
|
'tabular' => $this->encodeTabular($data), |
|
42
|
|
|
'readable' => $this->encodeReadable($data), |
|
43
|
|
|
default => $this->encode($data), |
|
44
|
|
|
}; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function estimateTokens(string $content): array |
|
48
|
|
|
{ |
|
49
|
|
|
$chars = strlen($content); |
|
50
|
|
|
$words = str_word_count($content); |
|
51
|
|
|
$charsPerToken = config('laravel-toon.token_analysis.chars_per_token', 4); |
|
52
|
|
|
$estimatedTokens = (int)ceil($chars / $charsPerToken); |
|
53
|
|
|
|
|
54
|
|
|
return [ |
|
55
|
|
|
'words' => $words, |
|
56
|
|
|
'chars' => $chars, |
|
57
|
|
|
'tokens_estimate' => $estimatedTokens, |
|
58
|
|
|
'method' => 'character_ratio', |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function compress(mixed $data): array |
|
63
|
|
|
{ |
|
64
|
|
|
$json = json_encode($data, JSON_THROW_ON_ERROR); |
|
65
|
|
|
$toon = $this->encodeReadable($data); |
|
66
|
|
|
|
|
67
|
|
|
$jsonSize = strlen($json); |
|
68
|
|
|
$toonSize = strlen($toon); |
|
69
|
|
|
$bytesReduced = $jsonSize - $toonSize; |
|
70
|
|
|
$percentReduced = $jsonSize > 0 ? (($bytesReduced / $jsonSize) * 100) : 0; |
|
71
|
|
|
|
|
72
|
|
|
$jsonTokens = $this->estimateTokens($json)['tokens_estimate']; |
|
73
|
|
|
$toonTokens = $this->estimateTokens($toon)['tokens_estimate']; |
|
74
|
|
|
$tokensReduced = $jsonTokens - $toonTokens; |
|
75
|
|
|
$tokenPercentReduced = $jsonTokens > 0 ? (($tokensReduced / $jsonTokens) * 100) : 0; |
|
76
|
|
|
|
|
77
|
|
|
return [ |
|
78
|
|
|
'success' => true, |
|
79
|
|
|
'original' => [ |
|
80
|
|
|
'format' => 'json', |
|
81
|
|
|
'size_bytes' => $jsonSize, |
|
82
|
|
|
'content' => $json, |
|
83
|
|
|
'tokens_estimate' => $jsonTokens, |
|
84
|
|
|
], |
|
85
|
|
|
'compressed' => [ |
|
86
|
|
|
'format' => 'toon', |
|
87
|
|
|
'size_bytes' => $toonSize, |
|
88
|
|
|
'content' => $toon, |
|
89
|
|
|
'tokens_estimate' => $toonTokens, |
|
90
|
|
|
], |
|
91
|
|
|
'metrics' => [ |
|
92
|
|
|
'bytes_reduced' => $bytesReduced, |
|
93
|
|
|
'percent_reduced' => round($percentReduced, 2), |
|
94
|
|
|
'tokens_reduced' => $tokensReduced, |
|
95
|
|
|
'token_percent_reduced' => round($tokenPercentReduced, 2), |
|
96
|
|
|
'compression_ratio' => $jsonSize > 0 ? round($toonSize / $jsonSize, 3) : 0, |
|
97
|
|
|
], |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function analyzeCompression(mixed $original): array |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->compress($original); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function calculateCompressionRatio(mixed $original): float |
|
107
|
|
|
{ |
|
108
|
|
|
$json = json_encode($original, JSON_THROW_ON_ERROR); |
|
109
|
|
|
$toon = $this->encodeReadable($original); |
|
110
|
|
|
|
|
111
|
|
|
$jsonSize = strlen($json); |
|
112
|
|
|
if (0 === $jsonSize) { |
|
113
|
|
|
return 0; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return round(strlen($toon) / $jsonSize, 3); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function compareWithJson(mixed $data): array |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->compress($data); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getMetrics(mixed $data): array |
|
125
|
|
|
{ |
|
126
|
|
|
$compressed = $this->compress($data); |
|
127
|
|
|
|
|
128
|
|
|
return [ |
|
129
|
|
|
'data' => $data, |
|
130
|
|
|
'json_size_bytes' => $compressed['original']['size_bytes'], |
|
131
|
|
|
'json_tokens' => $compressed['original']['tokens_estimate'], |
|
132
|
|
|
'toon_size_bytes' => $compressed['compressed']['size_bytes'], |
|
133
|
|
|
'toon_tokens' => $compressed['compressed']['tokens_estimate'], |
|
134
|
|
|
'bytes_saved' => $compressed['metrics']['bytes_reduced'], |
|
135
|
|
|
'bytes_saved_percent' => $compressed['metrics']['percent_reduced'], |
|
136
|
|
|
'tokens_saved' => $compressed['metrics']['tokens_reduced'], |
|
137
|
|
|
'tokens_saved_percent' => $compressed['metrics']['token_percent_reduced'], |
|
138
|
|
|
'compression_ratio' => $compressed['metrics']['compression_ratio'], |
|
139
|
|
|
'original_format' => $compressed['original']['content'], |
|
140
|
|
|
'compressed_format' => $compressed['compressed']['content'], |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|