ldc-cocotte/app/Lib/Helpers/ToolboxHelper.php
Jérémy Munsch 8986570e1d
refactor(ToolboxHelper): remove unused method and import
Cleaned up ToolboxHelper by removing the unused stripCkEditorSchemesOnMediaInputs method
and its associated import, streamlining the code.
2026-11-15 16:45:06 +01:00

88 lines
3.1 KiB
PHP

<?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
{
// @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)
return $model->getTranslation(
$attribute,
// @phpstan-ignore method.notFound (\Spatie\Translatable\HasTranslations could be absent)
$model->getLocale(),
// @phpstan-ignore method.notFound (\Spatie\Translatable\HasTranslations could be absent)
$model->useFallbackLocale()
);
}
return $model->getOriginal($attribute);
}
}