Passed
Push — master ( 3103ec...c79316 )
by Bruno
10:28 queued 33s
created

renderElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 50
rs 9.456
1
<?php declare(strict_types=1);
2
3
require(__DIR__ . '/../vendor/autoload.php');
4
5
use Formularium\Factory\DatatypeFactory;
6
use Formularium\Element;
7
use Formularium\Exception\ClassNotFoundException;
8
use Formularium\Formularium;
9
use Formularium\FrameworkComposer;
10
use Formularium\Frontend\HTML\Element\Button;
11
use Formularium\Frontend\HTML\Element\Card;
12
use Formularium\Frontend\HTML\Element\Pagination;
13
use Formularium\Frontend\HTML\Element\Table;
14
use Formularium\Frontend\HTML\Renderable\Renderable_choice;
15
use Formularium\Frontend\HTML\Renderable\Renderable_number;
16
use Formularium\Model;
17
use Formularium\Renderable;
18
use Formularium\Validator\MaxLength;
19
use Formularium\Validator\Min;
20
use Formularium\Validator\MinLength;
21
22
function renderElements(FrameworkComposer $framework): string
23
{
24
    $upload = $framework->element(
25
        'Upload',
26
        [
27
        ]
28
    );
29
    $submitButton = $framework->element(
30
        'Button',
31
        [
32
            Element::LABEL => 'Save',
33
            Button::COLOR => Button::COLOR_PRIMARY
34
        ]
35
    );
36
    $table = $framework->element(
37
        'Table',
38
        [
39
            Table::ROW_NAMES => ['First', 'Second', 'Third'],
40
            Table::ROW_DATA => [ ['a', 'b', 'c'], [ 'd', 'e', 'f'] ],
41
            Table::STRIPED => true
42
        ]
43
    );
44
    $pagination = $framework->element(
45
        'Pagination',
46
        [
47
            Pagination::CURRENT => 21,
48
            Pagination::CURRENT_PAGE => 2, // should have only CURRENT or CURRENT_PAGE, but depends on framework
49
            Pagination::TOTAL_ITEMS => 253,
50
        ]
51
    );
52
    $card = $framework->element(
53
        'Card',
54
        [
55
            Card::TITLE => 'Card title',
56
            Card::IMAGE => 'https://via.placeholder.com/150',
57
            Card::CONTENT => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
58
        ]
59
    );
60
    $spinner = $framework->element(
61
        'Spinner',
62
        [
63
        ]
64
    );
65
66
    return '<h3 class="kitchen">Upload</h3>' . $upload . "\n" .
67
        '<h3 class="kitchen">Button</h3>' . $submitButton . "\n" .
68
        '<h3 class="kitchen">Table</h3>' .  $table . "\n" .
69
        '<h3 class="kitchen">Pagination</h3>' .  $pagination . "\n" .
70
        '<h3 class="kitchen">Card</h3><div style="width: 180px">' .  $card . "</div>\n" .
71
        '<h3 class="kitchen">Spinner</h3><div>'. $spinner . "</div>\n";
72
}
73
74
function kitchenSink($frameworkName, string $templateName)
75
{
76
    $framework = FrameworkComposer::create($frameworkName);
77
    $head = $framework->htmlHead();
78
    $footer = $framework->htmlFooter();
79
80
    /*
81
     * kitchen sink fields
82
     */
83
    $fields = [];
84
    $datatypes = Formularium::getDatatypeNames();
85
86
    // make a default for all types
87
    foreach ($datatypes as $d) {
88
        try {
89
            DatatypeFactory::factory($d);
90
            $fields[$d] = [
91
                'datatype' => $d,
92
                'renderable' => [
93
                    Renderable::LABEL => 'Type ' . $d
94
                ],
95
            ];
96
        } catch (ClassNotFoundException $e) {
97
            // Abstract class
98
        }
99
    }
100
101
    /*
102
     * basic demo fiels
103
     */
104
    $basicFields = [
105
        'myString' => [
106
            'datatype' => 'string',
107
            'validators' => [
108
                MinLength::class => [ 'value' => 3],
109
                MaxLength::class => [ 'value' => 30],
110
            ],
111
            'renderable' => [
112
                Renderable::LABEL => 'Type string',
113
                Renderable::COMMENT => 'Some text explaining this field',
114
                Renderable::PLACEHOLDER => "Type here",
115
                Renderable::SIZE => Renderable::SIZE_LARGE,
116
                Renderable::ICON_PACK => 'fas',
117
                Renderable::ICON => 'fa-check'
118
            ],
119
        ],
120
        'myInteger' => [
121
            'datatype' => 'integer',
122
            'validators' => [
123
                Min::class => [ 'value' => 4],
124
                Max::class => [ 'value' => 40],
125
            ],
126
            'renderable' => [
127
                Renderable_number::STEP => 2,
128
                Renderable::LABEL => 'Type integer',
129
                Renderable::PLACEHOLDER => "Type here"
130
            ],
131
        ],
132
        'countrycodeselect' => [
133
            'datatype' => 'countrycodeISO3',
134
            'renderable' => [
135
                Renderable_choice::FORMAT_CHOOSER => Renderable_choice::FORMAT_CHOOSER_SELECT,
136
                Renderable::LABEL => 'Country code - select choice'
137
            ],
138
        ],
139
    ];
140
141
    // generate basic model
142
    $basicModel = Model::fromStruct(
143
        [
144
            'name' => 'BasicModel',
145
            'fields' => $basicFields
146
        ]
147
    );
148
    $basicDemoEditable = $basicModel->editable($framework, []);
149
150
    // generate kitchen sink model
151
    $model = Model::fromStruct(
152
        [
153
            'name' => 'TestModel',
154
            'fields' => $fields
155
        ]
156
    );
157
    $randomData = [];
158
    foreach ($model->getFields() as $f) {
159
        if ($f->getDatatype()->getBasetype() !== 'constant') {
160
            $randomData[$f->getName()] = $f->getDatatype()->getRandom();
161
        }
162
    }
163
    $modelViewable = $model->viewable($framework, $randomData);
164
    $modelEditable = $model->editable($framework);
165
166
    $title = join('/', $frameworkName);
167
    $template = file_get_contents(__DIR__ . '/kitchentemplates/' . $templateName);
168
169
    $template = strtr(
170
        $template,
171
        [
172
            '{{title}}' => $title,
173
            '{{head}}' => $head,
174
            '{{basicDemoEditable}}' => $basicDemoEditable,
175
            '{{elements}}' => renderElements($framework),
176
            '{{modelViewable}}' => $modelViewable,
177
            '{{modelEditable}}' => $modelEditable,
178
            '{{footer}}' => $footer,
179
        ]
180
    );
181
    return $template;
182
}
183
184
@mkdir(__DIR__ . '/../docs/');
185
@mkdir(__DIR__ . '/../docs/kitchensink/');
186
$path = __DIR__ . '/../Formularium/Frontend/';
187
$dir = scandir($path);
188
if ($dir === false) {
189
    echo 'Cannot find frontend';
190
    return 1;
191
}
192
$frameworks = array_diff($dir, array('.', '..'));
193
$index = <<<EOF
194
<!DOCTYPE html>
195
<html>
196
<head>
197
<head>
198
    <meta charset="UTF-8">
