-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
59 lines (44 loc) · 1.93 KB
/
Validator.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
<?php
declare(strict_types=1);
namespace Pandawa\Component\Validation;
use Illuminate\Validation\Rules\Unique;
use Illuminate\Validation\Validator as LaravelValidator;
use Pandawa\Contracts\Validation\Parser\ParserResolverInterface;
/**
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class Validator extends LaravelValidator
{
public function validateUnique($attribute, $value, $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'unique');
[$connection, $table, $idColumn] = $this->parseTable($parameters[0]);
// The second parameter position holds the name of the column that needs to
// be verified as unique. If this parameter isn't specified we will just
// assume that this column to be verified shares the attribute's name.
$column = $this->getQueryColumn($parameters, $attribute);
$id = null;
if (isset($parameters[2])) {
[$idColumn, $id] = $this->getUniqueIds($idColumn, $parameters);
if (! is_null($id)) {
$id = stripslashes($id);
}
}
// The presence verifier is responsible for counting rows within this store
// mechanism which might be a relational database or any other permanent
// data store like Redis, etc. We will use it to determine uniqueness.
$verifier = $this->getPresenceVerifier($connection);
$extra = $this->getUniqueExtra($parameters);
if ($this->currentRule instanceof Unique) {
$extra = array_merge($extra, $this->currentRule->queryCallbacks());
}
if (null !== $id) {
$id = $this->parserResolver()->resolve($id)?->parse($id) ?? $id;
}
return 0 === $verifier->getCount($table, $column, $value, $id, $idColumn, $extra);
}
protected function parserResolver(): ParserResolverInterface
{
return $this->container->get(ParserResolverInterface::class);
}
}