1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Napp\Core\Dbal\Repository; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Napp\Core\Dbal\Criteria\CriteriaCollectionInterface; |
9
|
|
|
|
10
|
|
|
abstract class BaseRepository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Model |
14
|
|
|
*/ |
15
|
|
|
protected $query; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param int $id |
19
|
|
|
* @param array $relations |
20
|
|
|
* |
21
|
|
|
* @return Model|Collection|null |
22
|
|
|
*/ |
23
|
|
|
public function find(int $id, array $relations = []) |
24
|
|
|
{ |
25
|
|
|
return $this->query->newQuery()->with($relations)->find($id); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $ids |
30
|
|
|
* @param array $relations |
31
|
|
|
* @param array $columns |
32
|
|
|
* |
33
|
|
|
* @return Collection |
34
|
|
|
*/ |
35
|
|
|
public function findMany(array $ids, array $relations = [], $columns = ['*']) |
36
|
|
|
{ |
37
|
|
|
return $this->query->newQuery()->with($relations)->findMany($ids, $columns); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $attribute |
42
|
|
|
* @param mixed $value |
43
|
|
|
* |
44
|
|
|
* @return Model|null |
45
|
|
|
*/ |
46
|
|
|
public function findByAttribute(string $attribute, $value) |
47
|
|
|
{ |
48
|
|
|
return $this->query->newQuery()->where($attribute, $value)->first(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $attributes |
53
|
|
|
* |
54
|
|
|
* @return Model|null |
55
|
|
|
*/ |
56
|
|
|
public function findByAttributes(array $attributes) |
57
|
|
|
{ |
58
|
|
|
return $this->query->newQuery()->where($attributes)->first(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param CriteriaCollectionInterface $criteriaCollection |
63
|
|
|
* |
64
|
|
|
* @return Model|null |
65
|
|
|
*/ |
66
|
|
|
public function findMatchingCriteria(CriteriaCollectionInterface $criteriaCollection) |
67
|
|
|
{ |
68
|
|
|
$query = $this->getCriteriaQuery($criteriaCollection); |
69
|
|
|
|
70
|
|
|
return $query->first(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $relations |
75
|
|
|
* |
76
|
|
|
* @return Collection |
77
|
|
|
*/ |
78
|
|
|
public function getAll(array $relations = []): Collection |
79
|
|
|
{ |
80
|
|
|
return $this->query->newQuery()->with($relations)->get(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $attribute |
85
|
|
|
* @param array|string $value |
86
|
|
|
* @param array $relations |
87
|
|
|
* |
88
|
|
|
* @return Collection |
89
|
|
|
*/ |
90
|
|
|
public function getAllByAttribute(string $attribute, $value, array $relations = []): Collection |
91
|
|
|
{ |
92
|
|
|
$query = $this->query->newQuery()->with($relations); |
93
|
|
|
|
94
|
|
|
if (true === is_array($value)) { |
95
|
|
|
return $query->whereIn($attribute, $value)->get(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $query->where($attribute, $value)->get(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $attributes |
103
|
|
|
* @param array $relations |
104
|
|
|
* |
105
|
|
|
* @return Collection |
106
|
|
|
*/ |
107
|
|
|
public function getAllByAttributes(array $attributes, array $relations = []): Collection |
108
|
|
|
{ |
109
|
|
|
return $this->query->newQuery()->with($relations)->where($attributes)->get(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param CriteriaCollectionInterface $criteriaCollection |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
116
|
|
|
*/ |
117
|
|
|
protected function getCriteriaQuery(CriteriaCollectionInterface $criteriaCollection): Builder |
118
|
|
|
{ |
119
|
|
|
$query = $this->query->newQuery(); |
120
|
|
|
$query->getQuery()->select($this->query->getTable().'.*'); |
121
|
|
|
|
122
|
|
|
foreach ($criteriaCollection->getAll() as $criterion) { |
123
|
|
|
$criterion->apply($query); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $query; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param CriteriaCollectionInterface $criteriaCollection |
131
|
|
|
* |
132
|
|
|
* @return Collection |
133
|
|
|
*/ |
134
|
|
|
public function getAllMatchingCriteria(CriteriaCollectionInterface $criteriaCollection): Collection |
135
|
|
|
{ |
136
|
|
|
$query = $this->getCriteriaQuery($criteriaCollection); |
137
|
|
|
|
138
|
|
|
return $query->get(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param callable $callback |
143
|
|
|
* @param int $attempts |
144
|
|
|
* |
145
|
|
|
* @throws \Throwable |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public function transaction(callable $callback, $attempts = 1) |
150
|
|
|
{ |
151
|
|
|
return $this->query->newQuery()->getConnection()->transaction($callback, $attempts); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|