ldc-ovoteam/app/Lib/Helpers/ToolboxHelper.php

63 lines
1.9 KiB
PHP
Raw Normal View History

<?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(
'validation.unique',
['attribute' => $field]
),
])->validate();
return;
}
Validator::make([$field => $value], [
$field => Rule::unique($table)->where($query)->ignore($id),
], [
$field . '.unique' => trans(
'validation.unique',
['attribute' => $field]
),
])->validate();
}
}