Skip to content

Commit 38fdd37

Browse files
authored
Merge pull request #54 from Galbar/fix_53_revert_length_functionality
Fix #53: revert .length operator functionality to avoid breaking change
2 parents 39a56a3 + 2d53dc6 commit 38fdd37

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
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) {
222+
if ($this->smartGet && $result !== false && !$hasDiverged && is_array($result)) {
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) {
254+
if ($this->smartGet && !$hasDiverged && is_array($result)) {
255255
return $objs[0];
256256
}
257257
return $objs;

src/Galbar/JsonPath/JsonPath.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ public static function subtreeGet(&$root, &$partial, $jsonPath, $createInexisten
4747
$newSelection = array_merge($newSelection, $result);
4848
}
4949
if (empty($newSelection) && Language\Token::LENGTH === $childName) {
50-
foreach ($selection as $item) {
51-
$newSelection[] = is_array($item) ? count($item) : strlen($item);
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]);
5256
}
5357
}
5458
if (empty($newSelection)) {

tests/Galbar/JsonPath/JsonObjectLengthOperatorTest.php

+3-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
}

0 commit comments

Comments
 (0)