dev-think-one /
nova-flexible-content
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace NovaFlexibleContent\Layouts\LayoutTraits; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Arr; |
||||||
| 6 | use Illuminate\Support\Str; |
||||||
| 7 | use NovaFlexibleContent\Layouts\Collections\LayoutsCollection; |
||||||
| 8 | use NovaFlexibleContent\Layouts\Layout; |
||||||
| 9 | use NovaFlexibleContent\Layouts\Preset; |
||||||
| 10 | |||||||
| 11 | trait ModelEmulates |
||||||
| 12 | { |
||||||
| 13 | use AttributesManipulation; |
||||||
| 14 | |||||||
| 15 | /** |
||||||
| 16 | * Define that Layout is a model, when in fact it is not. |
||||||
| 17 | * |
||||||
| 18 | * @var bool |
||||||
| 19 | */ |
||||||
| 20 | protected bool $exists = false; |
||||||
| 21 | |||||||
| 22 | /** |
||||||
| 23 | * Define that Layout is a model, when in fact it is not. |
||||||
| 24 | * |
||||||
| 25 | * @var bool |
||||||
| 26 | */ |
||||||
| 27 | protected bool $wasRecentlyCreated = false; |
||||||
| 28 | |||||||
| 29 | /** |
||||||
| 30 | * The relation resolver callbacks for the Layout. |
||||||
| 31 | * |
||||||
| 32 | * @var array |
||||||
| 33 | */ |
||||||
| 34 | protected static array $relationResolvers = []; |
||||||
| 35 | |||||||
| 36 | /** |
||||||
| 37 | * The loaded relationships for the Layout. |
||||||
| 38 | * |
||||||
| 39 | * @var array |
||||||
| 40 | */ |
||||||
| 41 | protected $relations = []; |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * Check if relation exists. |
||||||
| 45 | * Layouts do not have relations. |
||||||
| 46 | */ |
||||||
| 47 | 9 | public function relationLoaded($key): bool |
|||||
| 48 | { |
||||||
| 49 | 9 | return false; |
|||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | /** |
||||||
| 53 | * Get the value indicating whether the IDs are incrementing. |
||||||
| 54 | * Layouts do not have increment identifier. |
||||||
| 55 | */ |
||||||
| 56 | 13 | public function getIncrementing(): bool |
|||||
| 57 | { |
||||||
| 58 | 13 | return false; |
|||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | /** |
||||||
| 62 | * Determine if the model uses timestamps. |
||||||
| 63 | * Layouts do not use timestamps. |
||||||
| 64 | */ |
||||||
| 65 | 13 | public function usesTimestamps(): bool |
|||||
| 66 | { |
||||||
| 67 | 13 | return false; |
|||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | /** |
||||||
| 71 | * Get the dynamic relation resolver if defined or inherited, or return null. |
||||||
| 72 | * Since it is not possible to define a relation on a layout, this method |
||||||
| 73 | * returns null |
||||||
| 74 | * |
||||||
| 75 | * @param string $class |
||||||
| 76 | * @param string $key |
||||||
| 77 | * @return mixed |
||||||
| 78 | */ |
||||||
| 79 | 9 | public function relationResolver($class, $key) |
|||||
| 80 | { |
||||||
| 81 | 9 | return null; |
|||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | /** |
||||||
| 85 | * Force Fill the layout with an array of attributes. |
||||||
| 86 | */ |
||||||
| 87 | 5 | public function forceFill(array $attributes) |
|||||
| 88 | { |
||||||
| 89 | 5 | foreach ($attributes as $key => $value) { |
|||||
| 90 | 5 | $attribute = Str::replace('->', '.', $key); |
|||||
| 91 | 5 | Arr::set($this->attributes, $attribute, $value); |
|||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 92 | } |
||||||
| 93 | |||||||
| 94 | 5 | return $this; |
|||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | 1 | public function groups(string $fieldName, string|array|null $type = null): LayoutsCollection |
|||||
| 98 | { |
||||||
| 99 | 1 | $methodName = "{$fieldName}Preset"; |
|||||
| 100 | 1 | if ($fieldName && method_exists($this, $methodName)) { |
|||||
| 101 | 1 | $preset = $this->$methodName(); |
|||||
| 102 | 1 | if ($preset instanceof Preset) { |
|||||
| 103 | 1 | $value = $this->flexible($fieldName, $preset); |
|||||
|
0 ignored issues
–
show
It seems like
flexible() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 104 | 1 | if($value->isEmpty()) { |
|||||
| 105 | // Support snake_case |
||||||
| 106 | 1 | $value = $this->flexible(Str::snake($fieldName), $preset); |
|||||
| 107 | } |
||||||
| 108 | 1 | if ($type) { |
|||||
| 109 | 1 | $value = $value->whereName($type); |
|||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | 1 | return $value; |
|||||
| 113 | } |
||||||
| 114 | } |
||||||
| 115 | |||||||
| 116 | 1 | return LayoutsCollection::make(); |
|||||
| 117 | } |
||||||
| 118 | |||||||
| 119 | 1 | public function group(string $fieldName, string|array|null $type = null): ?Layout |
|||||
| 120 | { |
||||||
| 121 | 1 | return $this->groups($fieldName, $type)->first(); |
|||||
| 122 | } |
||||||
| 123 | |||||||
| 124 | 2 | public function __get($key) |
|||||
| 125 | { |
||||||
| 126 | 2 | if (Str::startsWith($key, 'flexible')) { |
|||||
| 127 | 1 | $field = Str::camel(Str::after($key, 'flexible')); |
|||||
| 128 | |||||||
| 129 | 1 | if ($value = $this->groups($field)) { |
|||||
| 130 | 1 | return $value; |
|||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | 2 | return parent::__get($key); |
|||||
| 135 | } |
||||||
| 136 | } |
||||||
| 137 |