-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeopleRepository.php
44 lines (33 loc) · 1.23 KB
/
PeopleRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace Tests\Example;
use Tests\Example\Entities\PeopleEntity;
use Tests\Example\Entities\PeopleEntityList;
use DevMakerLab\LaravelFilters\AbstractFilterableRepository;
class PeopleRepository extends AbstractFilterableRepository
{
private \Illuminate\Database\DatabaseManager $databaseManager;
public function __construct(\Illuminate\Database\DatabaseManager $databaseManager)
{
$this->databaseManager = $databaseManager;
}
public function get(array $args): PeopleEntityList
{
$queryBuilder = $this->databaseManager->table('people')
->select(['firstname', 'lastname', 'age', 'gender']);
$this->applyFilters($queryBuilder, $args);
$people = $queryBuilder->get();
return $this->transform($people);
}
public function transform(\Illuminate\Support\Collection $people): PeopleEntityList
{
$people->transform(function ($person) {
return new PeopleEntity([
'name' => sprintf('%s %s', $person->lastname, $person->firstname),
'age' => $person->age,
'gender' => $person->gender,
]);
});
return new PeopleEntityList($people->toArray());
}
}