Skip to content

Commit e07d4dc

Browse files
committed
Add strict type validator
1 parent 7641649 commit e07d4dc

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

src/Lodash/ServiceProvider.php

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Longman\LaravelLodash;
66

7+
use Illuminate\Contracts\Translation\Translator;
78
use Illuminate\Http\Request;
8-
use Illuminate\Support\Facades\Validator;
99
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
1010
use InvalidArgumentException;
1111
use Longman\LaravelLodash\Commands\ClearAll;
@@ -16,6 +16,7 @@
1616
use Longman\LaravelLodash\Commands\UserAdd;
1717
use Longman\LaravelLodash\Commands\UserPassword;
1818
use Longman\LaravelLodash\Validation\StrictTypeValidator;
19+
use Longman\LaravelLodash\Validation\Validator;
1920

2021
use function app;
2122
use function array_keys;
@@ -45,11 +46,39 @@ public function boot(): void
4546
__DIR__ . '/../config/config.php' => config_path('lodash.php'),
4647
]);
4748

49+
// Validator and rules
50+
$this->app['validator']
51+
->resolver(
52+
function (Translator $translator, array $data, array $rules, array $messages): Validator {
53+
$validator = new Validator(
54+
$translator,
55+
$data,
56+
$rules,
57+
$messages,
58+
);
59+
60+
$validator->addExtension('strict', function (string $attribute, mixed $value, array $parameters, Validator $validator): bool {
61+
if (empty($parameters[0])) {
62+
throw new InvalidArgumentException('Strict rule requires an type argument');
63+
}
64+
65+
$validator->addReplacer('strict', static function (string $message, string $attribute, string $rule, array $parameters): string {
66+
return str_replace(':type', $parameters[0], $message);
67+
});
68+
69+
$customValidator = $this->app->make(StrictTypeValidator::class);
70+
71+
return $customValidator->validate($attribute, $value, $parameters);
72+
});
73+
74+
return $validator;
75+
},
76+
);
77+
4878
$this->registerBladeDirectives();
4979

5080
$this->loadTranslations();
51-
52-
$this->loadValidations();
81+
//$this->loadValidations();
5382
}
5483

5584
public function register(): void
@@ -131,25 +160,4 @@ protected function loadTranslations(): void
131160
__DIR__ . '/../translations' => resource_path('lang/vendor/lodash'),
132161
]);
133162
}
134-
135-
protected function loadValidations(): void
136-
{
137-
if (! config('lodash.register.validation_rules')) {
138-
return;
139-
}
140-
141-
Validator::extend('strict', function (string $attribute, mixed $value, array $parameters, Validator $validator): bool {
142-
if (empty($parameters[0])) {
143-
throw new InvalidArgumentException('Strict rule requires an type argument');
144-
}
145-
146-
$validator->addReplacer('strict', static function (string $message, string $attribute, string $rule, array $parameters): string {
147-
return str_replace(':type', $parameters[0], $message);
148-
});
149-
150-
$customValidator = $this->app->make(StrictTypeValidator::class);
151-
152-
return $customValidator->validate($attribute, $value, $parameters);
153-
}, 'The :attribute must be of type :type');
154-
}
155163
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Longman\LaravelLodash\Validation;
6+
7+
use Illuminate\Validation\Validator as BaseValidator;
8+
use Override;
9+
10+
use function is_null;
11+
use function is_string;
12+
use function trim;
13+
14+
class Validator extends BaseValidator
15+
{
16+
#[Override]
17+
protected function presentOrRuleIsImplicit($rule, $attribute, $value): bool
18+
{
19+
if ((is_null($value) || (is_string($value) && trim($value) === '')) && ! $this->hasRule($attribute, ['Nullable', 'Present', 'Sometimes'])) {
20+
return $this->isImplicit($rule);
21+
}
22+
23+
return $this->validatePresent($attribute, $value)
24+
|| $this->isImplicit($rule);
25+
}
26+
}

0 commit comments

Comments
 (0)