-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepository.php
128 lines (109 loc) · 2.88 KB
/
Repository.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
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
<?php
declare(strict_types=1);
namespace Pandawa\Component\Eloquent;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Pandawa\Component\Eloquent\Criteria\Criteria;
use Pandawa\Component\Eloquent\Criteria\EagerLoad;
use Pandawa\Component\Eloquent\Criteria\Lock;
use Pandawa\Contracts\Eloquent\Cache\CacheableInterface;
use Pandawa\Contracts\Eloquent\CriterionInterface;
use Pandawa\Contracts\Eloquent\LockMode;
use Pandawa\Contracts\Eloquent\Persistent\PersistentInterface;
use Pandawa\Contracts\Eloquent\QueryBuilderInterface;
use Pandawa\Contracts\Eloquent\RepositoryInterface;
/**
* This class provides ability to load models and persistent.
*
* @template TModel
* @implements RepositoryInterface<TModel>
*
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class Repository implements RepositoryInterface
{
protected readonly Model $model;
public function __construct(
protected readonly QueryBuilderInterface $query,
protected readonly PersistentInterface $persistent,
) {
$this->model = $this->query->getModel();
if ($this instanceof CacheableInterface) {
$this->query->enableCache();
}
}
/**
* {@inheritDoc}
*/
public function withRelations(array $relations): static
{
$this->query->withCriteria(new EagerLoad($relations));
return $this;
}
/**
* {@inheritDoc}
*/
public function withCriteria(CriterionInterface|array $criteria): static
{
$this->query->withCriteria($criteria);
return $this;
}
/**
* {@inheritDoc}
*/
public function withLock(LockMode $lockMode): static
{
$this->query->withCriteria(new Lock($lockMode));
return $this;
}
/**
* {@inheritDoc}
*/
public function findById(int|string $id): ?Model
{
return $this->query->findByKey($id);
}
/**
* {@inheritDoc}
*/
public function findOneBy(array $criteria): ?Model
{
return $this->query->withCriteria(new Criteria($criteria))->first();
}
/**
* {@inheritDoc}
*/
public function findBy(array $criteria): Collection
{
return $this->query->withCriteria(new Criteria($criteria))->get();
}
/**
* {@inheritDoc}
*/
public function findAll(): Collection
{
return $this->query->get();
}
/**
* {@inheritDoc}
*/
public function paginate(int $pageSize): LengthAwarePaginator
{
return $this->query->paginate($pageSize);
}
/**
* {@inheritDoc}
*/
public function save(Model $model): Model
{
return $this->persistent->save($model);
}
/**
* {@inheritDoc}
*/
public function delete(Model $model): Model
{
return $this->persistent->delete($model);
}
}