Passed
Push — master ( a03165...951dea )
by Bruno
07:35
created

FrameworkComposer::editableNodes()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 16
c 0
b 0
f 0
nc 7
nop 3
dl 0
loc 23
ccs 0
cts 0
cp 0
crap 72
rs 8.4444
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\HTMLNode;
7
8
class FrameworkComposer
9
{
10
    /**
11
     * @var Framework[]
12
     */
13
    protected $frameworks = [];
14
15
    /**
16
     * @param Framework[] $frameworks
17
     */
18 5
    public function __construct(array $frameworks = [])
19
    {
20 5
        $this->setFrameworks($frameworks);
21 5
    }
22
23
    /**
24
     * @param Framework[] $frameworks
25
     */
26 4
    public static function create(array $frameworks = []): FrameworkComposer
27
    {
28 4
        return new self($frameworks);
29
    }
30
31
    /**
32
     *
33
     * @return Framework[]
34
     */
35 4
    public function getFrameworks(): array
36
    {
37 4
        return $this->frameworks;
38
    }
39
40
    /**
41
     *
42
     * @return Framework|null
43
     */
44 2
    public function getByName(string $name): ?Framework
45
    {
46 2
        foreach ($this->frameworks as $f) {
47 2
            if ($f->getName() === $name) {
48 2
                return $f;
49
            }
50
        }
51 1
        return null;
52
    }
53
54
    /**
55
     * @param Framework[] $frameworks
56
     * @return void
57
     */
58 5
    public function setFrameworks(array $frameworks = [])
59
    {
60 5
        $this->frameworks = [];
61 5
        foreach ($frameworks as $f) {
62 5
            $this->append($f);
63
        }
64 5
    }
65
66
    /**
67
     * Appends a framework to the queue
68
     *
69
     * @param string|Framework $framework
70
     * @return void
71
     */
72 5
    public function append($framework)
73
    {
74 5
        $this->frameworks[] = ($framework instanceof Framework ? $framework : Framework::factory($framework));
75 4
    }
76
77
    /**
78
     * Returns the html <head> contents for all frameworks.
79
     *
80
     * @return string
81
     */
82 1
    public function htmlHead(): string
83
    {
84 1
        $head = new HTMLNode('');
85 1
        foreach ($this->getFrameworks() as $framework) {
86 1
            $framework->htmlHead($head);
87
        }
88 1
        return $head->getRenderHTML();
89
    }
90
91 1
    public function htmlFooter(): string
92
    {
93 1
        $footer = new HTMLNode('');
94 1
        foreach ($this->getFrameworks() as $framework) {
95 1
            $framework->htmlFooter($footer);
96
        }
97 1
        return $footer->getRenderHTML();
98
    }
99
100
    /**
101
     * Renders an element
102
     *
103
     * @param string $elementName
104
     * @param array $parameters
105
     * @return HTMLNode The element HTMLNode
106
     */
107 1
    public function nodeElement(string $elementName, array $parameters = []): HTMLNode
108
    {
109 1
        $node = new HTMLNode('');
110 1
        foreach ($this->getFrameworks() as $framework) {
111
            try {
112 1
                $element = $framework->getElement($elementName);
113 1
                $node = $element->render($parameters, $node);
114
            } catch (ClassNotFoundException $e) {
115
                continue; // element default
116
            }
117
        }
118 1
        return $node;
119
    }
120
121
    /**
122
     * Renders an element to a string
123
     *
124
     * @param string $elementName
125
     * @param array $parameters
126
     * @return string The rendered HTML
127 1
     */
128
    public function element(string $elementName, array $parameters = []): string
129 1
    {
130 1
        return $this->nodeElement($elementName, $parameters)->getRenderHTML();
131 1
    }
132 1
133 1
    /**
134
     * Renders a Model with the loaded frameworks.
135 1
     *
136 1
     * @param Model $m
137 1
     * @param array $modelData Actual data for the fields to render. Can be empty.
138
     * @param string[]|callable $restrictFields If present, restrict rendered fields. Can either
139
     * be an array of strings (field names) or a callback which is called for each field.
140
     * Callable signature: (Field $field, Model $m, array $modelData): boolean
141
     * @return HTMLNode[]
142 1
     */
143
    public function viewableNodes(Model $m, array $modelData, $restrictFields = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $restrictFields is not used and could be removed. ( Ignorable by Annotation )

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

143
    public function viewableNodes(Model $m, array $modelData, /** @scrutinizer ignore-unused */ $restrictFields = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144 1
    {
145 1
        $elements = [];
146 1
        foreach ($m->getFields() as $field) {
147
            $value = $modelData[$field->getName()] ?? $field->getDataType()->getDefault(); // TODO: values?
148 1
            $html = new HTMLNode('');
149
            foreach ($this->getFrameworks() as $framework) {
150
                try {
151
                    $r = $framework->getRenderable($field->getDatatype());
152
                    $x = $r->viewable($value, $field, $html);
153
                    $html = $x;
154
                } catch (ClassNotFoundException $e) {
155
                    continue; // renderable default
156
                }
157
            }
158
            $elements[$field->getName()] = $html;
159
        }
160
        return $elements;
161
    }
162
163
    /**
164
     * Renders a Model with the loaded frameworks.
165
     *
166
     * @param Model $m
167
     * @param array $modelData Actual data for the fields to render. Can be empty.
168
     * @param string[]|callable $restrictFields If present, restrict rendered fields. Can either
169
     * be an array of strings (field names) or a callback which is called for each field.
170
     * Callable signature: (Field $field, Model $m, array $modelData): boolean
171
     * @return string
172
     */
173
    public function viewable(Model $m, array $modelData, $restrictFields = null): string
174
    {
175
        $elements = $this->viewableNodes($m, $modelData, $restrictFields);
176
        $output = '';
177
        foreach ($this->getFrameworks() as $framework) {
178
            $output = $framework->viewableCompose($m, $elements, $output);
179
        }
180
        return $output;
181
    }
182
183
    /**
184
     * Renders a Model as an editable form with the loaded frameworks.
185
     *
186
     * @param Model $m
187
     * @param array $modelData Actual data for the fields to render. Can be empty.
188
     * @param string[]|callable $restrictFields If present, restrict rendered fields. Can either
189
     * be an array of strings (field names) or a callback which is called for each field.
190
     * Callable signature: (Field $field, Model $m, array $modelData): boolean
191
     * @return HTMLNode[]
192
     */
193
    public function editableNodes(Model $m, array $modelData, $restrictFields = null): array
194
    {
195
        $elements = [];
196
        foreach ($m->getFields() as $field) {
197
            if (is_array($restrictFields) && !in_array($field->getName(), $restrictFields)) {
198
                continue;
199
            } elseif (is_callable($restrictFields) && !$restrictFields($field, $m, $modelData)) {
200
                continue;
201
            }
202
203
            $value = $modelData[$field->getName()] ?? $field->getDataType()->getDefault(); // TODO: values?
204
            $html = new HTMLNode('');
205
            foreach ($this->getFrameworks() as $framework) {
206
                try {
207
                    $r = $framework->getRenderable($field->getDatatype());
208
                    $html = $r->editable($value, $field, $html);
209
                } catch (ClassNotFoundException $e) {
210
                    continue; // renderable default
211
                }
212
            }
213
            $elements[$field->getName()] = $html;
214
        }
215
        return $elements;
216
    }
217
218
    /**
219
     * Renders a Model as an editable form with the loaded frameworks.
220
     *
221
     * @param Model $m
222
     * @param array $modelData Actual data for the fields to render. Can be empty.
223
     * @param string[]|callable $restrictFields If present, restrict rendered fields. Can either
224
     * be an array of strings (field names) or a callback which is called for each field.
225
     * Callable signature: (Field $field, Model $m, array $modelData): boolean
226
     * @return string
227
     */
228
    public function editable(Model $m, array $modelData, $restrictFields = null): string
229
    {
230
        $elements = $this->editableNodes($m, $modelData, $restrictFields);
231
        $output = '';
232
        foreach ($this->getFrameworks() as $framework) {
233
            $output = $framework->editableCompose($m, $elements, $output);
234
        }
235
        return $output;
236
    }
237
}
238