Passed
Push — master ( 0b0ed1...849c84 )
by Bruno
02:30
created

FrameworkComposer::htmlFooter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Formularium;
4
5
use Formularium\HTMLElement;
6
7
class FrameworkComposer
8
{
9
    /**
10
     * @var Framework[]
11
     */
12
    protected static $frameworks = [];
13
14
    /**
15
     *
16
     * @return Framework[]
17
     */
18
    public static function get(): array
19
    {
20
        return static::$frameworks;
21
    }
22
23
    /**
24
     * @param Framework[] $frameworks
25
     */
26
    public static function set(array $frameworks = [])
27
    {
28
        static::$frameworks = [];
29
        foreach ($frameworks as $f) {
30
            static::append($f);
31
        }
32
        return __CLASS__;
33
    }
34
35
    /**
36
     * Appends a framework to the queue
37
     *
38
     * @param string|Framework $framework
39
     */
40
    public static function append($framework)
41
    {
42
        static::$frameworks[] = ($framework instanceof Framework ? $framework : Framework::factory($framework));
43
        return __CLASS__;
44
    }
45
46
    /**
47
     * Returns the html <head> contents for all frameworks.
48
     *
49
     * @return string
50
     */
51
    public static function htmlHead(): string
52
    {
53
        $head = [];
54
        foreach (static::get() as $framework) {
55
            $head[] = $framework->htmlHead();
56
        }
57
        return join("\n", $head);
58
    }
59
60
    public static function htmlFooter(): string
61
    {
62
        $head = [];
63
        foreach (static::get() as $framework) {
64
            $head[] = $framework->htmlFooter();
65
        }
66
        return join("\n", $head);
67
    }
68
69
    /**
70
     * Renders a Model with the loaded frameworks.
71
     *
72
     * @param Model $m
73
     * @return string
74
     */
75
    public static function viewable(Model $m): string
76
    {
77
        $elements = [];
78
        foreach ($m->getFields() as $field) {
79
            $values = '';  // TODO: values?
80
            $html = new HTMLElement('');
81
            foreach (static::get() as $framework) {
82
                $r = $framework->getRenderable($field->getDatatype());
83
                $x = $r->viewable($values, $field, $html);
84
                $html = $x;
85
            }
86
            $elements[$field->getName()] = $html;
87
        }
88
        $output = '';
89
        foreach (static::get() as $framework) {
90
            $output = $framework->viewableCompose($m, $elements, $output);
91
        }
92
        return $output;
93
    }
94
95
    public static function editable(Model $m): string
96
    {
97
        $elements = [];
98
        foreach ($m->getFields() as $field) {
99
            $values = '';  // TODO: values?
100
            $html = new HTMLElement('');
101
            foreach (static::get() as $framework) {
102
                $r = $framework->getRenderable($field->getDatatype());
103
                $html = $r->editable($values, $field, $html);
104
            }
105
            $elements[$field->getName()] = $html;
106
        }
107
        $output = '';
108
        foreach (static::get() as $framework) {
109
            $output = $framework->editableCompose($m, $elements, $output);
110
        }
111
        return $output;
112
    }
113
}
114