1
|
|
|
<?php namespace Felixkiss\UniqueWithValidator; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
|
6
|
|
|
class Validator |
7
|
|
|
{ |
8
|
|
|
public function validateUniqueWith($attribute, $value, $parameters, $validator) |
9
|
|
|
{ |
10
|
|
|
$ruleParser = new RuleParser($attribute, $value, $parameters, $validator->getData()); |
11
|
|
|
|
12
|
|
|
// The presence verifier is responsible for counting rows within this |
13
|
|
|
// store mechanism which might be a relational database or any other |
14
|
|
|
// permanent data store like Redis, etc. We will use it to determine |
15
|
|
|
// uniqueness. |
16
|
|
|
$presenceVerifier = $validator->getPresenceVerifier(); |
17
|
|
|
|
18
|
|
|
return $presenceVerifier->getCount( |
19
|
|
|
$ruleParser->getTable(), |
20
|
|
|
$ruleParser->getPrimaryField(), |
21
|
|
|
$ruleParser->getPrimaryValue(), |
22
|
|
|
$ruleParser->getIgnoreValue(), |
23
|
|
|
$ruleParser->getIgnoreColumn(), |
24
|
|
|
$ruleParser->getAdditionalFields() |
25
|
|
|
) == 0; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function replaceUniqueWith($message, $attribute, $rule, $parameters, $translator) |
29
|
|
|
{ |
30
|
|
|
$ruleParser = new RuleParser($attribute, null, $parameters); |
31
|
|
|
$fields = $ruleParser->getDataFields(); |
32
|
|
|
|
33
|
|
|
$customAttributes = $translator->trans('validation.attributes'); |
34
|
|
|
|
35
|
|
|
// Check if translator has custom validation attributes for the fields |
36
|
|
|
$fields = array_map(function($field) use ($customAttributes) { |
37
|
|
|
return Arr::get($customAttributes, $field) ?: str_replace('_', ' ', Str::snake($field)); |
38
|
|
|
}, $fields); |
39
|
|
|
|
40
|
|
|
return str_replace(':fields', implode(', ', $fields), $message); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|