Skip to content

Commit 37f949d

Browse files
committed
0.8.0
1 parent 1883233 commit 37f949d

19 files changed

+132
-218
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
### 0.8.0
4+
🔻 Breaking changes ahead:
5+
6+
- Dropped support for PHP < 8.0
7+
- Removed deprecated method `JSONPath->data()`
8+
9+
310
### 0.7.5
411
- Added support for $.length
512
- Added trim to explode to support both 1,2,3 and 1, 2, 3 inputs

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This project aims to be a clean and simple implementation with the following goa
2020
## Installation
2121

2222
```bash
23-
composer require softcreatr/jsonpath:"^0.5 || ^0.7"
23+
composer require softcreatr/jsonpath:"^0.5 || ^0.7 || ^0.8"
2424
```
2525

2626
## JSONPath Examples

composer.json

Lines changed: 6 additions & 6 deletions
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.5",
4+
"version": "0.8.0",
55
"license": "MIT",
66
"authors": [
77
{
@@ -13,21 +13,21 @@
1313
{
1414
"name": "Sascha Greuel",
1515
"email": "[email protected]",
16-
"homepage": "http://1-2.dev",
16+
"homepage": "https://1-2.dev",
1717
"role": "Developer"
1818
}
1919
],
2020
"require": {
21-
"php": ">=7.1",
21+
"php": ">=8.0",
2222
"ext-json": "*"
2323
},
2424
"replace": {
2525
"flow/jsonpath": "*"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": ">=7.0",
29-
"roave/security-advisories": "dev-master",
30-
"squizlabs/php_codesniffer": "^3.5"
28+
"phpunit/phpunit": "^9.5",
29+
"roave/security-advisories": "dev-latest",
30+
"squizlabs/php_codesniffer": "^3.6"
3131
},
3232
"config": {
3333
"optimize-autoloader": true,

src/AccessHelper.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626

2727
class AccessHelper
2828
{
29-
/**
30-
* @param array|ArrayAccess $collection
31-
*/
32-
public static function collectionKeys($collection): array
29+
public static function collectionKeys(mixed $collection): array
3330
{
3431
if (is_object($collection)) {
3532
return array_keys(get_object_vars($collection));
@@ -38,18 +35,12 @@ public static function collectionKeys($collection): array
3835
return array_keys($collection);
3936
}
4037

41-
/**
42-
* @param array|ArrayAccess $collection
43-
*/
44-
public static function isCollectionType($collection): bool
38+
public static function isCollectionType(mixed $collection): bool
4539
{
4640
return is_array($collection) || is_object($collection);
4741
}
4842

49-
/**
50-
* @param array|ArrayAccess $collection
51-
*/
52-
public static function keyExists($collection, $key, bool $magicIsAllowed = false): bool
43+
public static function keyExists(mixed $collection, $key, bool $magicIsAllowed = false): bool
5344
{
5445
if ($magicIsAllowed && is_object($collection) && method_exists($collection, '__get')) {
5546
return true;
@@ -76,14 +67,9 @@ public static function keyExists($collection, $key, bool $magicIsAllowed = false
7667

7768
/**
7869
* @todo Optimize conditions
79-
*
80-
* @param array|ArrayAccess $collection
81-
* @noinspection NotOptimalIfConditionsInspection
8270
*/
83-
public static function getValue($collection, $key, bool $magicIsAllowed = false)
71+
public static function getValue(mixed $collection, $key, bool $magicIsAllowed = false)
8472
{
85-
$return = null;
86-
8773
if (
8874
$magicIsAllowed &&
8975
is_object($collection) &&
@@ -112,12 +98,8 @@ public static function getValue($collection, $key, bool $magicIsAllowed = false)
11298
/**
11399
* Find item in php collection by index
114100
* Written this way to handle instances ArrayAccess or Traversable objects
115-
*
116-
* @param array|ArrayAccess $collection
117-
*
118-
* @return mixed|null
119101
*/
120-
private static function getValueByIndex($collection, $key)
102+
private static function getValueByIndex(mixed $collection, $key): mixed
121103
{
122104
$i = 0;
123105

@@ -145,26 +127,21 @@ private static function getValueByIndex($collection, $key)
145127
return null;
146128
}
147129

148-
/**
149-
* @param array|ArrayAccess $collection
150-
*/
151-
public static function setValue(&$collection, $key, $value)
130+
public static function setValue(mixed &$collection, $key, $value)
152131
{
153132
if (is_object($collection) && !$collection instanceof ArrayAccess) {
154133
return $collection->$key = $value;
155134
}
156135

157136
if ($collection instanceof ArrayAccess) {
137+
/** @noinspection PhpVoidFunctionResultUsedInspection */
158138
return $collection->offsetSet($key, $value);
159139
}
160140

161141
return $collection[$key] = $value;
162142
}
163143

164-
/**
165-
* @param array|ArrayAccess $collection
166-
*/
167-
public static function unsetValue(&$collection, $key): void
144+
public static function unsetValue(mixed &$collection, $key): void
168145
{
169146
if (is_object($collection) && !$collection instanceof ArrayAccess) {
170147
unset($collection->$key);
@@ -180,11 +157,9 @@ public static function unsetValue(&$collection, $key): void
180157
}
181158

182159
/**
183-
* @param array|ArrayAccess $collection
184-
*
185160
* @throws JSONPathException
186161
*/
187-
public static function arrayValues($collection): array
162+
public static function arrayValues(array|ArrayAccess $collection): array
188163
{
189164
if (is_array($collection)) {
190165
return array_values($collection);

src/Filters/AbstractFilter.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,15 @@
1515

1616
abstract class AbstractFilter
1717
{
18-
/**
19-
* @var JSONPathToken
20-
*/
21-
protected $token;
22-
23-
/**
24-
* @var bool
25-
*/
26-
protected $magicIsAllowed = false;
27-
28-
/**
29-
* @param int|bool $options
30-
*/
31-
public function __construct(JSONPathToken $token, $options = false)
18+
protected JSONPathToken $token;
19+
20+
protected bool $magicIsAllowed = false;
21+
22+
public function __construct(JSONPathToken $token, int|bool $options = false)
3223
{
3324
$this->token = $token;
3425
$this->magicIsAllowed = (bool)($options & JSONPath::ALLOW_MAGIC);
3526
}
3627

37-
/**
38-
* @param array|ArrayAccess $collection
39-
*/
40-
abstract public function filter($collection): array;
28+
abstract public function filter(array|ArrayAccess $collection): array;
4129
}

src/Filters/IndexFilter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
class IndexFilter extends AbstractFilter
1616
{
1717
/**
18-
* @inheritDoc
19-
*
2018
* @throws JSONPathException
2119
*/
2220
public function filter($collection): array

src/Filters/IndexesFilter.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
class IndexesFilter extends AbstractFilter
1616
{
17-
/**
18-
* @inheritDoc
19-
*/
2017
public function filter($collection): array
2118
{
2219
$return = [];

src/Filters/QueryMatchFilter.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
use function is_string;
2020
use function preg_match;
2121
use function preg_replace;
22-
use function strpos;
22+
use function str_ends_with;
23+
use function str_starts_with;
2324
use function strtolower;
2425
use function substr;
2526

@@ -30,9 +31,6 @@ class QueryMatchFilter extends AbstractFilter
3031
(\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+))?
3132
';
3233

33-
/**
34-
* @inheritDoc
35-
*/
3634
public function filter($collection): array
3735
{
3836
preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);
@@ -51,7 +49,7 @@ public function filter($collection): array
5149
$comparisonValue = $matches['comparisonValue'] ?? null;
5250

5351
if (is_string($comparisonValue)) {
54-
if (strpos($comparisonValue, "[") === 0 && substr($comparisonValue, -1) === "]") {
52+
if (str_starts_with($comparisonValue, "[") && str_ends_with($comparisonValue, "]")) {
5553
$comparisonValue = substr($comparisonValue, 1, -1);
5654
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
5755
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
@@ -113,14 +111,14 @@ public function filter($collection): array
113111
$return[] = $value;
114112
}
115113

116-
if ($operator === 'in' && is_array($comparisonValue) && in_array($value1, $comparisonValue)) {
114+
if ($operator === 'in' && is_array($comparisonValue) && in_array($value1, $comparisonValue, false)) {
117115
$return[] = $value;
118116
}
119117

120118
if (
121119
($operator === 'nin' || $operator === '!in') &&
122120
is_array($comparisonValue) &&
123-
!in_array($value1, $comparisonValue)
121+
!in_array($value1, $comparisonValue, false)
124122
) {
125123
$return[] = $value;
126124
}

src/Filters/QueryResultFilter.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
class QueryResultFilter extends AbstractFilter
1919
{
2020
/**
21-
* @inheritDoc
22-
*
2321
* @throws JSONPathException
2422
*/
2523
public function filter($collection): array
@@ -36,22 +34,13 @@ public function filter($collection): array
3634
return [];
3735
}
3836

39-
switch ($matches['operator']) {
40-
case '+':
41-
$resultKey = $value + $matches['numeric'];
42-
break;
43-
case '*':
44-
$resultKey = $value * $matches['numeric'];
45-
break;
46-
case '-':
47-
$resultKey = $value - $matches['numeric'];
48-
break;
49-
case '/':
50-
$resultKey = $value / $matches['numeric'];
51-
break;
52-
default:
53-
throw new JSONPathException('Unsupported operator in expression');
54-
}
37+
$resultKey = match ($matches['operator']) {
38+
'+' => $value + $matches['numeric'],
39+
'*' => $value * $matches['numeric'],
40+
'-' => $value - $matches['numeric'],
41+
'/' => $value / $matches['numeric'],
42+
default => throw new JSONPathException('Unsupported operator in expression'),
43+
};
5544

5645
$result = [];
5746

src/Filters/RecursiveFilter.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
class RecursiveFilter extends AbstractFilter
1717
{
1818
/**
19-
* @inheritDoc
20-
*
2119
* @throws JSONPathException
2220
*/
2321
public function filter($collection): array
@@ -30,18 +28,14 @@ public function filter($collection): array
3028
}
3129

3230
/**
33-
* @param array|ArrayAccess $data
34-
*
3531
* @throws JSONPathException
3632
*/
37-
private function recurse(array &$result, $data): void
33+
private function recurse(array &$result, array|ArrayAccess $data): void
3834
{
3935
$result[] = $data;
4036

4137
if (AccessHelper::isCollectionType($data)) {
42-
foreach (AccessHelper::arrayValues($data) as $key => $value) {
43-
$results[] = $value;
44-
38+
foreach (AccessHelper::arrayValues($data) as $value) {
4539
if (AccessHelper::isCollectionType($value)) {
4640
$this->recurse($result, $value);
4741
}

0 commit comments

Comments
 (0)