1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaThinKit\Nova\Flexible\Resolvers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use NovaFlexibleContent\Layouts\Collections\GroupsCollection; |
7
|
|
|
use NovaFlexibleContent\Layouts\Collections\LayoutsCollection; |
8
|
|
|
use NovaFlexibleContent\Value\Resolver; |
9
|
|
|
|
10
|
|
|
class MetaTableResolver implements Resolver |
11
|
|
|
{ |
12
|
|
|
protected string $relationship; |
13
|
|
|
protected string $keyName; |
14
|
|
|
protected string $dataKeyName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $relationship |
18
|
|
|
*/ |
19
|
5 |
|
public function __construct(string $relationship, string $keyName = 'key', string $dataKeyName = 'data') |
20
|
|
|
{ |
21
|
5 |
|
$this->relationship = $relationship; |
22
|
5 |
|
$this->keyName = $keyName; |
23
|
5 |
|
$this->dataKeyName = $dataKeyName; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inerhitDoc |
28
|
|
|
*/ |
29
|
5 |
|
public function get(mixed $resource, string $attribute, LayoutsCollection $groups): GroupsCollection |
30
|
|
|
{ |
31
|
5 |
|
$meta = $resource->{$this->relationship}()->where($this->keyName, $attribute)->first(); |
32
|
5 |
|
if ($meta) { |
33
|
1 |
|
$value = static::extractValueFromResource($meta, $this->dataKeyName); |
34
|
|
|
|
35
|
1 |
|
return GroupsCollection::make($value)->map(function (LayoutValue $item) use ($groups) { |
|
|
|
|
36
|
1 |
|
$layout = $groups->find($item->layout); |
|
|
|
|
37
|
|
|
|
38
|
1 |
|
if (!$layout) { |
39
|
|
|
throw new \Exception("Layout [{$item->layout}] not found"); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
return $layout->duplicate($item->key, (array)$item->attributes) |
|
|
|
|
43
|
1 |
|
->setCollapsed((bool)$item->collapsed); |
|
|
|
|
44
|
1 |
|
})->filter()->values(); |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
return GroupsCollection::make([]); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $resource |
52
|
|
|
* @param string $attribute |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
1 |
|
public static function extractValueFromResource($resource, string $attribute = 'data'): array |
56
|
|
|
{ |
57
|
1 |
|
$value = data_get($resource, str_replace('->', '.', $attribute)) ?? []; |
58
|
|
|
|
59
|
1 |
|
if ($value instanceof Collection) { |
60
|
|
|
$value = $value->toArray(); |
61
|
1 |
|
} elseif (is_string($value)) { |
62
|
1 |
|
$value = json_decode($value, true) ?? []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Fail silently in case data is invalid |
66
|
1 |
|
if (!is_array($value)) { |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return array_filter(array_map(function ($item) { |
71
|
1 |
|
$layoutValue = new LayoutValue(!is_array($item) ? (array)$item : $item); |
72
|
1 |
|
if ($layoutValue->layout && $layoutValue->key && is_array($layoutValue->attributes)) { |
|
|
|
|
73
|
1 |
|
return $layoutValue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
1 |
|
}, $value)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inerhitDoc |
82
|
|
|
*/ |
83
|
1 |
|
public function set(mixed $resource, string $attribute, GroupsCollection $groups): string |
84
|
|
|
{ |
85
|
1 |
|
$resource->{$this->relationship}()->updateOrCreate( |
86
|
1 |
|
[$this->keyName => $attribute], |
87
|
1 |
|
[$this->dataKeyName => $groups->map(function ($group) { |
88
|
1 |
|
return [ |
89
|
1 |
|
'collapsed' => $group->isCollapsed(), |
90
|
1 |
|
'layout' => $group->name(), |
91
|
1 |
|
'key' => $group->key(), |
92
|
1 |
|
'attributes' => $group->getAttributes(), |
93
|
1 |
|
]; |
94
|
1 |
|
})] |
95
|
1 |
|
); |
96
|
|
|
|
97
|
1 |
|
return ''; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|