Skip to content

Commit eab3a7e

Browse files
authored
Merge pull request #55 from Galbar/normalize_length_op
Normalize behavior of .length operation.
2 parents 38fdd37 + 3bc1722 commit eab3a7e

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/Galbar/JsonPath/JsonObject.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function getJson($options=0)
219219
public function get($jsonPath)
220220
{
221221
list($result, $hasDiverged) = JsonPath::get($this->jsonObject, $jsonPath);
222-
if ($this->smartGet && $result !== false && !$hasDiverged && is_array($result)) {
222+
if ($this->smartGet && $result !== false && !$hasDiverged) {
223223
return $result[0];
224224
}
225225
return $result;
@@ -251,7 +251,7 @@ public function getJsonObjects($jsonPath)
251251
$jsonObject->jsonObject = &$value;
252252
$objs[] = $jsonObject;
253253
}
254-
if ($this->smartGet && !$hasDiverged && is_array($result)) {
254+
if ($this->smartGet && !$hasDiverged) {
255255
return $objs[0];
256256
}
257257
return $objs;

src/Galbar/JsonPath/JsonPath.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ public static function subtreeGet(&$root, &$partial, $jsonPath, $createInexisten
4747
$newSelection = array_merge($newSelection, $result);
4848
}
4949
if (empty($newSelection) && Language\Token::LENGTH === $childName) {
50-
if (count($selection) > 1) {
51-
foreach ($selection as $item) {
52-
$newSelection[] = is_array($item) ? count($item) : strlen($item);
53-
}
54-
} else if (count($selection) == 1) {
55-
$newSelection = is_array($selection[0]) ? count($selection[0]) : strlen($selection[0]);
50+
foreach ($selection as $item) {
51+
$newSelection[] = is_array($item) ? count($item) : strlen($item);
5652
}
5753
}
5854
if (empty($newSelection)) {

tests/Galbar/JsonPath/JsonObjectLengthOperatorTest.php

+58-3
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,22 @@ public function testLength()
156156
$jsonObject = new JsonObject($this->json);
157157
$result = $jsonObject->get($jsonPath);
158158
$this->assertEquals(
159-
3,
159+
[3],
160160
$result
161161
);
162162

163163
/** String Length Test */
164164
$jsonPath = '$.music.bands[0].albums[0].length.length';
165165
$result = $jsonObject->get($jsonPath);
166166
$this->assertEquals(
167-
5,
167+
[5],
168168
$result
169169
);
170170

171171
$jsonPath = '$.music.bands[0].albums[1].title.length';
172172
$result = $jsonObject->get($jsonPath);
173173
$this->assertEquals(
174-
6,
174+
[6],
175175
$result
176176
);
177177
}
@@ -260,4 +260,59 @@ public function testArrayLength()
260260
$result
261261
);
262262
}
263+
264+
/**
265+
* @throws InvalidJsonException
266+
*/
267+
public function testArrayLengthSmartGet()
268+
{
269+
/** Arrays Count Length Test */
270+
$jsonPath = '$.music.bands[*].albums.length';
271+
$jsonObject = new JsonObject($this->json, true);
272+
$result = $jsonObject->get($jsonPath);
273+
$this->assertEquals(
274+
[
275+
3,
276+
3
277+
],
278+
$result
279+
);
280+
281+
/** String Length Test */
282+
$jsonPath = '$.music.bands[0].albums[*].length.length';
283+
$result = $jsonObject->get($jsonPath);
284+
$this->assertEquals(
285+
[
286+
5,
287+
5,
288+
6
289+
],
290+
$result
291+
);
292+
293+
$jsonPath = '$.music.bands[0].albums[*].title.length';
294+
$result = $jsonObject->get($jsonPath);
295+
$this->assertEquals(
296+
[
297+
5,
298+
6,
299+
5
300+
],
301+
$result
302+
);
303+
304+
$jsonPath = '$.music.bands[*].albums[*].length.length';
305+
$result = $jsonObject->get($jsonPath);
306+
$this->assertEquals(
307+
[
308+
5,
309+
5,
310+
6,
311+
6,
312+
6,
313+
6
314+
],
315+
$result
316+
);
317+
}
263318
}

0 commit comments

Comments
 (0)