$array1 * @param array $array2 * @return array */ 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; } /** * Strip scheme (https) on oembed urls * Only embed allowed are for MP4 self hosted videos right now * * https is force by ckeditor MediaElements which natively supports * only cloud hosted medias (youtube, spotify etc..) */ public static function stripCkEditorSchemesOnMediaInputs(string $input): string { return Str::of($input) ->replaceMatches('/(?<=)/m', '') ->toString(); } /** * Asserts that the field is unique. * * @param array|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); } /** * Validate picture * * @param callable(string):void $fail A callback that should be called if the validation fails. * @param object{ * minWidth: int, * maxWidth: int, * minHeight: int, * maxHeight: int * }&\stdClass $sizes An object containing the minimum and maximum width and height for the picture. * * @throws \Illuminate\Validation\ValidationException If the picture is invalid. */ public static function validatePicture(string $attribute, ?string $value, callable $fail, object $sizes): void { if ($value === null) { return; } if (! ($useStorage = Storage::exists($value)) && ! File::exists($value)) { $fail(trans('validation.custom.has_picture.file_does_not_exist', [ 'attribute' => $attribute, ])); return; } $fileFullPath = self::getUploadedFileFullPath($value, $useStorage); if ( ! \in_array(self::getUploadedFileMimeType($value, $useStorage), [ 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', ]) ) { $fail(trans('validation.mimetypes', [ 'attribute' => $attribute, 'values' => 'image/jpeg, image/jpg, image/png, image/gif', ])); } if (! ($dimensions = \getimagesize($fileFullPath))) { $fail(trans('validation.dimensions', [ 'attribute' => $attribute, ])); return; } $width = intval($dimensions[0]); $height = intval($dimensions[1]); $xMessage = trans('validation.custom.has_picture.width_between', [ 'attribute' => $attribute, 'min' => "{$sizes->minWidth}px", 'max' => "{$sizes->maxWidth}px", ]); if ($sizes->minWidth === $sizes->maxWidth) { $xMessage = trans('validation.custom.has_picture.width_equal', [ 'attribute' => $attribute, 'value' => "{$sizes->minWidth}px", ]); } $yMessage = trans('validation.custom.has_picture.height_between', [ 'attribute' => $attribute, 'min' => "{$sizes->minHeight}px", 'max' => "{$sizes->maxHeight}px", ]); if ($sizes->minHeight === $sizes->maxHeight) { $yMessage = trans('validation.custom.has_picture.height_equal', [ 'attribute' => $attribute, 'value' => "{$sizes->minHeight}px", ]); } if (($width < $sizes->minWidth) || ($width > $sizes->maxWidth)) { $fail($xMessage); } if (($height < $sizes->minHeight) || ($height > $sizes->maxHeight)) { $fail($yMessage); } } }