Skip to content

Commit 619ab3b

Browse files
committed
fixed: deep clone issue
1 parent 9d87234 commit 619ab3b

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.5.0"
15+
"php": ">=5.5.0",
16+
"myclabs/deep-copy": "^1.8"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "^4.8 || ^5.0",

data.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id,name,cat,price
2+
1,iPhone,1,80000
3+
2,macbook pro,2,150000
4+
3,Redmi 3S Prime,1,12000
5+
4,Redmi 4X,1,15000
6+
5,macbook air,2,110000

src/Queriable.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ protected function prepareResult($data)
281281

282282
if ($this->isMultiArray($data)) {
283283
foreach ($data as $key => $val) {
284-
$output[$key] = $this->instanceWithValue($val, ['_select' => $this->_select, '_except' => $this->_except]);
284+
if (is_array($val)) {
285+
$output[$key] = $this->instanceWithValue($val, ['_select' => $this->_select, '_except' => $this->_except]);
286+
} else {
287+
$output[$key] = $val;
288+
}
285289
}
286290
} else {
287291
$value = json_decode(json_encode($this->takeColumn($data)), true);
@@ -302,8 +306,8 @@ protected function prepareResult($data)
302306
*/
303307
protected function instanceWithValue($value, $meta = [])
304308
{
305-
$this->fresh($meta);
306309
$instance = $this->copy();
310+
$instance->fresh($meta);
307311
$value = $instance->takeColumn($value);
308312
return $instance->collect($value);
309313
}

src/QueryEngine.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Nahid\QArray\Exceptions\ConditionNotAllowedException;
66
use Nahid\QArray\Exceptions\NullValueException;
7+
use function DeepCopy\deep_copy;
78

89
abstract class QueryEngine implements \Countable, \Iterator
910
{
@@ -128,8 +129,7 @@ public function copy($fresh = false)
128129
if ($fresh) {
129130
$this->fresh();
130131
}
131-
132-
return clone $this;
132+
return deep_copy($this);
133133
}
134134

135135
/**
@@ -240,8 +240,12 @@ public function get($column = [])
240240
}
241241

242242
$this->prepare();
243+
$prepare = $this->prepareResult($this->_map);
244+
245+
$result = $prepare->reset($prepare->_map, true);
246+
$this->collect($this->_baseContents);
243247

244-
return $this->prepareResult($this->_map);
248+
return $result;
245249
}
246250

247251
/**
@@ -304,18 +308,18 @@ public function exists()
304308
*/
305309
public function reset($data = null, $fresh = false)
306310
{
307-
if (!is_null($data)) {
308-
$this->_baseContents = $data;
309-
}
310311

311312
if ($fresh) {
312-
$self = $this->copy($fresh);
313-
$self->collect($this->_baseContents);
313+
$self = new static();
314+
$self->collect($data);
314315

315316
return $self;
316317
}
317318

318-
$this->_map = $this->_baseContents;
319+
if (!is_null($data)) {
320+
$this->collect($data);
321+
}
322+
319323
$this->reProcess();
320324

321325
return $this;
@@ -500,7 +504,11 @@ public function first($column = [])
500504
}
501505

502506
if (count($data) > 0) {
503-
return $this->prepareResult(reset($data));
507+
$prepare = $this->prepareResult(reset($data));
508+
$result = $prepare->reset($prepare->_map, true);
509+
$this->collect($this->_baseContents);
510+
511+
return $result;
504512
}
505513

506514
return null;
@@ -521,7 +529,11 @@ public function last($column = [])
521529
$this->_select = $column;
522530

523531
if (count($data) > 0) {
524-
return $this->prepareResult(end($data));
532+
$prepare = $this->prepareResult(end($data));
533+
$result = $prepare->reset($prepare->_map, true);
534+
$this->collect($this->_baseContents);
535+
536+
return $result;
525537
}
526538

527539
return null;
@@ -554,7 +566,12 @@ public function nth($index, $column = [])
554566
$result = $data[$this->count() + $index];
555567
}
556568

557-
return $this->prepareResult($result);
569+
$prepare = $this->prepareResult($result);
570+
$result = $prepare->reset($prepare->_map, true);
571+
$this->collect($this->_baseContents);
572+
573+
return $result;
574+
558575
}
559576

560577
/**
@@ -666,7 +683,11 @@ public function transform(callable $fn)
666683
$new_data[$key] = $fn($val);
667684
}
668685

669-
return $this->prepareResult($new_data, false);
686+
$prepare = $this->prepareResult($new_data, false);
687+
$result = $prepare->reset($prepare->_map, true);
688+
$this->collect($this->_baseContents);
689+
690+
return $result;
670691
}
671692

672693
/**
@@ -715,7 +736,12 @@ public function filter(callable $fn, $key = false)
715736
}
716737
}
717738

718-
return $this->prepareResult($data, false);
739+
$prepare = $this->prepareResult($data, false);
740+
$result = $prepare->reset($prepare->_map, true);
741+
$this->collect($this->_baseContents);
742+
743+
return $result;
744+
719745
}
720746

721747
/**
@@ -761,8 +787,9 @@ public function json($data)
761787
public function collect($data)
762788
{
763789
$data = $this->objectToArray($data);
764-
$this->_map = $data;
765-
$this->_baseContents = $data;
790+
$this->_map = deep_copy($data);
791+
$this->_baseContents = deep_copy($data);
792+
$this->_isProcessed = false;
766793

767794
return $this;
768795
}
@@ -837,7 +864,7 @@ public function toJson()
837864
{
838865
$this->prepare();
839866

840-
return json_encode($this->_map);
867+
return json_encode($this->toArray());
841868
}
842869

843870
/**

0 commit comments

Comments
 (0)