2025-09-16 16:44:49 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Back;
|
|
|
|
|
|
|
2026-02-19 14:06:28 +01:00
|
|
|
|
use App\Enums\Contact\ContactFormType;
|
2025-12-15 15:32:53 +01:00
|
|
|
|
use App\Enums\ElementPositionEnum;
|
2025-09-16 16:44:49 +02:00
|
|
|
|
use App\Enums\DynamicFieldTypes;
|
2026-02-12 16:14:11 +01:00
|
|
|
|
use App\Enums\StaticRoutesEnum;
|
2025-09-16 16:44:49 +02:00
|
|
|
|
use App\Models\PageConfig;
|
2026-03-05 14:30:43 +01:00
|
|
|
|
use App\Rules\Video;
|
2025-09-26 15:08:37 +02:00
|
|
|
|
use App\Traits\Requests\HasInertiaPicture;
|
2025-09-16 16:44:49 +02:00
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2025-12-15 15:32:53 +01:00
|
|
|
|
use Illuminate\Validation\Rules\Enum;
|
2025-09-16 16:44:49 +02:00
|
|
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
2025-12-15 15:46:00 +01:00
|
|
|
|
use Uwil\LaravelCommon\Rules\WysiwygLength;
|
2025-09-16 16:44:49 +02:00
|
|
|
|
|
|
|
|
|
|
abstract class UpdateDynamicFieldRequest extends FormRequest
|
|
|
|
|
|
{
|
2025-09-26 15:08:37 +02:00
|
|
|
|
use HasInertiaPicture;
|
|
|
|
|
|
|
2025-09-16 16:44:49 +02:00
|
|
|
|
/** @var \App\Models\PageConfig|null $configModel */
|
|
|
|
|
|
private $configModel = null;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var string $fieldName */
|
|
|
|
|
|
private $fieldName = "content";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return boolean
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function authorize(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return auth('backend')->check();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return void
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->configModel = PageConfig::query()->whereKey($this->input('config_id'))->firstOrFail();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function rules(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
$rules = [];
|
|
|
|
|
|
foreach ($this->configModel->config as $input) {
|
2025-11-07 19:00:09 +01:00
|
|
|
|
$rules = array_merge($rules, $this->recursiveRules($input, $this->fieldName));
|
|
|
|
|
|
} //end foreach
|
|
|
|
|
|
return $rules;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-24 16:01:36 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Get rules from a specific input type.
|
2025-12-02 15:33:30 +01:00
|
|
|
|
*
|
2026-02-10 15:43:47 +01:00
|
|
|
|
* @param array $input
|
|
|
|
|
|
* @param string $prefix
|
2025-11-24 16:01:36 +01:00
|
|
|
|
* @return array
|
2026-02-10 15:43:47 +01:00
|
|
|
|
* @throws \UnexpectedValueException Unrecognized dynamic type.
|
|
|
|
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity
|
2025-11-24 16:01:36 +01:00
|
|
|
|
*/
|
2026-02-10 15:43:47 +01:00
|
|
|
|
private function recursiveRules(array &$input, string $prefix): array
|
2025-11-07 19:00:09 +01:00
|
|
|
|
{
|
2026-02-10 15:43:47 +01:00
|
|
|
|
// phpcs:enable
|
2025-11-07 19:00:09 +01:00
|
|
|
|
$rules = [];
|
|
|
|
|
|
$ruleKey = "$prefix.$input[key]";
|
|
|
|
|
|
switch ($input['type']) {
|
|
|
|
|
|
case DynamicFieldTypes::string->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::string->getValidation(), $input)
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-11-24 16:01:36 +01:00
|
|
|
|
case DynamicFieldTypes::textarea->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::textarea->getValidation(), $input)
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-11-18 13:52:59 +01:00
|
|
|
|
case DynamicFieldTypes::wysiwyg->value:
|
2025-12-18 09:59:25 +01:00
|
|
|
|
$defaultRules = explode('|', DynamicFieldTypes::wysiwyg->getValidation());
|
|
|
|
|
|
$wysiwygRules = $this->compileValidate(DynamicFieldTypes::wysiwyg->getValidation(), $input);
|
|
|
|
|
|
|
2025-12-19 10:31:11 +01:00
|
|
|
|
// Get the minimum and maximum values of the ‘between’ rule and
|
|
|
|
|
|
// remove them from the rules so that our custom rule WysiwygLength can apply it.
|
2025-12-18 09:59:25 +01:00
|
|
|
|
$defaults = $this->extractMinMax($defaultRules);
|
|
|
|
|
|
['min' => $min, 'max' => $max] = $this->extractMinMax($wysiwygRules, $defaults);
|
2026-02-10 15:43:47 +01:00
|
|
|
|
$newWysiwygRules = array_values(array_filter($wysiwygRules, fn($rule) => !(
|
2025-12-19 10:31:11 +01:00
|
|
|
|
is_string($rule) && preg_match('/^(min|max|between):/', $rule)
|
|
|
|
|
|
)));
|
2026-02-06 16:25:00 +01:00
|
|
|
|
|
2025-12-15 15:46:00 +01:00
|
|
|
|
$rules = array_merge($rules, [
|
2025-12-19 10:31:11 +01:00
|
|
|
|
$ruleKey => array_merge($newWysiwygRules, [
|
2025-12-18 09:59:25 +01:00
|
|
|
|
new WysiwygLength($min, $max),
|
2025-12-15 15:46:00 +01:00
|
|
|
|
])
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::number->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::number->getValidation(), $input)
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::boolean->value:
|
2025-11-18 11:00:56 +01:00
|
|
|
|
$all = $this->all();
|
|
|
|
|
|
$modified = $this->castBooleanFields($all);
|
|
|
|
|
|
$this->merge($modified);
|
2025-11-07 19:00:09 +01:00
|
|
|
|
$rules = array_merge($rules, [
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::boolean->getValidation(), $input)
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::url->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$ruleKey => array_merge(explode("|", DynamicFieldTypes::url->getValidation()), [
|
2026-02-10 15:43:47 +01:00
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
2025-11-07 19:00:09 +01:00
|
|
|
|
function ($attribute, $value, $fail) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
ToolboxHelper::assertNoInternalUrls($attribute, $value);
|
|
|
|
|
|
ToolboxHelper::assertNoInternalBoUrls($attribute, $value);
|
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
|
$fail(collect($e->errors())->first()[0]);
|
2025-09-16 16:44:49 +02:00
|
|
|
|
}
|
2025-11-07 19:00:09 +01:00
|
|
|
|
}
|
2025-11-17 10:11:35 +01:00
|
|
|
|
]),
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::email->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::email->getValidation(), $input)
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::image->value:
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$imgRules = $this->compileValidate(DynamicFieldTypes::image->getValidation(), $input);
|
2025-11-17 17:02:22 +01:00
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->pictureRules(implode("|", $imgRules)),
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::list->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
"$ruleKey.*.order" => "required|numeric|min:1",
|
2025-11-17 10:11:35 +01:00
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::list->getValidation(), $input),
|
2025-11-07 19:00:09 +01:00
|
|
|
|
]);
|
|
|
|
|
|
foreach ($input['children'] as $key => $children) {
|
|
|
|
|
|
$rules = array_merge($rules, $this->recursiveRules($children, $ruleKey . ".*"));
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-11-24 16:03:43 +01:00
|
|
|
|
case DynamicFieldTypes::file->value:
|
|
|
|
|
|
$imgRules = $this->compileValidate(DynamicFieldTypes::file->getValidation(), $input);
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->pictureRules(implode("|", $imgRules)),
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-12-02 15:33:30 +01:00
|
|
|
|
case DynamicFieldTypes::vimeoid->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::vimeoid->getValidation(), $input)
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DynamicFieldTypes::youtubeid->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::youtubeid->getValidation(), $input)
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-11-19 12:14:04 +01:00
|
|
|
|
case DynamicFieldTypes::date->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::date->getValidation(), $input)
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-11-19 12:16:44 +01:00
|
|
|
|
case DynamicFieldTypes::color->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::color->getValidation(), $input)
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-12-15 15:18:23 +01:00
|
|
|
|
case DynamicFieldTypes::model->value:
|
|
|
|
|
|
// Set a new rule key for the model name.
|
|
|
|
|
|
$ruleKeyReserved = preg_replace('/([^.\s]+)$/', 'reserved_$1', $ruleKey);
|
2026-02-10 15:43:47 +01:00
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => $this->compileValidate(DynamicFieldTypes::model->getValidation(), $input),
|
2025-12-15 15:18:23 +01:00
|
|
|
|
$ruleKeyReserved => 'required|string|between:3,255',
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2026-02-06 16:25:00 +01:00
|
|
|
|
case DynamicFieldTypes::staticroute->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
2026-02-10 15:43:47 +01:00
|
|
|
|
$ruleKey => array_merge(
|
|
|
|
|
|
$this->compileValidate(DynamicFieldTypes::staticroute->getValidation(), $input),
|
|
|
|
|
|
[new Enum(StaticRoutesEnum::class)],
|
|
|
|
|
|
),
|
2026-02-06 16:25:00 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-12-15 15:32:53 +01:00
|
|
|
|
// Specific inputs.
|
|
|
|
|
|
case DynamicFieldTypes::position->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
2026-02-10 15:43:47 +01:00
|
|
|
|
$ruleKey => array_merge(
|
|
|
|
|
|
$this->compileValidate(DynamicFieldTypes::position->getValidation(), $input),
|
|
|
|
|
|
[new Enum(ElementPositionEnum::class)],
|
|
|
|
|
|
),
|
2025-12-15 15:32:53 +01:00
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2026-02-19 14:06:28 +01:00
|
|
|
|
case DynamicFieldTypes::contactform->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => array_merge(
|
|
|
|
|
|
$this->compileValidate(DynamicFieldTypes::position->getValidation(), $input),
|
|
|
|
|
|
[new Enum(ContactFormType::class)],
|
|
|
|
|
|
),
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2026-03-05 14:30:43 +01:00
|
|
|
|
case DynamicFieldTypes::video->value:
|
|
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
|
|
$ruleKey => new Video(),
|
|
|
|
|
|
]);
|
|
|
|
|
|
break;
|
2025-11-19 12:14:04 +01:00
|
|
|
|
default:
|
|
|
|
|
|
throw new \UnexpectedValueException("Unrecognized dynamic type: " . ($input['type'] ?? 'null'));
|
2025-11-07 19:00:09 +01:00
|
|
|
|
} //end switch
|
2025-09-16 16:44:49 +02:00
|
|
|
|
return $rules;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get custom attributes for validator errors.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function attributes(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$attributes = [];
|
|
|
|
|
|
foreach ($this->configModel->config as $input) {
|
2025-11-07 19:00:09 +01:00
|
|
|
|
$attributes = array_merge($attributes, $this->recursiveAttributes($input, $this->fieldName));
|
|
|
|
|
|
}
|
|
|
|
|
|
return $attributes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-24 16:01:36 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Get attributes from a specific input type.
|
2025-12-02 15:33:30 +01:00
|
|
|
|
*
|
2026-02-10 15:43:47 +01:00
|
|
|
|
* @param array $input
|
|
|
|
|
|
* @param string $prefix
|
2025-11-24 16:01:36 +01:00
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
2026-02-10 15:43:47 +01:00
|
|
|
|
private function recursiveAttributes(array &$input, string $prefix): array
|
2025-11-07 19:00:09 +01:00
|
|
|
|
{
|
|
|
|
|
|
$attributes = [];
|
|
|
|
|
|
$attributeKey = sprintf("%s.%s", $prefix, $input['key']);
|
|
|
|
|
|
$attributes = array_merge($attributes, [$attributeKey => Str::lower($input['label'])]);
|
|
|
|
|
|
if (isset($input['children'])) {
|
|
|
|
|
|
foreach ($input['children'] as $key => $children) {
|
|
|
|
|
|
$attributes = array_merge($attributes, $this->recursiveAttributes($children, $attributeKey . ".*"));
|
|
|
|
|
|
}
|
2025-09-16 16:44:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
return $attributes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Merge native validations and custom validations.
|
|
|
|
|
|
*
|
2025-09-26 15:08:37 +02:00
|
|
|
|
* @param string|array $defaultValidations
|
2025-09-16 16:44:49 +02:00
|
|
|
|
* @param array $input
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
2025-09-26 15:08:37 +02:00
|
|
|
|
private function compileValidate(string|array $defaultValidations, array $input): array
|
2025-09-16 16:44:49 +02:00
|
|
|
|
{
|
2025-09-26 15:08:37 +02:00
|
|
|
|
$newValidations = $this->mergeValidations($defaultValidations, $input);
|
2025-09-16 16:44:49 +02:00
|
|
|
|
|
2025-09-26 15:08:37 +02:00
|
|
|
|
// Get unique rule and priorise important rules first.
|
|
|
|
|
|
$rules = \collect($newValidations)->unique()->sort(function ($valueA, $valueB) {
|
2025-09-16 16:44:49 +02:00
|
|
|
|
$valueA = $this->ruleHasToComeFirst($valueA);
|
|
|
|
|
|
$valueB = $this->ruleHasToComeFirst($valueB);
|
|
|
|
|
|
return $valueA - $valueB;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-26 15:08:37 +02:00
|
|
|
|
// Conditions the rules.
|
2025-09-16 16:44:49 +02:00
|
|
|
|
$hasRequire = $rules->first(fn($rule) => $rule === 'required') ? true : false;
|
|
|
|
|
|
$hasRequireCond = $rules->first(fn($rule) => \strpos($rule, 'required_') !== false) ? true : false;
|
|
|
|
|
|
$hasNullable = $rules->first(fn($rule) => \strpos($rule, 'nullable') !== false) ? true : false;
|
|
|
|
|
|
$hasSometime = $rules->first(fn($rule) => \strpos($rule, 'sometimes') !== false) ? true : false;
|
|
|
|
|
|
$hasPresent = $rules->first(fn($rule) => \strpos($rule, 'present') !== false) ? true : false;
|
|
|
|
|
|
|
|
|
|
|
|
if ($hasRequire and ($hasRequireCond or $hasNullable or $hasSometime or $hasPresent)) {
|
|
|
|
|
|
$rules = $rules->reject(fn($rule) => $rule === 'required');
|
|
|
|
|
|
}
|
2025-09-26 15:08:37 +02:00
|
|
|
|
|
2025-09-16 16:44:49 +02:00
|
|
|
|
return $rules->all();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-26 15:08:37 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Merge new validations into default validations.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string|array $defaultValidations
|
|
|
|
|
|
* @param array $input
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
2026-02-10 15:43:47 +01:00
|
|
|
|
private function mergeValidations(string|array $defaultValidations, array $input): array
|
2025-09-26 15:08:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (\is_string($defaultValidations)) {
|
|
|
|
|
|
$defaultValidations = explode('|', $defaultValidations);
|
|
|
|
|
|
}
|
|
|
|
|
|
/** @var array|string $inputValidations */
|
|
|
|
|
|
$inputValidations = \collect($input)->get('validation', $input);
|
|
|
|
|
|
$inputValidations = \is_string($inputValidations) ? explode('|', $inputValidations) : $inputValidations;
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($inputValidations as $validate) {
|
|
|
|
|
|
// Split a string by a special character.
|
|
|
|
|
|
$parts = explode(':', $validate, 2);
|
|
|
|
|
|
|
|
|
|
|
|
// Check if there is a special character.
|
|
|
|
|
|
if (count($parts) > 1) {
|
|
|
|
|
|
[$ruleName, $ruleValue] = $parts;
|
|
|
|
|
|
|
|
|
|
|
|
// Deletes any old rule that starts with “ruleName:”.
|
|
|
|
|
|
$defaultValidations = collect($defaultValidations)
|
|
|
|
|
|
->reject(function ($existingRule) use ($ruleName) {
|
|
|
|
|
|
return is_string($existingRule) && str_starts_with($existingRule, sprintf('%s:', $ruleName));
|
|
|
|
|
|
})
|
|
|
|
|
|
->values()
|
|
|
|
|
|
->all();
|
|
|
|
|
|
|
|
|
|
|
|
// Add the new rule.
|
|
|
|
|
|
$defaultValidations[] = $validate;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Check if rule without special character already exist.
|
|
|
|
|
|
if (!in_array($validate, $defaultValidations)) {
|
|
|
|
|
|
// Add the input rule.
|
|
|
|
|
|
$defaultValidations[] = $validate;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-10 15:43:47 +01:00
|
|
|
|
} //end foreach
|
2025-09-26 15:08:37 +02:00
|
|
|
|
return $defaultValidations;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 16:44:49 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Does the rule need to come first ?
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $rule
|
|
|
|
|
|
* @return integer
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function ruleHasToComeFirst(string $rule): int
|
|
|
|
|
|
{
|
|
|
|
|
|
$rule = \collect(\explode(':', $rule, 2))->filter()->first(null, '');
|
|
|
|
|
|
$rules = \collect([
|
|
|
|
|
|
'required_if',
|
|
|
|
|
|
'required_unless',
|
|
|
|
|
|
'required_with',
|
|
|
|
|
|
'required_with_all',
|
|
|
|
|
|
'required_without',
|
|
|
|
|
|
'required_without_all',
|
|
|
|
|
|
'required_with_all',
|
|
|
|
|
|
'missing',
|
|
|
|
|
|
'missing_if',
|
|
|
|
|
|
'missing_unless',
|
|
|
|
|
|
'missing_with',
|
|
|
|
|
|
'missing_with_all',
|
|
|
|
|
|
'nullable',
|
|
|
|
|
|
'sometimes',
|
|
|
|
|
|
'present',
|
|
|
|
|
|
'required'
|
|
|
|
|
|
]);
|
|
|
|
|
|
$itemKey = $rules->search(fn(string $firstRule) => \strpos($rule, $firstRule) === 0);
|
|
|
|
|
|
if (\is_bool($itemKey)) {
|
|
|
|
|
|
return $rules->count() + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $itemKey;
|
|
|
|
|
|
}
|
2025-11-18 11:00:56 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Recursively traverses an array and converts it to Boolean
|
|
|
|
|
|
* all values whose key begins with “boolean_”.
|
2025-12-02 15:33:30 +01:00
|
|
|
|
*
|
2025-11-18 11:00:56 +01:00
|
|
|
|
* @param array $data
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function castBooleanFields(array $data): array
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
|
|
// If the value is an array, recursive method.
|
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
|
$data[$key] = $this->castBooleanFields($value);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Check if the key start with the pattern enum_.
|
|
|
|
|
|
if (str_starts_with($key, sprintf("%s_", DynamicFieldTypes::boolean->name))) {
|
2026-02-10 15:43:47 +01:00
|
|
|
|
// Accepts boolean-like values (true/false, one/zero, on/off) as strings or numbers.
|
2025-11-18 11:00:56 +01:00
|
|
|
|
$data[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
|
|
|
|
}
|
2025-12-18 09:59:25 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Extract min and max values from laravel validation.
|
2026-02-06 16:25:00 +01:00
|
|
|
|
*
|
2026-02-10 15:43:47 +01:00
|
|
|
|
* @param array $rules
|
2025-12-18 09:59:25 +01:00
|
|
|
|
* @param array|null $fallback
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function extractMinMax(array $rules, ?array $fallback = null): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$min = $fallback['min'] ?? null;
|
|
|
|
|
|
$max = $fallback['max'] ?? null;
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($rules as $rule) {
|
|
|
|
|
|
if (str_starts_with($rule, 'between:')) {
|
|
|
|
|
|
[$min, $max] = array_map(
|
|
|
|
|
|
'intval',
|
|
|
|
|
|
explode(',', substr($rule, 8))
|
|
|
|
|
|
);
|
|
|
|
|
|
return compact('min', 'max');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (str_starts_with($rule, 'min:')) {
|
|
|
|
|
|
$min = (int) substr($rule, 4);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (str_starts_with($rule, 'max:')) {
|
|
|
|
|
|
$max = (int) substr($rule, 4);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return compact('min', 'max');
|
|
|
|
|
|
}
|
2025-09-16 16:44:49 +02:00
|
|
|
|
}
|