Issues (71)

src/Layouts/Preset.php (1 issue)

1
<?php
2
3
namespace NovaFlexibleContent\Layouts;
4
5
use NovaFlexibleContent\Flexible;
6
7
class Preset
8
{
9
    /**
10
     * @var array
11
     */
12
    protected array $usedLayouts = [];
13
14
    /**
15
     * @var array[string]string
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[string]string at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
16
     */
17
    protected array $layouts = [];
18
19 11
    public function __construct()
20
    {
21 11
        $this->setLayouts($this->usedLayouts);
22
    }
23
24
    /**
25
     * Initialise new instance using layouts set.
26
     */
27 11
    public static function withLayouts(array $usedLayouts = []): static
28
    {
29 11
        return (new static())->setLayouts($usedLayouts);
30
    }
31
32 11
    public function setLayouts(array $usedLayouts = []): static
33
    {
34 11
        $this->layouts = [];
35
36 11
        foreach ($usedLayouts as $key => $layout) {
37 11
            if (is_a($layout, Layout::class, true)) {
38 11
                if (is_string($layout)) {
39 11
                    $layout = new $layout;
40
                }
41 11
                $this->layouts[(is_numeric($key) || !$key) ? $layout->name() : $key] = $layout::class;
42
            }
43
        }
44
45 11
        return $this;
46
    }
47
48 11
    public function layouts(): array
49
    {
50 11
        return $this->layouts;
51
    }
52
53 9
    public function handle(Flexible $field): static
54
    {
55 9
        foreach ($this->layouts() as $layout) {
56 9
            $field->useLayout($layout);
57
        }
58
59 9
        return $this;
60
    }
61
}
62