Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent translations to be discarded #324

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add command `enum:to-native` to convert a class that extends `BenSampo\Enum\Enum` to a native PHP enum

### Fixed

- Prevent translations to be discarded [324](https://github.com/BenSampo/laravel-enum/pull/324)

## 6.3.3

### Fixed
Expand Down
55 changes: 27 additions & 28 deletions src/EnumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,33 @@ protected function bootCommands(): void

protected function bootValidators(): void
{
$validationFactory = $this->app->make(ValidationFactory::class);
assert($validationFactory instanceof ValidationFactory);

$validationFactory->extend('enum_key', function ($attribute, $value, $parameters, $validator) {
$enum = $parameters[0] ?? null;

return (new EnumKey($enum))->passes($attribute, $value);
}, __('laravelEnum::messages.enum_key'));

$validationFactory->extend('enum_value', function ($attribute, $value, $parameters, $validator) {
$enum = $parameters[0] ?? null;

$strict = $parameters[1] ?? null;

if (! $strict) {
return (new EnumValue($enum))->passes($attribute, $value);
}

$strict = (bool) json_decode(strtolower($strict));

return (new EnumValue($enum, $strict))->passes($attribute, $value);
}, __('laravelEnum::messages.enum_value'));

$validationFactory->extend('enum', function ($attribute, $value, $parameters, $validator) {
$enum = $parameters[0] ?? null;

return (new Enum($enum))->passes($attribute, $value);
}, __('laravelEnum::messages.enum'));
$this->app->afterResolving(ValidationFactory::class, function (ValidationFactory $validationFactory) {
$validationFactory->extend('enum_key', function ($attribute, $value, $parameters, $validator) {
$enum = $parameters[0] ?? null;

return (new EnumKey($enum))->passes($attribute, $value);
}, __('laravelEnum::messages.enum_key'));

$validationFactory->extend('enum_value', function ($attribute, $value, $parameters, $validator) {
$enum = $parameters[0] ?? null;

$strict = $parameters[1] ?? null;

if (! $strict) {
return (new EnumValue($enum))->passes($attribute, $value);
}

$strict = (bool) json_decode(strtolower($strict));

return (new EnumValue($enum, $strict))->passes($attribute, $value);
}, __('laravelEnum::messages.enum_value'));

$validationFactory->extend('enum', function ($attribute, $value, $parameters, $validator) {
$enum = $parameters[0] ?? null;

return (new Enum($enum))->passes($attribute, $value);
}, __('laravelEnum::messages.enum'));
});
}

protected function bootDoctrineType(): void
Expand Down