-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathAssetQueryBuilder.php
More file actions
79 lines (66 loc) · 2.42 KB
/
AssetQueryBuilder.php
File metadata and controls
79 lines (66 loc) · 2.42 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
<?php
namespace Statamic\Eloquent\Assets;
use Illuminate\Support\Collection;
use Statamic\Assets\AssetCollection;
use Statamic\Contracts\Assets\AssetContainer;
use Statamic\Contracts\Assets\QueryBuilder;
use Statamic\Eloquent\QueriesJsonColumns;
use Statamic\Facades;
use Statamic\Fields\Field;
use Statamic\Query\EloquentQueryBuilder;
class AssetQueryBuilder extends EloquentQueryBuilder implements QueryBuilder
{
use QueriesJsonColumns;
const COLUMNS = [
'id', 'container', 'folder', 'basename', 'filename', 'extension', 'path', 'created_at', 'updated_at',
];
const META_COLUMNS = [
'size', 'width', 'height', 'duration', 'mime_type', 'last_modified',
];
protected function column($column)
{
if (in_array($column, self::META_COLUMNS)) {
$column = 'meta->'.$column;
} elseif (! in_array($column, self::COLUMNS)) {
$column = 'meta->data->'.$column;
}
return $column;
}
protected function transform($items, $columns = [])
{
return AssetCollection::make($items)->map(function ($model) use ($columns) {
return app('statamic.eloquent.assets.asset')::fromModel($model)
->selectedQueryColumns($this->selectedQueryColumns ?? $columns);
});
}
public function with($relations, $callback = null)
{
return $this;
}
protected function getJsonCasts(): Collection
{
$wheres = collect($this->builder->getQuery()->wheres);
$containerWhere = $wheres->firstWhere('column', 'container');
if (! $containerWhere || ! isset($containerWhere['value'])) {
return [
'size' => 'float',
'width' => 'float',
'height' => 'float',
'duration' => 'float',
];
}
$container = $containerWhere['value'] instanceof AssetContainer
? $containerWhere['value']
: Facades\AssetContainer::find($containerWhere['value']);
return $container->blueprint()->fields()->all()
->filter(fn (Field $field): bool => in_array($field->type(), ['float', 'integer', 'date']))
->map(fn (Field $field): string => $this->toCast($field))
->filter()
->merge([
'size' => 'float',
'width' => 'float',
'height' => 'float',
'duration' => 'float',
]);
}
}