Skip to content

Commit 2abf57b

Browse files
Merge pull request #2085 from redis/DOC-5674-php-json-examples
DOC-5674 added PHP JSON examples
2 parents dffe64b + 99853c2 commit 2abf57b

File tree

1 file changed

+395
-0
lines changed

1 file changed

+395
-0
lines changed

local_examples/php/DtJsonTest.php

Lines changed: 395 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,395 @@
1+
// EXAMPLE: json_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtJsonTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtJson() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START set_get
26+
$res1 = $r->jsonset('bike', '$', '"Hyperion"');
27+
echo $res1 . PHP_EOL;
28+
// >>> OK
29+
30+
$res2 = $r->jsonget('bike', '', '', '', '$');
31+
echo $res2 . PHP_EOL;
32+
// >>> ["Hyperion"]
33+
34+
$res3 = $r->jsontype('bike', '$');
35+
echo json_encode($res3) . PHP_EOL;
36+
// >>> ["string"]
37+
// STEP_END
38+
// REMOVE_START
39+
$this->assertEquals('OK', $res1);
40+
$this->assertEquals('["Hyperion"]', $res2);
41+
$this->assertEquals(['string'], $res3);
42+
// REMOVE_END
43+
44+
// STEP_START str
45+
$res4 = $r->jsonstrlen('bike', '$');
46+
echo json_encode($res4) . PHP_EOL;
47+
// >>> [8]
48+
49+
$res5 = $r->jsonstrappend('bike', '$', '" (Enduro bikes)"');
50+
echo json_encode($res5) . PHP_EOL;
51+
// >>> [23]
52+
53+
$res6 = $r->jsonget('bike', '', '', '', '$');
54+
echo $res6 . PHP_EOL;
55+
// >>> "Hyperion (Enduro bikes)"
56+
// STEP_END
57+
// REMOVE_START
58+
$this->assertEquals([8], $res4);
59+
$this->assertEquals([23], $res5);
60+
$this->assertEquals('["Hyperion (Enduro bikes)"]', $res6);
61+
// REMOVE_END
62+
63+
// STEP_START num
64+
$res7 = $r->jsonset('crashes', '$', '0');
65+
echo $res7 . PHP_EOL;
66+
// >>> OK
67+
68+
$res8 = $r->jsonnumincrby('crashes', '$', 1);
69+
echo $res8 . PHP_EOL;
70+
// >>> [1]
71+
72+
$res9 = $r->jsonnumincrby('crashes', '$', 1.5);
73+
echo $res9 . PHP_EOL;
74+
// >>> [2.5]
75+
76+
$res10 = $r->jsonnumincrby('crashes', '$', -0.75);
77+
echo $res10 . PHP_EOL;
78+
// >>> [1.75]
79+
// STEP_END
80+
// REMOVE_START
81+
$this->assertEquals('OK', $res7);
82+
$this->assertEquals('[1]', $res8);
83+
$this->assertEquals('[2.5]', $res9);
84+
$this->assertEquals('[1.75]', $res10);
85+
// REMOVE_END
86+
87+
// STEP_START arr
88+
$newbike = json_encode(["Deimos", ["crashes" => 0], null], JSON_THROW_ON_ERROR);
89+
$res11 = $r->jsonset('newbike', '$', $newbike);
90+
echo $res11 . PHP_EOL;
91+
// >>> OK
92+
93+
$res12 = $r->jsonget('newbike', '', '', '', '$');
94+
echo $res12 . PHP_EOL;
95+
// >>> [["Deimos",{"crashes":0},null]]
96+
97+
$res13 = $r->jsonget('newbike', '', '', '', '$[1].crashes');
98+
echo $res13 . PHP_EOL;
99+
// >>> 0
100+
101+
$res14 = $r->jsondel('newbike', '$.[-1]');
102+
echo $res14 . PHP_EOL;
103+
// >>> 1
104+
105+
$res15 = $r->jsonget('newbike', '', '', '', '$');
106+
echo $res15 . PHP_EOL;
107+
// >>> ["Deimos",{"crashes":0}]
108+
// STEP_END
109+
// REMOVE_START
110+
$this->assertEquals('OK', $res11);
111+
$this->assertEquals('[["Deimos",{"crashes":0},null]]', $res12);
112+
$this->assertEquals('[0]', $res13);
113+
$this->assertEquals(1, $res14);
114+
$this->assertEquals('[["Deimos",{"crashes":0}]]', $res15);
115+
// REMOVE_END
116+
117+
// STEP_START arr2
118+
$res16 = $r->jsonset('riders', '$', '[]');
119+
echo $res16 . PHP_EOL;
120+
// >>> OK
121+
122+
$res17 = $r->jsonarrappend('riders', '$', '"Norem"');
123+
echo json_encode($res17) . PHP_EOL;
124+
// >>> [1]
125+
126+
$res18 = $r->jsonget('riders', '', '', '', '$');
127+
echo $res18 . PHP_EOL;
128+
// >>> ["Norem"]
129+
130+
$res19 = $r->jsonarrinsert('riders', '$', 1, '"Prickett"', '"Royce"', '"Castilla"');
131+
echo json_encode($res19) . PHP_EOL;
132+
// >>> [4]
133+
134+
$res20 = $r->jsonget('riders', '', '', '', '$');
135+
echo $res20 . PHP_EOL;
136+
// >>> ["Norem","Prickett","Royce","Castilla"]
137+
138+
$res21 = $r->jsonarrtrim('riders', '$', 1, 1);
139+
echo json_encode($res21) . PHP_EOL;
140+
// >>> [1]
141+
142+
$res22 = $r->jsonget('riders', '', '', '', '$');
143+
echo $res22 . PHP_EOL;
144+
// >>> ["Prickett"]
145+
146+
$res23 = $r->jsonarrpop('riders', '$');
147+
echo json_encode($res23) . PHP_EOL;
148+
// >>> ["\"Prickett\""]
149+
150+
$res24 = $r->jsonarrpop('riders', '$');
151+
echo json_encode($res24) . PHP_EOL;
152+
// >>> [null]
153+
// STEP_END
154+
// REMOVE_START
155+
$this->assertEquals('OK', $res16);
156+
$this->assertEquals([1], $res17);
157+
$this->assertEquals('[["Norem"]]', $res18);
158+
$this->assertEquals([4], $res19);
159+
$this->assertEquals('[["Norem","Prickett","Royce","Castilla"]]', $res20);
160+
$this->assertEquals([1], $res21);
161+
$this->assertEquals('[["Prickett"]]', $res22);
162+
$this->assertEquals(['"Prickett"'], $res23);
163+
$this->assertEquals([null], $res24);
164+
// REMOVE_END
165+
166+
// STEP_START obj
167+
$bike1 = json_encode([
168+
'model' => 'Deimos',
169+
'brand' => 'Ergonom',
170+
'price' => 4972,
171+
], JSON_THROW_ON_ERROR);
172+
$res25 = $r->jsonset('bike:1', '$', $bike1);
173+
echo $res25 . PHP_EOL;
174+
// >>> OK
175+
176+
$res26 = $r->jsonobjlen('bike:1', '$');
177+
echo json_encode($res26) . PHP_EOL;
178+
// >>> [3]
179+
180+
$res27 = $r->jsonobjkeys('bike:1', '$');
181+
echo json_encode($res27) . PHP_EOL;
182+
// >>> [["model","brand","price"]]
183+
// STEP_END
184+
// REMOVE_START
185+
$this->assertEquals('OK', $res25);
186+
$this->assertEquals([3], $res26);
187+
$this->assertEquals([["model","brand","price"]], $res27);
188+
// REMOVE_END
189+
190+
// STEP_START set_bikes
191+
$inventory = [
192+
'inventory' => [
193+
'mountain_bikes' => [
194+
[
195+
'id' => 'bike:1',
196+
'model' => 'Phoebe',
197+
'description' => 'This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there’s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.',
198+
'price' => 1920,
199+
'specs' => ['material' => 'carbon', 'weight' => 13.1],
200+
'colors' => ['black', 'silver'],
201+
],
202+
[
203+
'id' => 'bike:2',
204+
'model' => 'Quaoar',
205+
'description' => "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.",
206+
'price' => 2072,
207+
'specs' => ['material' => 'aluminium', 'weight' => 7.9],
208+
'colors' => ['black', 'white'],
209+
],
210+
[
211+
'id' => 'bike:3',
212+
'model' => 'Weywot',
213+
'description' => 'This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you\'re after a budget option, this is one of the best bikes you could get.',
214+
'price' => 3264,
215+
'specs' => ['material' => 'alloy', 'weight' => 13.8],
216+
],
217+
],
218+
'commuter_bikes' => [
219+
[
220+
'id' => 'bike:4',
221+
'model' => 'Salacia',
222+
'description' => 'This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance. It’s for the rider who wants both efficiency and capability.',
223+
'price' => 1475,
224+
'specs' => ['material' => 'aluminium', 'weight' => 16.6],
225+
'colors' => ['black', 'silver'],
226+
],
227+
[
228+
'id' => 'bike:5',
229+
'model' => 'Mimas',
230+
'description' => 'A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.',
231+
'price' => 3941,
232+
'specs' => ['material' => 'alloy', 'weight' => 11.6],
233+
],
234+
],
235+
],
236+
];
237+
$res1b = $r->jsonset('bikes:inventory', '$', json_encode($inventory, JSON_THROW_ON_ERROR));
238+
echo $res1b . PHP_EOL;
239+
// >>> OK
240+
// STEP_END
241+
// REMOVE_START
242+
$this->assertEquals('OK', $res1b);
243+
// REMOVE_END
244+
245+
// STEP_START get_bikes
246+
$res2b = $r->jsonget('bikes:inventory', '', '', '', '$.inventory.*');
247+
echo $res2b . PHP_EOL;
248+
// >>> [{'id': 'bike:1', 'model': 'Phoebe',
249+
// STEP_END
250+
// REMOVE_START
251+
$this->assertIsString($res2b);
252+
$decoded2b = json_decode($res2b, true);
253+
$this->assertArrayHasKey('mountain_bikes', $inventory['inventory']);
254+
$this->assertArrayHasKey('commuter_bikes', $inventory['inventory']);
255+
// REMOVE_END
256+
257+
// STEP_START get_mtnbikes
258+
$res3b = $r->jsonget('bikes:inventory', '', '', '', '$.inventory.mountain_bikes[*].model');
259+
echo $res3b . PHP_EOL;
260+
// >>> ["Phoebe","Quaoar","Weywot"]
261+
262+
$res4b = $r->jsonget('bikes:inventory', '', '', '', '$.inventory["mountain_bikes"][*].model');
263+
echo $res4b . PHP_EOL;
264+
// >>> ["Phoebe","Quaoar","Weywot"]
265+
266+
$res5b = $r->jsonget('bikes:inventory', '', '', '', '$..mountain_bikes[*].model');
267+
echo $res5b . PHP_EOL;
268+
// >>> ["Phoebe","Quaoar","Weywot"]
269+
// STEP_END
270+
// REMOVE_START
271+
$this->assertEquals('["Phoebe","Quaoar","Weywot"]', $res3b);
272+
$this->assertEquals('["Phoebe","Quaoar","Weywot"]', $res4b);
273+
$this->assertEquals('["Phoebe","Quaoar","Weywot"]', $res5b);
274+
// REMOVE_END
275+
276+
// STEP_START get_models
277+
$res6b = $r->jsonget('bikes:inventory', '', '', '', '$..model');
278+
echo $res6b . PHP_EOL;
279+
// >>> ["Phoebe","Quaoar","Weywot","Salacia","Mimas"]
280+
// STEP_END
281+
// REMOVE_START
282+
$this->assertEquals('["Phoebe","Quaoar","Weywot","Salacia","Mimas"]', $res6b);
283+
// REMOVE_END
284+
285+
// STEP_START get2mtnbikes
286+
$res7b = $r->jsonget('bikes:inventory', '', '', '', '$..mountain_bikes[0:2].model');
287+
echo $res7b . PHP_EOL;
288+
// >>> ["Phoebe","Quaoar"]
289+
// STEP_END
290+
// REMOVE_START
291+
$this->assertEquals('["Phoebe","Quaoar"]', $res7b);
292+
// REMOVE_END
293+
294+
// STEP_START filter1
295+
$res8b = $r->jsonget(
296+
'bikes:inventory',
297+
'',
298+
'',
299+
'',
300+
'$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]'
301+
);
302+
echo $res8b . PHP_EOL;
303+
// >>> [{"id":"bike:2","model":"Quaoar",...}]
304+
// STEP_END
305+
// REMOVE_START
306+
$this->assertIsString($res8b);
307+
$decoded8b = json_decode($res8b, true);
308+
$this->assertNotEmpty($decoded8b);
309+
$this->assertEquals('Quaoar', $decoded8b[0]['model']);
310+
$this->assertEquals('aluminium', $decoded8b[0]['specs']['material']);
311+
// REMOVE_END
312+
313+
// STEP_START filter2
314+
$res9b = $r->jsonget('bikes:inventory', '', '', '', "$..[?(@.specs.material == 'alloy')].model");
315+
echo $res9b . PHP_EOL;
316+
// >>> ["Weywot","Mimas"]
317+
// STEP_END
318+
// REMOVE_START
319+
$this->assertEquals('["Weywot","Mimas"]', $res9b);
320+
// REMOVE_END
321+
322+
// STEP_START filter3
323+
$res10b = $r->jsonget('bikes:inventory', '', '', '', "$..[?(@.specs.material =~ '(?i)al')].model");
324+
echo $res10b . PHP_EOL;
325+
// >>> ["Quaoar","Weywot","Salacia","Mimas"]
326+
// STEP_END
327+
// REMOVE_START
328+
$this->assertEquals('["Quaoar","Weywot","Salacia","Mimas"]', $res10b);
329+
// REMOVE_END
330+
331+
// STEP_START filter4
332+
$r->jsonset('bikes:inventory', '$.inventory.mountain_bikes[0].regex_pat', '"(?i)al"');
333+
$r->jsonset('bikes:inventory', '$.inventory.mountain_bikes[1].regex_pat', '"(?i)al"');
334+
$r->jsonset('bikes:inventory', '$.inventory.mountain_bikes[2].regex_pat', '"(?i)al"');
335+
336+
$res14b = $r->jsonget(
337+
'bikes:inventory',
338+
'',
339+
'',
340+
'',
341+
'$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model'
342+
);
343+
echo $res14b . PHP_EOL;
344+
// >>> ["Quaoar","Weywot"]
345+
// STEP_END
346+
// REMOVE_START
347+
$this->assertEquals('["Quaoar","Weywot"]', $res14b);
348+
// REMOVE_END
349+
350+
// STEP_START update_bikes
351+
$res15b = $r->jsonget('bikes:inventory', '', '', '', '$..price');
352+
echo $res15b . PHP_EOL;
353+
// >>> [1920,2072,3264,1475,3941]
354+
355+
$res16b = $r->jsonnumincrby('bikes:inventory', '$..price', -100);
356+
echo json_encode($res16b) . PHP_EOL;
357+
// >>> [1820,1972,3164,1375,3841]
358+
359+
$res17b = $r->jsonnumincrby('bikes:inventory', '$..price', 100);
360+
echo json_encode($res17b) . PHP_EOL;
361+
// >>> [1920,2072,3264,1475,3941]
362+
// STEP_END
363+
// REMOVE_START
364+
$this->assertEquals('[1920,2072,3264,1475,3941]', $res15b);
365+
$this->assertEquals('[1820,1972,3164,1375,3841]', $res16b);
366+
$this->assertEquals('[1920,2072,3264,1475,3941]', $res17b);
367+
// REMOVE_END
368+
369+
// STEP_START update_filters1
370+
$res18b = $r->jsonset('bikes:inventory', '$.inventory.*[?(@.price<2000)].price', '1500');
371+
$res19b = $r->jsonget('bikes:inventory', '', '', '', '$..price');
372+
echo $res19b . PHP_EOL;
373+
// >>> [1500,2072,3264,1500,3941]
374+
// STEP_END
375+
// REMOVE_START
376+
$this->assertEquals('OK', $res18b);
377+
$this->assertEquals('[1500,2072,3264,1500,3941]', $res19b);
378+
// REMOVE_END
379+
380+
// STEP_START update_filters2
381+
$res20b = $r->jsonarrappend('bikes:inventory', '$.inventory.*[?(@.price<2000)].colors', '"pink"');
382+
echo json_encode($res20b) . PHP_EOL;
383+
// >>> [3,3]
384+
385+
$res21b = $r->jsonget('bikes:inventory', '', '', '', '$..[*].colors');
386+
echo $res21b . PHP_EOL;
387+
// >>> [["black","silver","pink"],["black","white"],["black","silver","pink"]]
388+
// STEP_END
389+
// REMOVE_START
390+
$this->assertEquals([3,3], $res20b);
391+
$this->assertEquals('[["black","silver","pink"],["black","white"],["black","silver","pink"]]', $res21b);
392+
// REMOVE_END
393+
}
394+
}
395+

0 commit comments

Comments
 (0)