1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\Models; |
6
|
|
|
|
7
|
|
|
use Rinvex\Tenants\Traits\Tenantable; |
8
|
|
|
use Cortex\Foundation\Traits\Auditable; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Rinvex\Support\Traits\HashidsTrait; |
11
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
12
|
|
|
use Rinvex\Attributes\Models\Attribute as BaseAttribute; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cortex\Attributes\Models\Attribute. |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property string $slug |
19
|
|
|
* @property string $name |
20
|
|
|
* @property array $description |
21
|
|
|
* @property int $sort_order |
22
|
|
|
* @property string $group |
23
|
|
|
* @property string $type |
24
|
|
|
* @property bool $is_required |
25
|
|
|
* @property bool $is_collection |
26
|
|
|
* @property string $default |
27
|
|
|
* @property \Carbon\Carbon|null $created_at |
28
|
|
|
* @property \Carbon\Carbon|null $updated_at |
29
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[] $activity |
30
|
|
|
* @property array $entities |
31
|
|
|
* @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values |
32
|
|
|
* |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute ordered($direction = 'asc') |
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereCreatedAt($value) |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDefault($value) |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDescription($value) |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereGroup($value) |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereId($value) |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsCollection($value) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsRequired($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSlug($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSortOrder($value) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($value) |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereType($value) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereUpdatedAt($value) |
46
|
|
|
* @mixin \Eloquent |
47
|
|
|
*/ |
48
|
|
|
class Attribute extends BaseAttribute |
49
|
|
|
{ |
50
|
|
|
use Auditable; |
51
|
|
|
use Tenantable; |
52
|
|
|
use HashidsTrait; |
53
|
|
|
use LogsActivity; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Indicates whether to log only dirty attributes or all. |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected static $logOnlyDirty = true; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The attributes that are logged on change. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected static $logFillable = true; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The attributes that are ignored on change. |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected static $ignoreChangedAttributes = [ |
75
|
|
|
'created_at', |
76
|
|
|
'updated_at', |
77
|
|
|
'deleted_at', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the route key for the model. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getRouteKeyName() |
86
|
|
|
{ |
87
|
|
|
return 'slug'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the route key for the model. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Database\Eloquent\Model $entity |
94
|
|
|
* @param string $accessArea |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\View\View |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
public function render(Model $entity, string $accessArea): string |
99
|
|
|
{ |
100
|
|
|
$default = ''; |
101
|
|
|
$selected = ''; |
102
|
|
|
|
103
|
|
|
switch ($this->type) { |
104
|
|
|
case 'select': |
|
|
|
|
105
|
|
|
|
106
|
|
|
$default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use (&$selected) { |
|
|
|
|
107
|
|
|
if (mb_strpos($item, '=')) { |
108
|
|
|
$key = mb_strstr($item, '=', true); |
109
|
|
|
$value = str_replace_first('=', '', mb_strstr($item, '=')); |
110
|
|
|
|
111
|
|
|
// Check for SELECTED itmes (marked by asterisk) |
112
|
|
|
! str_contains($value, '*') || $selected = $key; |
113
|
|
|
! str_contains($value, '*') || $value = str_replace_first('*', '', $value); |
114
|
|
|
} else { |
115
|
|
|
$key = $value = $item; |
116
|
|
|
|
117
|
|
|
// Check for SELECTED itmes (marked by asterisk) |
118
|
|
|
! str_contains($value, '*') || $key = $value = $selected = str_replace_first('*', '', $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return [$key => $value]; |
122
|
|
|
})->collapse(); |
123
|
|
|
|
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return view("cortex/attributes::$accessArea.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default, 'selected' => $selected])->render(); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.