Skip to content

Commit 1aef0b1

Browse files
authored
Merge pull request #2 from axian11/patch-1
Adding the ability to specify a custom function to map objects.
2 parents 0a30141 + d9b2d9d commit 1aef0b1

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/Mapper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,20 @@ public function getQueryBuilder()
4444
* Map a data to a collection of model objects.
4545
*
4646
* @param array $data An array of data.
47+
* @param callable $callable The callable you want to call for each row of data.
4748
* @return Collection A collection of objects.
4849
*/
49-
protected function mapToObjects($data)
50+
protected function mapToObjects(array $data, callable $callable = null)
5051
{
5152
$this->collection->clear();
5253

5354
if (count($data) > 0) {
5455
foreach ($data as $row) {
55-
$this->collection->add($this->instantiateObject($row));
56+
if (is_callable($callable)) {
57+
$this->collection->add(call_user_func($callable, $row));
58+
} else {
59+
$this->collection->add($this->instantiateObject($row));
60+
}
5661
}
5762
}
5863

0 commit comments

Comments
 (0)