Skip to content

Commit 3df099c

Browse files
authored
Merge pull request #5 from me-shaon/master
Some code refactored
2 parents 8580ba8 + d4a37b6 commit 3df099c

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

example/index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@
3636
$data = $json->collect([1, 2, 3, 4, 5])->nth(-2);
3737

3838
echo '<pre>';
39-
dump($data);
40-
*/
39+
dump($data);

src/JsonQueriable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ public function isJson($value, $return_map = false)
161161

162162
return (json_last_error() == JSON_ERROR_NONE) ? ($return_map ? $data : true) : json_last_error_msg();
163163
}
164+
protected function prepareResult($data, $object)
165+
{
166+
$output = [];
167+
if (is_array($data)) {
168+
foreach ($data as $key => $val) {
169+
if ($object) {
170+
$output[$key] = (object) $val;
171+
} else {
172+
$output[$key] = $val;
173+
}
174+
}
175+
} else {
176+
if ($object) {
177+
$output = json_decode(json_encode($data));
178+
} else {
179+
$output = json_decode(json_encode($data), true);
180+
}
181+
}
182+
183+
return $output;
184+
}
164185

165186
/**
166187
* read JSON data from file

src/Jsonq.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,8 @@ public function get($object = true)
9191
if (!$this->isMultiArray($this->_map)) {
9292
return (object) $this->_map;
9393
}
94-
$resultingData = [];
95-
foreach ($this->_map as $key=>$data) {
96-
if ($object) {
97-
$resultingData[$key] = (object) $data;
98-
} else {
99-
$resultingData[$key] = $data;
100-
}
101-
}
10294

103-
return $resultingData;
95+
return $this->prepareResult($this->_map, $object);
10496
}
10597

10698

@@ -125,7 +117,11 @@ public function fetch($object = true)
125117
*/
126118
public function exists()
127119
{
128-
return count($this->_map) > 0;
120+
if (!empty($this->_map) && !is_null($this->_map)) {
121+
return true;
122+
}
123+
124+
return false;
129125
}
130126

131127

@@ -282,11 +278,7 @@ public function first($object = true)
282278

283279
$data = $this->_map;
284280
if (count($data) > 0) {
285-
if ($object) {
286-
return json_decode(json_encode(reset($data)));
287-
}
288-
289-
return json_decode(json_encode(reset($data)), true);
281+
return $this->prepareResult(reset($data), $object);
290282
}
291283

292284
return null;
@@ -305,11 +297,7 @@ public function last($object = true)
305297

306298
$data = $this->_map;
307299
if (count($data) > 0) {
308-
if ($object) {
309-
return json_decode(json_encode(end($data)));
310-
}
311-
312-
return json_decode(json_encode(end($data)), true);
300+
return $this->prepareResult(end($data), $object);
313301
}
314302

315303
return null;
@@ -341,11 +329,7 @@ public function nth($index, $object = true)
341329
$result = $data[$this->count() + $index];
342330
}
343331

344-
if ($object) {
345-
return json_decode(json_encode($result));
346-
}
347-
348-
return json_decode(json_encode($result), true);
332+
return $this->prepareResult($result, $object);
349333
}
350334

351335
/**

0 commit comments

Comments
 (0)