-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepository.php
More file actions
45 lines (39 loc) · 1.12 KB
/
UserRepository.php
File metadata and controls
45 lines (39 loc) · 1.12 KB
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
45
<?php
namespace Infrastructure\Repository;
use Domain\User;
use WebFiori\Database\Repository\AbstractRepository;
/**
* Repository implementation using AbstractRepository
*/
class UserRepository extends AbstractRepository {
/** @return User[] */
public function findByAge(int $minAge): array {
$result = $this->getDatabase()->table($this->getTableName())
->select()
->where('age', $minAge, '>=')
->execute();
return array_map(fn($row) => $this->toEntity($row), $result->fetchAll());
}
protected function getIdField(): string {
return 'id';
}
protected function getTableName(): string {
return 'users';
}
protected function toArray(object $entity): array {
return [
'id' => $entity->id,
'name' => $entity->name,
'email' => $entity->email,
'age' => $entity->age
];
}
protected function toEntity(array $row): User {
return new User(
(int) $row['id'],
$row['name'],
$row['email'],
(int) $row['age']
);
}
}