|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Verifiers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Validation\DatabasePresenceVerifier; |
|
9
|
|
|
use Illuminate\Database\ConnectionResolverInterface; |
|
10
|
|
|
|
|
11
|
|
|
class EloquentPresenceVerifier extends DatabasePresenceVerifier |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The eloquent model instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $model; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new eloquent presence verifier instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @param \Illuminate\Database\ConnectionResolverInterface $db |
|
24
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(ConnectionResolverInterface $db, Model $model) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($db); |
|
31
|
|
|
|
|
32
|
|
|
$this->model = $model; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Count the number of objects in a collection having the given value. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $collection |
|
39
|
|
|
* @param string $column |
|
40
|
|
|
* @param string $value |
|
41
|
|
|
* @param int|null $excludeId |
|
42
|
|
|
* @param string|null $idColumn |
|
43
|
|
|
* @param array $extra |
|
44
|
|
|
* |
|
45
|
|
|
* @return int |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$query = $this->model($collection)->where($column, '=', $value); |
|
50
|
|
|
|
|
51
|
|
|
if (! is_null($excludeId) && $excludeId !== 'NULL') { |
|
|
|
|
|
|
52
|
|
|
$query->where($idColumn ?: 'id', '<>', $excludeId); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->addConditions($query, $extra)->count(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Count the number of objects in a collection with the given values. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $collection |
|
62
|
|
|
* @param string $column |
|
63
|
|
|
* @param array $values |
|
64
|
|
|
* @param array $extra |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getMultiCount($collection, $column, array $values, array $extra = []) |
|
69
|
|
|
{ |
|
70
|
|
|
$query = $this->model($collection)->whereIn($column, $values); |
|
71
|
|
|
|
|
72
|
|
|
return $this->addConditions($query, $extra)->count(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $collection |
|
77
|
|
|
* |
|
78
|
|
|
* @return Model |
|
79
|
|
|
*/ |
|
80
|
|
|
private function model($collection) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->model->setConnection($this->connection)->setTable($collection)->useWritePdo(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.