Skip to content

Commit 9d42c99

Browse files
committed
update json
1 parent af4c1ac commit 9d42c99

File tree

10 files changed

+771
-9
lines changed

10 files changed

+771
-9
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^8.2",
2020
"ext-bcmath": "*",
21-
"ext-ctype": "*"
21+
"ext-ctype": "*",
22+
"ext-zlib": "*"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "^11.4.2"

composer.lock

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/json.php

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
// json.php
3+
4+
declare(strict_types=1);
5+
6+
// Include Composer's autoloader
7+
use Nejcc\PhpDatatypes\Composite\Json;
8+
use Nejcc\PhpDatatypes\Encoding\HuffmanEncoding;
9+
10+
include_once __DIR__ . '/../vendor/autoload.php';
11+
12+
// Sample JSON data
13+
$jsonData1 = '{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}';
14+
$jsonData2 = '{"users":[{"id":3,"name":"Charlie"},{"id":4,"name":"Diana"}]}';
15+
16+
// Initialize variables for outputs
17+
$examples = [];
18+
$errorMessage = '';
19+
20+
// Start output buffering to capture print_r outputs
21+
ob_start();
22+
23+
try {
24+
// 1. Create Json instances
25+
$json1 = new Json($jsonData1);
26+
$json2 = new Json($jsonData2);
27+
$examples[] = [
28+
'title' => 'Create Json Instances',
29+
'description' => 'We create two <code>Json</code> objects with different user data.',
30+
'code' => "\$json1 = new Json(\$jsonData1);\n\$json2 = new Json(\$jsonData2);",
31+
'output' => "Json1: " . $json1->getJson() . "\nJson2: " . $json2->getJson(),
32+
];
33+
34+
// // 2. Compare Json instances
35+
// $areEqual = $json1->compareWith($json2) ? 'Yes' : 'No';
36+
// $examples[] = [
37+
// 'title' => 'Compare Json Instances',
38+
// 'description' => 'We compare <code>json1</code> and <code>json2</code> to check if they are identical.',
39+
// 'code' => "\$areEqual = \$json1->compareWith(\$json2) ? 'Yes' : 'No';",
40+
// 'output' => "Are Json1 and Json2 identical? " . $areEqual,
41+
// ];
42+
43+
// 3. Serialize Json to Array
44+
$array1 = $json1->toArray();
45+
$examples[] = [
46+
'title' => 'Serialize Json1 to Array',
47+
'description' => 'We convert <code>json1</code> to a PHP array.',
48+
'code' => "\$array1 = \$json1->toArray();",
49+
'output' => print_r($array1, true),
50+
];
51+
52+
// 4. Deserialize Array to Json
53+
$jsonFromArray = Json::fromArray($array1);
54+
$examples[] = [
55+
'title' => 'Deserialize Array to Json',
56+
'description' => 'We create a new <code>Json</code> object from <code>array1</code>.',
57+
'code' => "\$jsonFromArray = Json::fromArray(\$array1);",
58+
'output' => "Json from Array: " . $jsonFromArray->getJson(),
59+
];
60+
61+
// // 5. Compress Json1 using HuffmanEncoding
62+
$huffmanEncoder = new HuffmanEncoding();
63+
$compressed = $json1->compress($huffmanEncoder);
64+
$examples[] = [
65+
'title' => 'Compress Json1 using HuffmanEncoding',
66+
'description' => 'We compress <code>json1</code> using <code>HuffmanEncoding</code>.',
67+
'code' => "\$huffmanEncoder = new HuffmanEncoding();\n\$compressed = \$json1->compress(\$huffmanEncoder);",
68+
'output' => "Compressed Json1 (hex): " . bin2hex($compressed),
69+
];
70+
71+
// // 6. Decompress the previously compressed data
72+
$decompressedJson = $json1->decompress($huffmanEncoder, $compressed);
73+
$examples[] = [
74+
'title' => 'Decompress the Compressed Data',
75+
'description' => 'We decompress the previously compressed data to retrieve the original JSON.',
76+
'code' => "\$decompressedJson = \$json1->decompress(\$huffmanEncoder, \$compressed);",
77+
'output' => "Decompressed Json: " . $decompressedJson->getJson(),
78+
];
79+
80+
// 7. Verify decompressed data matches original
81+
$isMatch = ($json1->toArray() === $decompressedJson->toArray()) ? 'Yes' : 'No';
82+
$examples[] = [
83+
'title' => 'Verify Decompressed Data',
84+
'description' => 'We check if the decompressed JSON matches the original <code>json1</code> data.',
85+
'code' => "\$isMatch = (\$json1->toArray() === \$decompressedJson->toArray()) ? 'Yes' : 'No';",
86+
'output' => "Does decompressed Json match original Json1? " . $isMatch,
87+
];
88+
//
89+
// 8. Update Json1 by adding a new user
90+
$updatedJson1 = $json1->update('users', array_merge($json1->toArray()['users'], [['id' => 5, 'name' => 'Eve']]));
91+
$examples[] = [
92+
'title' => 'Update Json1 by Adding a New User',
93+
'description' => 'We add a new user to <code>json1</code>.',
94+
'code' => "\$updatedJson1 = \$json1->update('users', array_merge(\$json1->toArray()['users'], [['id' => 5, 'name' => 'Eve']]));",
95+
'output' => "Updated Json1: " . $updatedJson1->getJson(),
96+
];
97+
//
98+
// 9. Remove a user from updated Json1
99+
$modifiedJson1 = $updatedJson1->remove('users', 2); // Assuming remove method removes by 'id' or index
100+
$examples[] = [
101+
'title' => 'Remove a User from Updated Json1',
102+
'description' => 'We remove the user with ID 2 from <code>updatedJson1</code>.',
103+
'code' => "\$modifiedJson1 = \$updatedJson1->remove('users', 2);",
104+
'output' => "Modified Json1: " . $modifiedJson1->getJson(),
105+
];
106+
107+
} catch (InvalidArgumentException|JsonException $e) {
108+
$errorMessage = $e->getMessage();
109+
}
110+
111+
// Capture all outputs
112+
$content = ob_get_clean();
113+
114+
?>
115+
116+
<!DOCTYPE html>
117+
<html lang="en">
118+
<head>
119+
<meta charset="UTF-8">
120+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
121+
<title>Json Class Test Example</title>
122+
123+
<!-- TailwindCSS CDN for Styling -->
124+
<script src="https://cdn.tailwindcss.com"></script>
125+
126+
<!-- Prism.js for Syntax Highlighting -->
127+
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/themes/prism.min.css" rel="stylesheet"/>
128+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/prism.min.js"></script>
129+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/components/prism-php.min.js"></script>
130+
</head>
131+
<body class="bg-gray-100 p-8">
132+
<div class="max-w-7xl mx-auto">
133+
<h1 class="text-4xl font-bold text-center mb-8">Json Class Test Example</h1>
134+
135+
<?php if (!empty($errorMessage)) : ?>
136+
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-6" role="alert">
137+
<strong class="font-bold">Error:</strong>
138+
<span class="block sm:inline"><?php echo htmlspecialchars($errorMessage); ?></span>
139+
</div>
140+
<?php endif; ?>
141+
142+
<?php foreach ($examples as $example) : ?>
143+
<div class="bg-white shadow-md rounded-lg mb-6">
144+
<div class="px-6 py-4">
145+
<h2 class="text-2xl font-semibold mb-2"><?php echo htmlspecialchars($example['title']); ?></h2>
146+
<p class="text-gray-700 mb-4"><?php echo htmlspecialchars($example['description']); ?></p>
147+
148+
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
149+
<!-- Code Block -->
150+
<div>
151+
<h3 class="text-xl font-medium mb-2">Code</h3>
152+
<pre class="language-php bg-gray-800 text-white p-4 rounded"><code class="language-php"><?php echo htmlspecialchars($example['code']); ?></code></pre>
153+
</div>
154+
<!-- Output Block -->
155+
<div>
156+
<h3 class="text-xl font-medium mb-2">Output</h3>
157+
<pre class="bg-gray-100 text-gray-800 p-4 rounded"><?php echo htmlspecialchars($example['output']); ?></pre>
158+
</div>
159+
</div>
160+
</div>
161+
</div>
162+
<?php endforeach; ?>
163+
</div>
164+
</body>
165+
</html>

0 commit comments

Comments
 (0)