| 1 | <?php |
||
| 5 | trait CollectionBuilderTrait |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Build a collection of entities from an indexed array of data. |
||
| 9 | * |
||
| 10 | * @param array $data |
||
| 11 | * @param string|null $entityType |
||
| 12 | * |
||
| 13 | * @return \Percy\Entity\Collection |
||
| 14 | */ |
||
| 15 | 4 | public function buildCollection(array $data, $entityType = null) |
|
| 16 | { |
||
| 17 | 4 | $collection = new Collection; |
|
| 18 | |||
| 19 | 4 | foreach ($data as $row) { |
|
| 20 | 3 | $entity = (is_null($entityType)) ? $this->getEntityType() : $entityType; |
|
| 21 | 3 | $entity = new $entity; |
|
| 22 | |||
| 23 | 3 | $collection->addEntity( |
|
| 24 | 3 | $entity->hydrate($row) |
|
| 25 | 3 | ); |
|
| 26 | 4 | } |
|
| 27 | |||
| 28 | 4 | return $collection; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get the primary entity type associated with the repository. |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | abstract public function getEntityType(); |
||
| 37 | } |
||
| 38 |