1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaFlexibleContent\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection as BaseCollection; |
6
|
|
|
use NovaFlexibleContent\Layouts\Collections\LayoutsCollection; |
7
|
|
|
use NovaFlexibleContent\Layouts\ContentToFlexibleCollectionTransformer; |
8
|
|
|
use NovaFlexibleContent\Layouts\Layout; |
9
|
|
|
use NovaFlexibleContent\Layouts\Preset; |
10
|
|
|
|
11
|
|
|
trait HasFlexible |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Parse a Flexible Content attribute. |
16
|
|
|
*/ |
17
|
2 |
|
public function flexible(string $attribute, array|Preset $layoutMapping = []): LayoutsCollection |
18
|
|
|
{ |
19
|
2 |
|
$value = data_get($this->attributes, $attribute); |
20
|
|
|
|
21
|
2 |
|
return $this->toFlexibleCollection($value ?: null, $layoutMapping); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Parse a Flexible Content from value. |
26
|
|
|
*/ |
27
|
4 |
|
public function toFlexibleCollection(mixed $value, array|Preset $layoutMapping = []): LayoutsCollection |
28
|
|
|
{ |
29
|
4 |
|
return ContentToFlexibleCollectionTransformer::make()->transform($value, $layoutMapping)->each(fn (Layout $l) => $l->setModel($this)); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Transform incoming value into an array of usable layouts. |
34
|
|
|
* @deprecated see ContentToFlexibleCollectionTransformer |
35
|
|
|
*/ |
36
|
|
|
protected function getFlexibleArrayFromValue(mixed $value): ?array |
37
|
|
|
{ |
38
|
|
|
if (is_string($value)) { |
39
|
|
|
$value = json_decode($value, true); |
40
|
|
|
|
41
|
|
|
return is_array($value) ? $value : null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (is_a($value, BaseCollection::class)) { |
45
|
|
|
return $value->all(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (is_array($value)) { |
49
|
|
|
return $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Map array with Flexible Content Layouts. |
57
|
|
|
* @deprecated see ContentToFlexibleCollectionTransformer |
58
|
|
|
*/ |
59
|
|
|
protected function getMappedFlexibleLayouts(array $flexible, array|Preset $layoutMapping = []): array |
60
|
|
|
{ |
61
|
|
|
return array_map(function ($item) use ($layoutMapping) { |
62
|
|
|
return $this->getMappedLayout($item, $layoutMapping); |
|
|
|
|
63
|
|
|
}, $flexible); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Transform given layout value into a usable Layout instance. |
68
|
|
|
* @deprecated see ContentToFlexibleCollectionTransformer |
69
|
|
|
*/ |
70
|
|
|
protected function getMappedLayout(mixed $item, array|Preset $layoutMapping = []): ?Layout |
71
|
|
|
{ |
72
|
|
|
$name = null; |
73
|
|
|
$key = null; |
74
|
|
|
$attributes = []; |
75
|
|
|
|
76
|
|
|
if (is_a($item, \stdClass::class)) { |
77
|
|
|
$item = json_decode(json_encode($item), true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (is_string($item)) { |
81
|
|
|
$item = json_decode($item, true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (is_array($item)) { |
85
|
|
|
$name = $item['layout'] ?? null; |
86
|
|
|
$key = $item['key'] ?? null; |
87
|
|
|
$attributes = (array) $item['attributes'] ?? []; |
88
|
|
|
} elseif (is_a($item, Layout::class)) { |
89
|
|
|
$name = $item->name(); |
90
|
|
|
$key = (string) $item->key(); |
91
|
|
|
$attributes = $item->getAttributes(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!$name) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->createMappedLayout($name, $key, $attributes, $layoutMapping); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Transform given layout value into a usable Layout instance. |
103
|
|
|
* @deprecated see ContentToFlexibleCollectionTransformer |
104
|
|
|
*/ |
105
|
|
|
protected function createMappedLayout(string $name, string $key, array $attributes, array|Preset $layoutMapping = []): Layout |
106
|
|
|
{ |
107
|
|
|
if($layoutMapping instanceof Preset) { |
|
|
|
|
108
|
|
|
$layoutMapping = $layoutMapping->layouts(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$classname = array_key_exists($name, $layoutMapping) |
112
|
|
|
? $layoutMapping[$name] |
113
|
|
|
: Layout::class; |
114
|
|
|
|
115
|
|
|
$layout = new $classname($name, $name, [], $key, $attributes); |
116
|
|
|
|
117
|
|
|
$layout->setModel($this); |
118
|
|
|
|
119
|
|
|
return $layout; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.