Skip to content

Commit 4762d21

Browse files
committed
Added "before" callback to CollectionLoader
Continues 4fbeec8 Changes: - Moved model building from 'CollectionLoader::processCollection()' to 'CollectionLoader::processModel()' - Added new callback parameter to manipulate the model before the raw data is applied
1 parent 4ba303a commit 4762d21

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

src/Charcoal/Loader/CollectionLoader.php

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -518,20 +518,21 @@ public function callback()
518518
/**
519519
* Load a collection from source.
520520
*
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.
523523
* Leave blank to use {@see CollectionLoader::callback()}.
524+
* @param callable|null $before Process each entity before applying raw data.
524525
* @throws Exception If the database connection fails.
525526
* @return ModelInterface[]|ArrayAccess
526527
*/
527-
public function load($ident = null, callable $callback = null)
528+
public function load($ident = null, callable $callback = null, callable $before = null)
528529
{
529530
// Unused.
530531
unset($ident);
531532

532533
$query = $this->source()->sqlLoad();
533534

534-
return $this->loadFromQuery($query, $callback);
535+
return $this->loadFromQuery($query, $callback, $before);
535536
}
536537

537538
/**
@@ -575,15 +576,16 @@ public function loadCount()
575576
* ]);
576577
* ```
577578
*
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,
579580
* 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.
581582
* Leave blank to use {@see CollectionLoader::callback()}.
583+
* @param callable|null $before Process each entity before applying raw data.
582584
* @throws RuntimeException If the database connection fails.
583585
* @throws InvalidArgumentException If the SQL string/set is invalid.
584586
* @return ModelInterface[]|ArrayAccess
585587
*/
586-
public function loadFromQuery($query, callable $callback = null)
588+
public function loadFromQuery($query, callable $callback = null, callable $before = null)
587589
{
588590
$db = $this->source()->db();
589591

@@ -612,40 +614,27 @@ public function loadFromQuery($query, callable $callback = null)
612614

613615
$sth->setFetchMode(PDO::FETCH_ASSOC);
614616

615-
return $this->processCollection($sth, $callback);
617+
if ($callback === null) {
618+
$callback = $this->callback();
619+
}
620+
621+
return $this->processCollection($sth, $before, $callback);
616622
}
617623

618624
/**
619625
* Process the collection of raw data.
620626
*
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.
625630
* @return ModelInterface[]|ArrayAccess
626631
*/
627-
protected function processCollection($results, callable $callback = null)
632+
protected function processCollection($results, callable $before = null, callable $after = null)
628633
{
629-
if ($callback === null) {
630-
$callback = $this->callback();
631-
}
632-
633634
$modelObjType = $this->model()->objType();
634635
$collection = $this->createCollection();
635636
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);
649638

650639
if ($obj instanceof ModelInterface) {
651640
$collection[] = $obj;
@@ -655,6 +644,37 @@ protected function processCollection($results, callable $callback = null)
655644
return $collection;
656645
}
657646

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+
658678
/**
659679
* Create a collection class or array.
660680
*

0 commit comments

Comments
 (0)