@@ -518,20 +518,21 @@ public function callback()
518
518
/**
519
519
* Load a collection from source.
520
520
*
521
- * @param string|null $ident Optional. A pre-defined list to use from the model.
522
- * @param callable $callback Optional. Apply a callback to every entity of the collection .
521
+ * @param string|null $ident Optional. A pre-defined list to use from the model.
522
+ * @param callable|null $callback Process each entity after applying raw data .
523
523
* Leave blank to use {@see CollectionLoader::callback()}.
524
+ * @param callable|null $before Process each entity before applying raw data.
524
525
* @throws Exception If the database connection fails.
525
526
* @return ModelInterface[]|ArrayAccess
526
527
*/
527
- public function load ($ ident = null , callable $ callback = null )
528
+ public function load ($ ident = null , callable $ callback = null , callable $ before = null )
528
529
{
529
530
// Unused.
530
531
unset($ ident );
531
532
532
533
$ query = $ this ->source ()->sqlLoad ();
533
534
534
- return $ this ->loadFromQuery ($ query , $ callback );
535
+ return $ this ->loadFromQuery ($ query , $ callback, $ before );
535
536
}
536
537
537
538
/**
@@ -575,15 +576,16 @@ public function loadCount()
575
576
* ]);
576
577
* ```
577
578
*
578
- * @param string|array $query The SQL query as a string or an array composed of the query,
579
+ * @param string|array $query The SQL query as a string or an array composed of the query,
579
580
* parameter binds, and types of parameter bindings.
580
- * @param callable $callback Optional. Apply a callback to every entity of the collection .
581
+ * @param callable|null $callback Process each entity after applying raw data .
581
582
* Leave blank to use {@see CollectionLoader::callback()}.
583
+ * @param callable|null $before Process each entity before applying raw data.
582
584
* @throws RuntimeException If the database connection fails.
583
585
* @throws InvalidArgumentException If the SQL string/set is invalid.
584
586
* @return ModelInterface[]|ArrayAccess
585
587
*/
586
- public function loadFromQuery ($ query , callable $ callback = null )
588
+ public function loadFromQuery ($ query , callable $ callback = null , callable $ before = null )
587
589
{
588
590
$ db = $ this ->source ()->db ();
589
591
@@ -612,40 +614,27 @@ public function loadFromQuery($query, callable $callback = null)
612
614
613
615
$ sth ->setFetchMode (PDO ::FETCH_ASSOC );
614
616
615
- return $ this ->processCollection ($ sth , $ callback );
617
+ if ($ callback === null ) {
618
+ $ callback = $ this ->callback ();
619
+ }
620
+
621
+ return $ this ->processCollection ($ sth , $ before , $ callback );
616
622
}
617
623
618
624
/**
619
625
* Process the collection of raw data.
620
626
*
621
- * @param ModelInterface[]|Traversable $results The raw result set.
622
- * @param callable $callback Optional. Apply a callback to every entity of the collection.
623
- * Leave blank to use {@see CollectionLoader::callback()}.
624
- * @throws InvalidArgumentException If the SQL string/set is invalid.
627
+ * @param mixed[]|Traversable $results The raw result set.
628
+ * @param callable|null $before Process each entity before applying raw data.
629
+ * @param callable|null $after Process each entity after applying raw data.
625
630
* @return ModelInterface[]|ArrayAccess
626
631
*/
627
- protected function processCollection ($ results , callable $ callback = null )
632
+ protected function processCollection ($ results , callable $ before = null , callable $ after = null )
628
633
{
629
- if ($ callback === null ) {
630
- $ callback = $ this ->callback ();
631
- }
632
-
633
634
$ modelObjType = $ this ->model ()->objType ();
634
635
$ collection = $ this ->createCollection ();
635
636
foreach ($ results as $ objData ) {
636
- if ($ this ->dynamicTypeField && isset ($ objData [$ this ->dynamicTypeField ])) {
637
- $ objType = $ objData [$ this ->dynamicTypeField ];
638
- } else {
639
- $ objType = $ modelObjType ;
640
- }
641
-
642
-
643
- $ obj = $ this ->factory ()->create ($ objType );
644
- $ obj ->setFlatData ($ objData );
645
-
646
- if (isset ($ callback )) {
647
- call_user_func_array ($ callback , [ &$ obj ]);
648
- }
637
+ $ obj = $ this ->processModel ($ objData , $ before , $ after );
649
638
650
639
if ($ obj instanceof ModelInterface) {
651
640
$ collection [] = $ obj ;
@@ -655,6 +644,37 @@ protected function processCollection($results, callable $callback = null)
655
644
return $ collection ;
656
645
}
657
646
647
+ /**
648
+ * Process the raw data for one model.
649
+ *
650
+ * @param mixed $objData The raw dataset.
651
+ * @param callable|null $before Process each entity before applying raw data.
652
+ * @param callable|null $after Process each entity after applying raw data.
653
+ * @return ModelInterface|ArrayAccess|null
654
+ */
655
+ protected function processModel ($ objData , callable $ before = null , callable $ after = null )
656
+ {
657
+ if ($ this ->dynamicTypeField && isset ($ objData [$ this ->dynamicTypeField ])) {
658
+ $ objType = $ objData [$ this ->dynamicTypeField ];
659
+ } else {
660
+ $ objType = $ this ->model ()->objType ();
661
+ }
662
+
663
+ $ obj = $ this ->factory ()->create ($ objType );
664
+
665
+ if ($ before !== null ) {
666
+ call_user_func_array ($ before , [ &$ obj ]);
667
+ }
668
+
669
+ $ obj ->setFlatData ($ objData );
670
+
671
+ if ($ after !== null ) {
672
+ call_user_func_array ($ after , [ &$ obj ]);
673
+ }
674
+
675
+ return $ obj ;
676
+ }
677
+
658
678
/**
659
679
* Create a collection class or array.
660
680
*
0 commit comments