2026-07-02 17:15:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
|
|
|
|
|
class ToolboxHelper extends ToolboxHelperCore
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 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('/(?<=<oembed url=")(http:\/\/)(?=.*?">)/m', '')
|
|
|
|
|
->toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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(
|
2026-11-04 09:55:57 +01:00
|
|
|
'validation.unique',
|
|
|
|
|
['attribute' => $field]
|
2026-07-02 17:15:07 +02:00
|
|
|
),
|
|
|
|
|
])->validate();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Validator::make([$field => $value], [
|
|
|
|
|
$field => Rule::unique($table)->where($query)->ignore($id),
|
|
|
|
|
], [
|
|
|
|
|
$field . '.unique' => trans(
|
2026-11-04 09:55:57 +01:00
|
|
|
'validation.unique',
|
|
|
|
|
['attribute' => $field]
|
2026-07-02 17:15:07 +02:00
|
|
|
),
|
|
|
|
|
])->validate();
|
|
|
|
|
}
|
|
|
|
|
}
|