-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathTerms.php
More file actions
90 lines (69 loc) · 2.34 KB
/
Terms.php
File metadata and controls
90 lines (69 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Statamic\Search\Searchables;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Statamic\Contracts\Taxonomies\Term as TermContract;
use Statamic\Facades\Term;
use Statamic\Support\Str;
class Terms extends Provider
{
public static function handle(): string
{
return 'taxonomy';
}
public static function referencePrefix(): string
{
return 'term';
}
public function provide(): Collection|LazyCollection
{
$query = Term::query();
if (! $this->usesWildcard()) {
$query->whereIn('taxonomy', $this->keys);
}
if ($site = $this->index->locale()) {
$query->where('site', $site);
}
$this->applyQueryScope($query);
if ($filter = $this->filter()) {
return $query
->lazy(config('statamic.search.chunk_size'))
->filter($filter)
->values()
->map->reference();
}
return $query->pluck('reference');
}
public function contains($searchable): bool
{
if (! $searchable instanceof TermContract) {
return false;
}
if (! $this->usesWildcard() && ! in_array($searchable->taxonomyHandle(), $this->keys)) {
return false;
}
if (($site = $this->index->locale()) && $site !== $searchable->locale()) {
return false;
}
if ($filter = $this->filter()) {
return $filter($searchable);
}
$query = Term::query()->where('reference', $searchable->reference());
$this->applyQueryScope($query);
return $query->exists();
}
public function find(array $refs): Collection
{
$ids = collect($refs)
->groupBy(fn ($ref) => Str::beforeLast($ref, '::'))
->keys()->all();
// References are provided to this method without the prefix.
$refs = collect($refs)->map(fn ($ref) => $this->referencePrefix().'::'.$ref);
$terms = Term::query()->whereIn('id', $ids)->get();
// Terms would be returned from the query with all localizations, but
// they might not have all beeen requested, so we'll filter them out.
return $terms
->filter(fn ($term) => $refs->contains($term->reference()))
->values();
}
}