199
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
200
    <meta name="viewport" content="width=device-width, initial-scale=1">
201
202
    <title>Formularium Kitchen Sink</title>
203
    <meta property="og:title" content="Formularium">
204
    <meta property="og:locale" content="en_US">
205
    <meta name="description" content="Form validation and generation for PHP with custom frontend generators">
206
    <meta property="og:description" content="Form validation and generation for PHP with custom frontend generators">
207
    <link rel="canonical" href="https://corollarium.github.io/Formularium/">
208
    <meta property="og:url" content="https://corollarium.github.io/Formularium/">
209
    <meta property="og:site_name" content="Formularium">
210
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
211
</head>
212
<body>
213
<div class="container">
214
<h1>Formularium Kitchen Sink</h1>
215
<ul>
216
EOF;
217
$frameworks = [
218
    ['framework' => ['HTML', 'Quill'], 'template' => 'base.html'],
219
    ['framework' => ['HTML', 'Bulma', 'Quill'], 'template' => 'bulma.html'],
220
    ['framework' => ['HTML', 'Bootstrap', 'Quill'], 'template' => 'base.html'],
221
    ['framework' => ['HTML', 'Bootstrap', 'Quill', 'Parsley'], 'template' => 'base.html'],
222
    ['framework' => ['HTML', 'Bootstrapvue', 'Vue'], 'template' => 'base.html'],
223
    ['framework' => ['HTML', 'Materialize'], 'template' => 'base.html'],
224
    ['framework' => ['HTML', 'Bulma', 'Quill', 'Vue'], 'template' => 'bulma.html'],
225
    ['framework' => ['HTML', 'Bootstrap', 'Vue'], 'template' => 'base.html'],
226
    ['framework' => ['HTML', 'Buefy', 'Vue'], 'template' => 'bulma.html'],
227
    ['framework' => ['HTML', 'React'], 'template' => 'base.html'],
228
    ['framework' => ['HTML', 'Bootstrap', 'React'], 'template' => 'base.html'],
229
];
230
foreach ($frameworks as $f) {
231
    $name = join('', $f['framework']);
232
    echo "Building $name...\n";
233
    $html = kitchenSink($f['framework'], $f['template']);
234
    file_put_contents(__DIR__ . '/../docs/kitchensink/' . $name . '.html', $html);
235
    $index .= "<li><a href='{$name}.html'>" . join('+', $f['framework']) . '</a></li>';
236
}
237
$index .= "</ul>
238
<footer>
239
    <a href='https://github.com/Corollarium/Formularium/'>Source code</a> and <a href='https://corollarium.github.io/Formularium/'>Documentation</a>
240
</footer>
241
</div>
242
</body></html>";
243
file_put_contents(__DIR__ . '/../docs/kitchensink/index.html', $index);
244