Skip to content

Commit

Permalink
Add type validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
akalongman committed Jan 31, 2024
1 parent c7aae62 commit f53c96b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Add `LodashServiceProvider` to your service providers list in the `app.php`
/*
* Package Service Providers...
*/
Longman\LaravelLodash\LodashServiceProvider::class,
Longman\LaravelLodash\ServiceProvider::class,
. . .
],
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@
namespace Longman\LaravelLodash;

use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Longman\LaravelLodash\Commands\ClearAll;
use Longman\LaravelLodash\Commands\DbClear;
use Longman\LaravelLodash\Commands\DbDump;
use Longman\LaravelLodash\Commands\DbRestore;
use Longman\LaravelLodash\Commands\LogClear;
use Longman\LaravelLodash\Commands\UserAdd;
use Longman\LaravelLodash\Commands\UserPassword;
use Longman\LaravelLodash\Validation\StrictTypesValidator;

use function array_keys;
use function array_pad;
use function config;
use function preg_split;
use function str_replace;
use function trim;

class LodashServiceProvider extends ServiceProvider
class ServiceProvider extends LaravelServiceProvider
{
protected array $commands = [
'command.lodash.clear-all' => ClearAll::class,
Expand All @@ -40,6 +44,8 @@ public function boot(): void
$this->registerBladeDirectives();

$this->loadTranslations();

$this->loadValidations();
}

public function register(): void
Expand All @@ -51,6 +57,10 @@ public function register(): void

protected function registerCommands(): void
{
if (! config('lodash.register.commands')) {
return;
}

foreach ($this->commands as $name => $class) {
$this->app->singleton($name, $class);
}
Expand All @@ -60,6 +70,10 @@ protected function registerCommands(): void

protected function registerBladeDirectives(): void
{
if (! config('lodash.register.blade_directives')) {
return;
}

// Display relative time
app('blade.compiler')->directive('datetime', static function ($expression) {
return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
Expand All @@ -78,6 +92,10 @@ protected function registerBladeDirectives(): void

protected function registerRequestMacros(): void
{
if (! config('lodash.register.request_macros')) {
return;
}

Request::macro('getInt', function (string $name, int $default = 0): int {
return (int) $this->get($name, $default);
});
Expand All @@ -97,10 +115,32 @@ protected function registerRequestMacros(): void

protected function loadTranslations(): void
{
if (! config('lodash.register.translations')) {
return;
}

$this->loadTranslationsFrom(__DIR__ . '/../translations', 'lodash');

$this->publishes([
__DIR__ . '/../translations' => resource_path('lang/vendor/lodash'),
]);
}

protected function loadValidations(): void
{
if (! config('lodash.register.validation_rules')) {
return;
}

Validator::extend('type', function ($attribute, $value, $parameters, $validator): bool {
$validator->addReplacer('type', function ($message, $attribute, $rule, $parameters): string {
return str_replace(':type', $parameters[0], $message);
});

/** @var \Longman\LaravelLodash\Validation\StrictTypesValidator $validator */
$customValidator = $this->app->make(StrictTypesValidator::class);

return $customValidator->validate($attribute, $value, $parameters);
}, 'The :attribute must be of type :type');
}
}
46 changes: 46 additions & 0 deletions src/Lodash/Validation/StrictTypesValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Longman\LaravelLodash\Validation;

readonly class StrictTypesValidator
{
protected const NATIVE_TYPE_INT = 'integer';
protected const NATIVE_TYPE_FLOAT = 'double';
protected const NATIVE_TYPE_BOOL = 'boolean';

protected const TYPE_MAP = [
'int' => self::NATIVE_TYPE_INT,
'integer' => self::NATIVE_TYPE_INT,
'float' => self::NATIVE_TYPE_FLOAT,
'double' => self::NATIVE_TYPE_FLOAT,
'bool' => self::NATIVE_TYPE_BOOL,
'boolean' => self::NATIVE_TYPE_BOOL,
];

public function validate($attribute, $value, $parameters): bool
{
if (empty($parameters)) {
return false;
}

$valueType = gettype($value);
$requiredType = $parameters[0];

if (empty(static::TYPE_MAP[$requiredType]) || $this->isNativeTypeString($requiredType)) {
return $valueType === $requiredType;
}

return $valueType === static::TYPE_MAP[$requiredType];
}

protected function isNativeTypeString(string $type): bool
{
return in_array($type, [
static::NATIVE_TYPE_INT,
static::NATIVE_TYPE_FLOAT,
static::NATIVE_TYPE_BOOL,
]);
}
}
8 changes: 8 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
'x_content_type_options' => 'nosniff', // X-Content-Type-Options header value
'x_xss_protection' => '1; mode=block', // X-XSS-Protection header value
],

'register' => [
'blade_directives' => false,
'request_macros' => false,
'translations' => true,
'validation_rules' => true,
'commands' => true,
],
];
4 changes: 2 additions & 2 deletions tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Longman\LaravelLodash\Cache\CacheServiceProvider;
use Longman\LaravelLodash\Debug\DebugServiceProvider;
use Longman\LaravelLodash\Elasticsearch\ElasticsearchServiceProvider;
use Longman\LaravelLodash\LodashServiceProvider;
use Longman\LaravelLodash\ServiceProvider;
use Longman\LaravelLodash\Queue\QueueServiceProvider;
use Longman\LaravelLodash\Redis\RedisServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
Expand All @@ -17,7 +17,7 @@ class TestCase extends BaseTestCase
protected function getPackageProviders($app)
{
return [
LodashServiceProvider::class,
ServiceProvider::class,
CacheServiceProvider::class,
DebugServiceProvider::class,
ElasticsearchServiceProvider::class,
Expand Down

0 comments on commit f53c96b

Please sign in to comment.