Skip to content

Commit 3a03780

Browse files
authored
Merge pull request #65 from warlof/fix-php7.4
fix: make the class compliant with php 7.4+
2 parents d70df52 + 947c00f commit 3a03780

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "softcreatr/jsonpath",
33
"description": "JSONPath implementation for parsing, searching and flattening arrays",
4-
"version": "0.7.2",
4+
"version": "0.7.3",
55
"license": "MIT",
66
"authors": [
77
{
@@ -54,6 +54,7 @@
5454
},
5555
"support": {
5656
"email": "[email protected]",
57+
"forum": "https://github.com/SoftCreatR/JSONPath/discussions",
5758
"issues": "https://github.com/SoftCreatR/JSONPath/issues",
5859
"source": "https://github.com/SoftCreatR/JSONPath"
5960
}

src/AccessHelper.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ public static function keyExists($collection, $key, bool $magicIsAllowed = false
5959
$key = abs($key);
6060
}
6161

62-
if (is_array($collection) || $collection instanceof ArrayAccess) {
62+
if (is_array($collection)) {
6363
return array_key_exists($key, $collection);
6464
}
6565

66+
if ($collection instanceof ArrayAccess) {
67+
return $collection->offsetExists($key);
68+
}
69+
6670
if (is_object($collection)) {
6771
return property_exists($collection, (string)$key);
6872
}
@@ -88,6 +92,8 @@ public static function getValue($collection, $key, bool $magicIsAllowed = false)
8892
$return = $collection->__get($key);
8993
} elseif (is_object($collection) && !$collection instanceof ArrayAccess) {
9094
$return = $collection->$key;
95+
} elseif ($collection instanceof ArrayAccess) {
96+
$return = $collection->offsetGet($key);
9197
} elseif (is_array($collection)) {
9298
if (is_int($key) && $key < 0) {
9399
$return = array_slice($collection, $key, 1, false)[0];
@@ -148,6 +154,10 @@ public static function setValue(&$collection, $key, $value)
148154
return $collection->$key = $value;
149155
}
150156

157+
if ($collection instanceof ArrayAccess) {
158+
return $collection->offsetSet($key, $value);
159+
}
160+
151161
return $collection[$key] = $value;
152162
}
153163

@@ -158,7 +168,13 @@ public static function unsetValue(&$collection, $key): void
158168
{
159169
if (is_object($collection) && !$collection instanceof ArrayAccess) {
160170
unset($collection->$key);
161-
} else {
171+
}
172+
173+
if ($collection instanceof ArrayAccess) {
174+
$collection->offsetUnset($key);
175+
}
176+
177+
if (is_array($collection)) {
162178
unset($collection[$key]);
163179
}
164180
}

tests/JSONPathArrayAccessTest.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
namespace Flow\JSONPath\Test;
1212

13+
use ArrayObject;
1314
use Exception;
1415
use Flow\JSONPath\JSONPath;
1516
use Flow\JSONPath\Test\Traits\TestDataTrait;
1617
use PHPUnit\Framework\TestCase;
1718

1819
use function is_array;
19-
use function random_int;
2020

2121
class JSONPathArrayAccessTest extends TestCase
2222
{
@@ -27,7 +27,8 @@ class JSONPathArrayAccessTest extends TestCase
2727
*/
2828
public function testChaining(): void
2929
{
30-
$jsonPath = (new JSONPath($this->getData('conferences')));
30+
$container = new ArrayObject($this->getData('conferences'));
31+
$jsonPath = new JSONPath($container);
3132

3233
$teams = $jsonPath
3334
->find('.conferences.*')
@@ -55,9 +56,9 @@ public function testChaining(): void
5556
*/
5657
public function testIterating(): void
5758
{
58-
$data = $this->getData('conferences');
59+
$container = new ArrayObject($this->getData('conferences'));
5960

60-
$conferences = (new JSONPath($data))
61+
$conferences = (new JSONPath($container))
6162
->find('.conferences.*');
6263

6364
$names = [];
@@ -75,11 +76,14 @@ public function testIterating(): void
7576
}
7677

7778
/**
78-
* @throws Exception
79+
* @param bool $asArray
80+
* @testWith [false]
81+
* [true]
7982
*/
80-
public function testDifferentStylesOfAccess(): void
83+
public function testDifferentStylesOfAccess(bool $asArray): void
8184
{
82-
$data = (new JSONPath($this->getData('conferences', random_int(0, 1))));
85+
$container = new ArrayObject($this->getData('conferences', $asArray));
86+
$data = new JSONPath($container);
8387

8488
self::assertArrayHasKey('conferences', $data);
8589

@@ -91,4 +95,13 @@ public function testDifferentStylesOfAccess(): void
9195
self::assertEquals('Western Conference', $conferences[0]->name);
9296
}
9397
}
98+
99+
public function testUpdate(): void
100+
{
101+
$container = new ArrayObject($this->getData('conferences'));
102+
$data = new JSONPath($container);
103+
104+
$data->offsetSet('name', 'Major League Football');
105+
self::assertEquals('Major League Football', $data->name);
106+
}
94107
}

tests/JSONPathArrayTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/**
4+
* JSONPath implementation for PHP.
5+
*
6+
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Flow\JSONPath\Test;
12+
13+
use Exception;
14+
use Flow\JSONPath\JSONPath;
15+
use Flow\JSONPath\Test\Traits\TestDataTrait;
16+
use PHPUnit\Framework\TestCase;
17+
18+
use function is_array;
19+
use function random_int;
20+
21+
class JSONPathArrayTest extends TestCase
22+
{
23+
use TestDataTrait;
24+
25+
/**
26+
* @throws Exception
27+
*/
28+
public function testChaining(): void
29+
{
30+
$jsonPath = (new JSONPath($this->getData('conferences')));
31+
32+
$teams = $jsonPath
33+
->find('.conferences.*')
34+
->find('..teams.*');
35+
36+
self::assertEquals('Dodger', $teams[0]['name']);
37+
self::assertEquals('Mets', $teams[1]['name']);
38+
39+
$teams = $jsonPath
40+
->find('.conferences.*')
41+
->find('..teams.*');
42+
43+
self::assertEquals('Dodger', $teams[0]['name']);
44+
self::assertEquals('Mets', $teams[1]['name']);
45+
46+
$teams = $jsonPath
47+
->find('.conferences..teams.*');
48+
49+
self::assertEquals('Dodger', $teams[0]['name']);
50+
self::assertEquals('Mets', $teams[1]['name']);
51+
}
52+
53+
/**
54+
* @throws Exception
55+
*/
56+
public function testIterating(): void
57+
{
58+
$data = $this->getData('conferences');
59+
60+
$conferences = (new JSONPath($data))
61+
->find('.conferences.*');
62+
63+
$names = [];
64+
65+
foreach ($conferences as $conference) {
66+
$players = $conference
67+
->find('.teams.*.players[?(@.active=yes)]');
68+
69+
foreach ($players as $player) {
70+
$names[] = $player->name;
71+
}
72+
}
73+
74+
self::assertEquals(['Joe Face', 'something'], $names);
75+
}
76+
77+
/**
78+
* @throws Exception
79+
*/
80+
public function testDifferentStylesOfAccess(): void
81+
{
82+
$data = (new JSONPath($this->getData('conferences', random_int(0, 1))));
83+
84+
self::assertArrayHasKey('conferences', $data);
85+
86+
$conferences = $data->__get('conferences')->getData();
87+
88+
if (is_array($conferences[0])) {
89+
self::assertEquals('Western Conference', $conferences[0]['name']);
90+
} else {
91+
self::assertEquals('Western Conference', $conferences[0]->name);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)