Passed
Push — main ( 66769c...c749bb )
by Yaroslav
19:17
created

HasFlexible::getFlexibleArrayFromValue()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 17
ccs 0
cts 9
cp 0
crap 30
rs 9.6111
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));
0 ignored issues
show
Deprecated Code introduced by
The function NovaFlexibleContent\Layouts\Layout::setModel() has been deprecated: I have no Idea where this used, and why we should keep it? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
        return ContentToFlexibleCollectionTransformer::make()->transform($value, $layoutMapping)->each(fn (Layout $l) => /** @scrutinizer ignore-deprecated */ $l->setModel($this));

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.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function NovaFlexibleContent\Conc...ible::getMappedLayout() has been deprecated: see ContentToFlexibleCollectionTransformer ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
            return /** @scrutinizer ignore-deprecated */ $this->getMappedLayout($item, $layoutMapping);

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.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function NovaFlexibleContent\Conc...e::createMappedLayout() has been deprecated: see ContentToFlexibleCollectionTransformer ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

98
        return /** @scrutinizer ignore-deprecated */ $this->createMappedLayout($name, $key, $attributes, $layoutMapping);

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.

Loading history...
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) {
0 ignored issues
show
introduced by
$layoutMapping is never a sub-type of NovaFlexibleContent\Layouts\Preset.
Loading history...
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