-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAttribute.php
More file actions
65 lines (52 loc) · 1.76 KB
/
Copy pathAttribute.php
File metadata and controls
65 lines (52 loc) · 1.76 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
<?php
namespace Rapidez\Core\Models;
use Illuminate\Database\Eloquent\Casts\Attribute as CastsAttribute;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Rapidez\Core\Models\Scopes\Attribute\OnlyProductAttributesScope;
class Attribute extends Model
{
protected $table = 'eav_attribute';
protected $primaryKey = 'attribute_id';
protected $appends = ['prefix'];
protected static function booting()
{
static::addGlobalScope(new OnlyProductAttributesScope);
}
protected function filter(): CastsAttribute
{
return CastsAttribute::make(
get: fn ($value) => $value || in_array($this->code, Arr::pluck(config('rapidez.searchkit.filter_attributes'), 'field')),
)->shouldCache();
}
protected function prefix(): CastsAttribute
{
return CastsAttribute::make(
get: function () {
if ($this->super) {
return 'super_';
}
if ($this->visual_swatch) {
return 'visual_';
}
return '';
}
)->shouldCache();
}
public static function getCached()
{
return Cache::rememberForever('eav_attributes', function () {
return DB::table('eav_attribute')->get()->keyBy('attribute_code');
});
}
public static function getCachedWhere(callable $callback): array
{
$attributes = Cache::store('rapidez:multi')->rememberForever('attributes.' . config('rapidez.store'), function () {
return self::all()->toArray();
});
return Arr::where($attributes, function ($attribute) use ($callback) {
return $callback($attribute);
});
}
}