1 | <?php |
||||
2 | |||||
3 | namespace NovaFlexibleContent\Value; |
||||
4 | |||||
5 | use Illuminate\Support\Collection; |
||||
6 | use Laravel\Nova\Makeable; |
||||
7 | use NovaFlexibleContent\Layouts\Collections\GroupsCollection; |
||||
8 | use NovaFlexibleContent\Layouts\Collections\LayoutsCollection; |
||||
9 | use NovaFlexibleContent\Layouts\Layout; |
||||
10 | |||||
11 | class DefaultResolver implements Resolver |
||||
12 | { |
||||
13 | use Makeable; |
||||
14 | |||||
15 | protected ?\Closure $set = null; |
||||
16 | protected ?\Closure $get = null; |
||||
17 | |||||
18 | 15 | public function __construct(?\Closure $set = null, ?\Closure $get = null) |
|||
19 | { |
||||
20 | 15 | $this->set = $set; |
|||
21 | 15 | $this->get = $get; |
|||
22 | } |
||||
23 | |||||
24 | /** |
||||
25 | * @inerhitDoc |
||||
26 | */ |
||||
27 | 4 | public function set(mixed $resource, string $attribute, GroupsCollection $groups): string |
|||
28 | { |
||||
29 | 4 | $value = $groups->map(function (Layout $group) { |
|||
30 | 4 | return [ |
|||
31 | 4 | 'layout' => $group->name(), |
|||
32 | 4 | 'key' => $group->key(), |
|||
33 | 4 | 'collapsed' => $group->isCollapsed(), |
|||
34 | 4 | 'attributes' => $group->getAttributes(), |
|||
35 | 4 | ]; |
|||
36 | 4 | }); |
|||
37 | |||||
38 | 4 | if ($this->set) { |
|||
39 | call_user_func($this->set, $resource, $value, $attribute, $groups); |
||||
40 | } else { |
||||
41 | 4 | $resource->$attribute = $value; |
|||
42 | } |
||||
43 | |||||
44 | 4 | return $value; |
|||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * @inerhitDoc |
||||
49 | */ |
||||
50 | 13 | public function get(mixed $resource, string $attribute, LayoutsCollection $groups): GroupsCollection |
|||
51 | { |
||||
52 | 13 | if ($this->get) { |
|||
53 | $value = call_user_func($this->get, $resource, $attribute, $groups); |
||||
54 | } else { |
||||
55 | 13 | $value = $this->extractValueFromResource($resource, $attribute); |
|||
56 | } |
||||
57 | |||||
58 | // Fail silently in case data is invalid |
||||
59 | 13 | if (!is_array($value)) { |
|||
60 | $value = []; |
||||
61 | } |
||||
62 | |||||
63 | // Force transform arrays to objects |
||||
64 | 13 | $value = array_map(function ($item) { |
|||
65 | 7 | return is_array($item) ? (object)$item : $item; |
|||
66 | 13 | }, $value); |
|||
67 | |||||
68 | 13 | return GroupsCollection::make($value)->map(function ($item) use ($groups, $attribute) { |
|||
0 ignored issues
–
show
$value of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
69 | 7 | if ($item instanceof Layout) { |
|||
70 | return $item; |
||||
71 | } |
||||
72 | |||||
73 | 7 | if ($layout = $groups->find($item->layout)) { |
|||
74 | 7 | return $layout->duplicate($item->key, (array)$item->attributes) |
|||
75 | 7 | ->setCollapsed((bool)($item->collapsed ?? false)); |
|||
76 | } |
||||
77 | |||||
78 | return null; |
||||
79 | 13 | })->filter()->values(); |
|||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * Find the attribute's value in the given resource |
||||
84 | * |
||||
85 | * @param mixed $resource |
||||
86 | * @param string $attribute |
||||
87 | * @return array |
||||
88 | */ |
||||
89 | 13 | protected function extractValueFromResource($resource, $attribute) |
|||
90 | { |
||||
91 | 13 | $value = data_get($resource, str_replace('->', '.', $attribute)) ?? []; |
|||
92 | |||||
93 | 13 | if ($value instanceof Collection) { |
|||
94 | $value = $value->all(); |
||||
95 | 13 | } elseif (is_string($value)) { |
|||
96 | 7 | $value = json_decode($value) ?? []; |
|||
97 | } |
||||
98 | |||||
99 | 13 | return $value; |
|||
100 | } |
||||
101 | } |
||||
102 |
This check looks for imports that have been defined, but are not used in the scope.