44
55use Nahid \QArray \Exceptions \ConditionNotAllowedException ;
66use Nahid \QArray \Exceptions \NullValueException ;
7+ use function DeepCopy \deep_copy ;
78
89abstract 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