77 lines
No EOL
2.2 KiB
PHP
77 lines
No EOL
2.2 KiB
PHP
<?php
|
|
namespace App\Http\Requests\Back\Users\Concerns;
|
|
|
|
use Uwil\LaravelBackend\Support\UserRoleRegistry;
|
|
|
|
/**
|
|
* Shared validation rules for user requests.
|
|
*/
|
|
trait HasUserRequestRules
|
|
{
|
|
/**
|
|
* Resolve role flags from the current request input.
|
|
*
|
|
* @return array{isClient:boolean,isProjectManager:boolean,isDevelopper:boolean}
|
|
*/
|
|
private function resolveRoleFlags(): array
|
|
{
|
|
$role = $this->input('role');
|
|
return [
|
|
'isClient' => $role === UserRoleRegistry::get('client')->value,
|
|
'isProjectManager' => $role === UserRoleRegistry::get('project-manager')->value,
|
|
'isDevelopper' => $role === UserRoleRegistry::get('developper')->value,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function extraPrepareForValidation(): void
|
|
{
|
|
[
|
|
'isClient' => $isClient,
|
|
'isProjectManager' => $isProjectManager,
|
|
'isDevelopper' => $isDevelopper,
|
|
] = $this->resolveRoleFlags();
|
|
|
|
$this->merge([
|
|
'gitlab_id' => $isProjectManager || $isDevelopper ? $this->input('gitlab_id') : null,
|
|
'sites' => $isClient ? $this->input('sites') : null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the extra validation rules.
|
|
*
|
|
* @return array<string,mixed>
|
|
*/
|
|
protected function extraRules(): array
|
|
{
|
|
[
|
|
'isClient' => $isClient,
|
|
'isProjectManager' => $isProjectManager,
|
|
'isDevelopper' => $isDevelopper,
|
|
] = $this->resolveRoleFlags();
|
|
|
|
return [
|
|
'gitlab_id' => $isProjectManager || $isDevelopper ? 'required|integer|min:1' : 'nullable|integer|min:1',
|
|
'sites' => $isClient ? 'required|array' : 'nullable|array',
|
|
'sites.*' => 'integer|exists:sites,id|distinct',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the extra attributes.
|
|
*
|
|
* @return array<string,string>
|
|
*/
|
|
protected function extraAttributes(): array
|
|
{
|
|
return [
|
|
'gitlab_id' => trans('validation.custom.gitlab_id'),
|
|
'sites' => trans_choice('models.classes.sites', \INF),
|
|
];
|
|
}
|
|
} |