-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveRepository.php
More file actions
157 lines (145 loc) · 4.08 KB
/
Copy pathActiveRepository.php
File metadata and controls
157 lines (145 loc) · 4.08 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace Cycle\ActiveRecord\Repository;
use Cycle\ActiveRecord\Facade;
use Cycle\ActiveRecord\Query\ActiveQuery;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\RepositoryInterface;
use Cycle\ORM\Select;
/**
* ActiveRepository combines the advantages of the {@see RepositoryInterface} and the {@see ActiveQuery}:
*
* - Immutable by default
* - Can be associated with an entity, or not
* - No restriction "an entity can have only one repository"
* - Not a QueryBuilder entity, so it can follow a contract with a limited set of methods.
* - Organically used in the DI container
*
* @see self::forUpdate() as an example of immutabile method.
*
* @template-covariant TEntity of object
*/
class ActiveRepository
{
/** @var Select<TEntity> */
private Select $select;
/**
* Redefine the constructor in the child class to set the default entity class:
*
* ```php
* public function __construct()
* {
* parent::__construct(User::class);
* }
* ```
*
* @param class-string<TEntity> $role
*/
public function __construct(string $role)
{
$orm = Facade::getOrm();
$this->select = $this->initSelect($orm, $role);
}
/**
* Find entity by primary key.
*
* @note Limit of 1 will be added to the query.
*
* @param string|int|non-empty-list<string|int>|non-empty-array<non-empty-string, string|int>|object $id
*
* @return TEntity|null
*/
public function findByPK(mixed $id): ?object
{
return $this->select()->wherePK($id)->fetchOne();
}
/**
* Find a single record based on the given scope.
*
* @note Limit of 1 will be added to the query.
*
* @return TEntity|null
*/
public function findOne(array $scope = [], array $orderBy = []): ?object
{
return $this->select()->orderBy($orderBy)->fetchOne($scope);
}
/**
* @return iterable<TEntity>
*/
public function findAll(array $scope = [], array $orderBy = []): iterable
{
return $this->select()->where($scope)->orderBy($orderBy)->fetchAll();
}
/**
* Return a new repository instance with the "FOR UPDATE" mode enabled.
*
* This method does not modify the current repository instance, but returns a new one.
* It is useful for performing row-level locking in a transaction.
*
* @return $this
* @mutation-free
*/
public function forUpdate(): static
{
return $this->with(
// Change Select or ActiveQuery like this
$this->select()->forUpdate(),
);
}
/**
* Get a copy of the current selector associated with the repository.
*
* @note The method is final to prevent the selector from being modified.
* If you need to modify the default selector, consider using a constructor or {@see self::initSelect()}.
*
* @return Select<TEntity>
* @mutation-free
*/
final public function select(): Select
{
return clone $this->select;
}
/**
* @param Select<TEntity> $select
*
* @return $this
*/
final protected function with(Select $select): static
{
$repository = clone $this;
$repository->select = $select;
return $repository;
}
/**
* Create a default selector for the repository. Used in the constructor.
*
* How to modify the default selector:
*
* ```php
* public function initSelect(ORMInterface $orm, string $role)
* {
* parent::initSelect($orm, $role)->where('deleted_at', '=', null);
* }
* ```
*
* How to use {@see ActiveQuery} instead of {@see Select}:
*
* ```php
* protected function initSelect(ORMInterface $orm, string $role): Select
* {
* return new CustomActiveQuery($role);
* }
* ```
*
* @template T
*
* @param class-string<T> $role
*
* @return Select<T>
*/
protected function initSelect(ORMInterface $orm, string $role): Select
{
return new Select($orm, $role);
}
}