98 lines
2.7 KiB
PHP
98 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
use Illuminate\Http\Request as HttpRequest;
|
|
use Shared\Pagination\PageNumber;
|
|
use Shared\Pagination\PageSize;
|
|
use Shared\Pagination\Pagination;
|
|
use Uri\Rfc3986\Uri;
|
|
|
|
final class RequestHelper
|
|
{
|
|
private function __construct()
|
|
{
|
|
// Static class
|
|
}
|
|
|
|
public static function pagination(HttpRequest $request, int $defaultPageSize = 25): Pagination
|
|
{
|
|
if ($request->has('offset') || $request->has('limit')) {
|
|
return Pagination::ofOffsetAndLimit(
|
|
$request->integer('offset', 0),
|
|
$request->integer('limit', $defaultPageSize)
|
|
);
|
|
}
|
|
|
|
$pageSize = $request->integer(
|
|
'pageSize',
|
|
$request->integer('per_page', $request->integer('pagination', $defaultPageSize))
|
|
);
|
|
|
|
return Pagination::ofPage(
|
|
new PageNumber($request->integer('page', 1)),
|
|
new PageSize($pageSize)
|
|
);
|
|
}
|
|
|
|
public static function toUri(HttpRequest $request): Uri
|
|
{
|
|
return new Uri($request->url());
|
|
}
|
|
|
|
public static function toFullUri(HttpRequest $request): Uri
|
|
{
|
|
return new Uri($request->fullUrl());
|
|
}
|
|
|
|
/**
|
|
* Build configuration array for the Back-Office image input Vue component.
|
|
*
|
|
* @param list<string> $allowedExtensions
|
|
* @return array{
|
|
* name: string,
|
|
* single: bool,
|
|
* width: int,
|
|
* height: int,
|
|
* preview: bool,
|
|
* accept: string,
|
|
* helper: string,
|
|
* value: string,
|
|
* }
|
|
*/
|
|
public static function imageInputConfig(
|
|
string $name,
|
|
int $width,
|
|
int $height,
|
|
int $maxSizeKb,
|
|
array $allowedExtensions,
|
|
string $value = '',
|
|
): array {
|
|
$maxSizeMb = round($maxSizeKb / 1024, 1);
|
|
$formattedSize = fmod($maxSizeMb, 1.0) === 0.0
|
|
? (string) (int) $maxSizeMb
|
|
: (string) $maxSizeMb;
|
|
$prefixedExtensions = array_map(
|
|
static fn (string $ext): string => '.' . ltrim($ext, '.'),
|
|
$allowedExtensions,
|
|
);
|
|
|
|
return [
|
|
'name' => $name,
|
|
'single' => true,
|
|
'width' => $width,
|
|
'height' => $height,
|
|
'preview' => false,
|
|
'accept' => implode(',', $prefixedExtensions),
|
|
'helper' => __('Allowed extensions: :extensions (:maxSize MB max). Required dimensions: :width px by :height px', [
|
|
'extensions' => implode(', ', $prefixedExtensions),
|
|
'maxSize' => $formattedSize,
|
|
'width' => $width,
|
|
'height' => $height,
|
|
]),
|
|
'value' => $value,
|
|
];
|
|
}
|
|
}
|