Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions sourcecode/hub/app/Http/Requests/ContentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
use Laravel\Scout\Builder;
use Override;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use function abort;
use function trans;

class ContentFilter extends FormRequest
{
/** @var Builder<Content> */
/** @var Builder<Content> */
private Builder $builder;
private bool $forUser = false;
private bool $languageChanged = false;
private bool $queryChanged = false;
private bool $typesChanged = false;

#[Override]
protected function failedValidation(Validator $validator): never
#[Override] protected function failedValidation(Validator $validator): never
{
abort(404);
}
Expand All @@ -42,9 +42,8 @@ public function rules(): array
return [
'q' => ['sometimes', 'string', 'max:300'],
'language' => ['sometimes', 'string', 'max:100'],
'sort' => ['sometimes', 'required', Rule::in('created', 'updated', 'views')],
'sort' => ['sometimes', Rule::in('created', 'updated', 'views')],
'type' => ['sometimes', 'array'],
'type.*' => ['string', 'max:255', 'regex:/^[a-zA-Z0-9._+ ]+$/'],
];
}

Expand Down Expand Up @@ -90,13 +89,15 @@ public function getLanguageOptions(bool $withExpectedHits = false): array

return $options
->map(
fn(int $value, string $key) => $key === ''
? trans('messages.filter-language-all')
: (locale_get_display_name($key, $displayLocale) ?: (locale_get_display_name($key, $fallBack) ?: $key)),
fn(int $value, string $key) =>
$key === ''
? trans('messages.filter-language-all')
: (locale_get_display_name($key, $displayLocale) ?: (locale_get_display_name($key, $fallBack) ?: $key)),
)
->when(
$withExpectedHits,
fn(Collection $items) => $items->map(fn(string $value, string $key) => sprintf('%s (%d)', $value, $options[$key] ?? 0)),
fn(Collection $items) =>
$items->map(fn(string $value, string $key) => sprintf('%s (%d)', $value, $options[$key] ?? 0)),
)
->sort()
->toArray();
Expand Down Expand Up @@ -216,7 +217,8 @@ public function applyCriteria(Builder $query): Builder
count($this->getContentTypes()) > 0,
fn(Builder $query) => $query
->whereIn('content_type', $this->getContentTypes()),
);
)
;

$this->builder = clone($query);

Expand Down Expand Up @@ -304,6 +306,8 @@ public function getWithModel(Builder $builder, int $limit, bool $forUser = false
*/
private function attachModel(array $hits, bool $forUser, bool $showDrafts): Collection
{
$hits = new Collection($hits);

$eagerLoad = ['users'];
if ($showDrafts) {
$eagerLoad[] = 'latestVersion';
Expand All @@ -312,14 +316,12 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
$eagerLoad[] = 'latestPublishedVersion';
}

/** @phpstan-ignore-next-line */
$contents = Content::whereIn('id', $hits->pluck('id'))
->with($eagerLoad)
->withCount(['views'])
->get()
->keyBy('id');

/** @phpstan-ignore-next-line */
return $hits
->map(fn(array $item) => [
...$item,
Expand Down
70 changes: 0 additions & 70 deletions sourcecode/hub/tests/Feature/ContentFilterTest.php

This file was deleted.

Loading