Passed
Push — master ( df6c29...dfe2a1 )
by Bruno
14:21
created

FrameworkComposer::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\HTMLElement;
7
8
class FrameworkComposer
9
{
10
    /**
11
     * @var Framework[]
12
     */
13
    protected $frameworks = [];
14
15
    /**
16
     * @param Framework[] $frameworks
17
     */
18
    public function __construct(array $frameworks = [])
19 1
    {
20
        $this->setFrameworks($frameworks);
21 1
    }
22
23
    /**
24
     * @param Framework[] $frameworks
25
     */
26
    public static function create(array $frameworks = []): FrameworkComposer
27
    {
28
        return new self($frameworks);
29
    }
30
31
    /**
32
     *
33
     * @return Framework[]
34
     */
35
    public function getFrameworks(): array
36
    {
37
        return $this->frameworks;
38
    }
39
40
    /**
41
     *
42 2
     * @return Framework
43
     */
44 2
    public function getByName(string $name): ?Framework
45 2
    {
46 2
        foreach ($this->frameworks as $f) {
47
            if ($f->getName() === $name) {
48 1
                return $f;
49
            }
50
        }
51
        return null;
52
    }
53
54
    /**
55
     * @param Framework[] $frameworks
56 2
     * @return void
57
     */
58 2
    public function setFrameworks(array $frameworks = [])
59 1
    {
60
        $this->frameworks = [];
61
        foreach ($frameworks as $f) {
62
            $this->append($f);
63
        }
64
    }
65
66
    /**
67
     * Appends a framework to the queue
68
     *
69
     * @param string|Framework $framework
70
     * @return void
71
     */
72
    public function append($framework)
73
    {
74
        $this->frameworks[] = ($framework instanceof Framework ? $framework : Framework::factory($framework));
75
    }
76
77
    /**
78
     * Returns the html <head> contents for all frameworks.
79
     *
80
     * @return string
81
     */
82
    public function htmlHead(): string
83
    {
84
        $head = new HTMLElement('');
85
        foreach ($this->get() as $framework) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on Formularium\FrameworkComposer. ( Ignorable by Annotation )

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

85
        foreach ($this->/** @scrutinizer ignore-call */ get() as $framework) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
            $framework->htmlHead($head);
87
        }
88
        return $head->getRenderHTML();
89
    }
90
91
    public function htmlFooter(): string
92
    {
93
        $footer = new HTMLElement('');
94
        foreach ($this->get() as $framework) {
95
            $framework->htmlFooter($footer);
96
        }
97
        return $footer->getRenderHTML();
98
    }
99
100
    /**
101
     * Renders a Model with the loaded frameworks.
102
     *
103
     * @param Model $m
104
     * @return string
105
     */
106
    public function viewable(Model $m, array $modelData): string
107
    {
108
        $elements = [];
109
        foreach ($m->getFields() as $field) {
110
            $value = $modelData[$field->getName()] ?? $field->getDataType()->getDefault(); // TODO: values?
111
            $html = new HTMLElement('');
112
            foreach ($this->get() as $framework) {
113
                try {
114
                    $r = $framework->getRenderable($field->getDatatype());
115
                    $x = $r->viewable($value, $field, $html);
116
                    $html = $x;
117
                } catch (ClassNotFoundException $e) {
118
                    continue; // renderable default
119
                }
120
            }
121
            $elements[$field->getName()] = $html;
122
        }
123
        $output = '';
124
        foreach ($this->get() as $framework) {
125
            $output = $framework->viewableCompose($m, $elements, $output);
126
        }
127
        return $output;
128
    }
129
130
    public function editable(Model $m, array $modelData): string
131
    {
132
        $elements = [];
133
        foreach ($m->getFields() as $field) {
134
            $value = $modelData[$field->getName()] ?? $field->getDataType()->getDefault(); // TODO: values?
135
            $html = new HTMLElement('');
136
            foreach ($this->get() as $framework) {
137
                try {
138
                    $r = $framework->getRenderable($field->getDatatype());
139
                    $html = $r->editable($value, $field, $html);
140
                } catch (ClassNotFoundException $e) {
141
                    continue; // renderable default
142
                }
143
            }
144
            $elements[$field->getName()] = $html;
145
        }
146
        $output = '';
147
        foreach ($this->get() as $framework) {
148
            $output = $framework->editableCompose($m, $elements, $output);
149
        }
150
        return $output;
151
    }
152
}
153