1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nestecha\LaravelJsonApiValidation; |
4
|
|
|
|
5
|
|
|
use CloudCreativity\LaravelJsonApi\Factories\Factory; |
6
|
|
|
use CloudCreativity\LaravelJsonApi\LaravelJsonApi; |
7
|
|
|
use Illuminate\Validation\ValidationException; |
8
|
|
|
use Neomerx\JsonApi\Document\Error; |
9
|
|
|
use Nestecha\LaravelJsonApiValidation\Exception\JsonApiValidationException; |
10
|
|
|
|
11
|
|
|
class JsonApiValidator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $configName; |
17
|
|
|
|
18
|
|
|
public function __construct(string $configName = 'json-api-validation') |
19
|
|
|
{ |
20
|
|
|
$this->configName = $configName; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $data |
25
|
|
|
* @param array $rules |
26
|
|
|
* @param array $messages |
27
|
|
|
* @param array $customAttributes |
28
|
|
|
* @throws JsonApiValidationException |
29
|
|
|
*/ |
30
|
|
|
public function validateAsJsonApi(array $data, array $rules, array $messages = [], array $customAttributes = []) |
31
|
|
|
{ |
32
|
|
|
$originalValidationFailuresSetting = LaravelJsonApi::$validationFailures; |
33
|
|
|
LaravelJsonApi::showValidatorFailures(); |
34
|
|
|
|
35
|
|
|
/** @var Factory $factory */ |
36
|
|
|
$factory = app(Factory::class); |
37
|
|
|
$configName = $this->configName; |
38
|
|
|
|
39
|
|
|
$validator = $factory->createValidator( |
40
|
|
|
$data, |
41
|
|
|
$rules, |
42
|
|
|
$messages, |
43
|
|
|
$customAttributes, |
44
|
|
|
function ($field, $message, $failures) use ($configName, $data) |
45
|
|
|
{ |
46
|
|
|
$errorCode = null; |
47
|
|
View Code Duplication |
if (!empty(config("{$configName}.{$failures['rule']}.code"))) { |
|
|
|
|
48
|
|
|
$errorCode = config("{$configName}.{$failures['rule']}.code"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$fieldType = (new GetFieldType())->getType($field); |
52
|
|
View Code Duplication |
if (!empty(config("{$configName}.{$failures['rule']}.$fieldType.code"))) { |
|
|
|
|
53
|
|
|
$errorCode = config("{$configName}.{$failures['rule']}.$fieldType.code"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new Error( |
57
|
|
|
null, |
58
|
|
|
null, |
59
|
|
|
422, |
60
|
|
|
$errorCode, |
61
|
|
|
"Unprocessable Entity", |
62
|
|
|
$message, |
63
|
|
|
['pointer' => "/data/attributes/$field", 'value' => data_get($data, $field)], |
64
|
|
|
['failed' => $failures] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$validator->validate(); |
71
|
|
|
} catch (ValidationException $validationException) { |
72
|
|
|
throw new JsonApiValidationException($validator); |
73
|
|
|
} finally { |
74
|
|
|
LaravelJsonApi::$validationFailures = $originalValidationFailuresSetting; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.