2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
|
|
|
|
|
class ToolboxHelper extends ToolboxHelperCore
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param array<int|string, mixed> $array1
|
|
|
|
|
* @param array<int|string, mixed> $array2
|
|
|
|
|
* @return array<int|string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
public static function arrayMergeRecursiveDistinct(array &$array1, array &$array2): array
|
|
|
|
|
{
|
|
|
|
|
$merged = $array1;
|
|
|
|
|
foreach ($array2 as $key => &$value) {
|
|
|
|
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
|
|
|
|
|
$merged[$key] = self::arrayMergeRecursiveDistinct($merged[$key], $value);
|
|
|
|
|
} else {
|
|
|
|
|
$merged[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $merged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts that the field is unique.
|
|
|
|
|
*
|
|
|
|
|
* @param array<mixed>|string|int|float|bool|null $value
|
|
|
|
|
*
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If the field is not unique.
|
|
|
|
|
*/
|
|
|
|
|
public static function assertFieldIsUnique(
|
|
|
|
|
string $table,
|
|
|
|
|
string $field,
|
|
|
|
|
string | int | float | bool | null | array $value,
|
|
|
|
|
null | int | string $id = null,
|
|
|
|
|
\Closure | string | null $query = null
|
|
|
|
|
): void {
|
|
|
|
|
if (! $query) {
|
|
|
|
|
Validator::make([$field => $value], [
|
|
|
|
|
(string) str_replace('.', '\.', $field) => Rule::unique($table, $field)->ignore($id),
|
|
|
|
|
], [
|
|
|
|
|
(string) str_replace('.', '\.', "{$field}.unique") => trans(
|
|
|
|
|
'crud.libs.toolbox.assertFieldIsUnique',
|
|
|
|
|
['attribute' => $field, 'value' => is_array($value) ? json_encode($value) : $value]
|
|
|
|
|
),
|
|
|
|
|
])->validate();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Validator::make([$field => $value], [
|
|
|
|
|
$field => Rule::unique($table)->where($query)->ignore($id),
|
|
|
|
|
], [
|
|
|
|
|
$field . '.unique' => trans(
|
|
|
|
|
'crud.libs.toolbox.assertFieldIsUnique',
|
|
|
|
|
['attribute' => $field, 'value' => is_array($value) ? json_encode($value) : $value]
|
|
|
|
|
),
|
|
|
|
|
])->validate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get original polyfill with autotranslate behavior
|
|
|
|
|
*/
|
|
|
|
|
public static function getOriginal(Model $model, string $attribute): mixed
|
|
|
|
|
{
|
2026-10-28 08:53:34 +01:00
|
|
|
|
|
|
|
|
// @phpstan-ignore method.notFound (\Spatie\Translatable\HasTranslations could be absent)
|
|
|
|
|
if (\in_array('Spatie\Translatable\HasTranslations', \class_uses_recursive($model)) && $model->isTranslatableAttribute($attribute)) {
|
|
|
|
|
// @phpstan-ignore method.notFound (\Spatie\Translatable\HasTranslations could be absent)
|
2026-06-14 21:32:07 +02:00
|
|
|
return $model->getTranslation(
|
|
|
|
|
$attribute,
|
2026-10-28 08:53:34 +01:00
|
|
|
// @phpstan-ignore method.notFound (\Spatie\Translatable\HasTranslations could be absent)
|
2026-06-14 21:32:07 +02:00
|
|
|
$model->getLocale(),
|
2026-10-28 08:53:34 +01:00
|
|
|
// @phpstan-ignore method.notFound (\Spatie\Translatable\HasTranslations could be absent)
|
2026-06-14 21:32:07 +02:00
|
|
|
$model->useFallbackLocale()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $model->getOriginal($attribute);
|
|
|
|
|
}
|
|
|
|
|
}
|