-
-
Notifications
You must be signed in to change notification settings - Fork 480
/
Repository.php
89 lines (81 loc) · 2.41 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
<?php
namespace App\Ship\Parents\Repositories;
use Apiato\Core\Abstracts\Repositories\Repository as AbstractRepository;
use App\Ship\Exceptions\DeleteResourceFailedException;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Exceptions\RepositoryException;
/**
* @template TModel of Model
*/
abstract class Repository extends AbstractRepository
{
/**
* Find a model by its primary key.
*
* @return TModel|null
*/
public function findById(int|string $id, array $columns = ['*'])
{
try {
return $this->find($id, $columns);
} catch (ModelNotFoundException) {
return null;
}
}
/**
* Get a model by its primary key or throw an exception.
*
* @return TModel
*
* @throws ModelNotFoundException
*/
public function getById(int|string $id, array $columns = ['*'])
{
return $this->find($id, $columns);
}
/**
* @return Collection<array-key, TModel>
*/
public function findMany(array|Arrayable $ids, array $columns = ['*']): Collection
{
try {
return $this->find($ids, $columns);
} catch (ModelNotFoundException) {
return new Collection();
}
}
/**
* Delete a model by its primary key or throw an exception.
*
* @param int|string $id
*
* @throws ModelNotFoundException
* @throws DeleteResourceFailedException
*/
public function delete($id): bool
{
try {
return (bool) parent::delete($id);
} catch (\Exception) {
throw new DeleteResourceFailedException();
}
}
/**
* @param class-string<CriteriaInterface> $criteria Criteria class name
* @param array<string, mixed> $args Arguments to pass to the criteria constructor
*
* @throws RepositoryException
* @throws BindingResolutionException
*/
public function pushCriteriaWith(string $criteria, array $args): static
{
/** @var CriteriaInterface $criteriaInstance */
$criteriaInstance = $this->app->makeWith($criteria, $args);
return $this->pushCriteria($criteriaInstance);
}
}