Test Failed
Push — master ( e97986...2933e0 )
by Bruno
19:41 queued 09:43
created

templatify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 27
rs 9.7
cc 1
nc 1
nop 4
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\Framework;
10
use Formularium\FrameworkComposer;
11
use Formularium\Frontend\HTML\Element\Button;
12
use Formularium\Frontend\HTML\Element\Card;
13
use Formularium\Frontend\HTML\Element\Pagination;
14
use Formularium\Frontend\HTML\Element\Table;
15
use Formularium\Frontend\HTML\Renderable\Renderable_enum;
16
use Formularium\Frontend\HTML\Renderable\Renderable_number;
17
use Formularium\Model;
18
use Formularium\Renderable;
19
use Formularium\Validator\Max;
20
use Formularium\Validator\MaxLength;
21
use Formularium\Validator\Min;
22
use Formularium\Validator\MinLength;
23
24
function templatify(FrameworkComposer $frameworkComposer, string $templateName, string $contents, string $title)
25
{
26
    $head = $frameworkComposer->htmlHead();
27
    $footer = $frameworkComposer->htmlFooter();
28
29
    $frameworkNames = join(
30
        '/',
31
        array_map(
32
            function (Framework $f) {
33
                return $f->getName();
34
            },
35
            $frameworkComposer->getFrameworks()
36
        )
37
    );
38
    $template = file_get_contents(__DIR__ . '/kitchentemplates/' . $templateName);
39
40
    $template = strtr(
41
        $template,
42
        [
43
            '{{frameworkNames}}' => $frameworkNames,
44
            '{{title}}' => $title,
45
            '{{head}}' => $head,
46
            '{{contents}}' => $contents,
47
            '{{footer}}' => $footer,
48
        ]
49
    );
50
    return $template;
51
}
52
53
function generateBase(array $frameworkNames, string $templateName)
54
{
55
    $frameworkComposer = FrameworkComposer::create($frameworkNames);
56
57
    /*
58
     * basic demo fiels
59
     */
60
    $basicFields = [
61
        'myString' => [
62
            'datatype' => 'string',
63
            'validators' => [
64
                MinLength::class => [ 'value' => 3],
65
                MaxLength::class => [ 'value' => 30],
66
            ],
67
            'renderable' => [
68
                Renderable::LABEL => 'Type string',
69
                Renderable::COMMENT => 'Some text explaining this field',
70
                Renderable::PLACEHOLDER => "Type here",
71
                Renderable::SIZE => Renderable::SIZE_LARGE,
72
                Renderable::ICON_PACK => 'fas',
73
                Renderable::ICON => 'fa-check'
74
            ],
75
        ],
76
        'myInteger' => [
77
            'datatype' => 'integer',
78
            'validators' => [
79
                Min::class => [ 'value' => 4],
80
                Max::class => [ 'value' => 40],
81
            ],
82
            'renderable' => [
83
                Renderable_number::STEP => 2,
84
                Renderable::LABEL => 'Type integer',
85
                Renderable::PLACEHOLDER => "Type here"
86
            ],
87
        ],
88
        'countrycodeselect' => [
89
            'datatype' => 'countrycodeiso3',
90
            'renderable' => [
91
                Renderable_enum::FORMAT_CHOOSER => Renderable_enum::FORMAT_CHOOSER_SELECT,
92
                Renderable::LABEL => 'Country code - select choice'
93
            ],
94
        ],
95
    ];
96
97
    // generate basic model
98
    $basicModel = Model::fromStruct(
99
        [
100
            'name' => 'BasicModel',
101
            'fields' => $basicFields
102
        ]
103
    );
104
    $basicDemoEditable = $basicModel->editable($frameworkComposer, []);
105
    return templatify($frameworkComposer, $templateName, $basicDemoEditable, 'Basic demo');
106
}
107
108
function generateElements(array $frameworkNames, string $templateName)
109
{
110
    $frameworkComposer = FrameworkComposer::create($frameworkNames);
111
    $upload = $frameworkComposer->element(
112
        'Upload',
113
        [
114
        ]
115
    );
116
    $submitButton = $frameworkComposer->element(
117
        'Button',
118
        [
119
            Element::LABEL => 'Save',
120
            Button::COLOR => Button::COLOR_PRIMARY
121
        ]
122
    );
123
    $table = $frameworkComposer->element(
124
        'Table',
125
        [
126
            Table::ROW_NAMES => ['First', 'Second', 'Third'],
127
            Table::ROW_DATA => [ ['a', 'b', 'c'], [ 'd', 'e', 'f'] ],
128
            Table::STRIPED => true
129
        ]
130
    );
131
    $pagination = $frameworkComposer->element(
132
        'Pagination',
133
        [
134
            Pagination::CURRENT => 21,
135
            Pagination::CURRENT_PAGE => 2, // should have only CURRENT or CURRENT_PAGE, but depends on framework
136
            Pagination::TOTAL_ITEMS => 253,
137
        ]
138
    );
139
    $card = $frameworkComposer->element(
140
        'Card',
141
        [
142
            Card::TITLE => 'Card title',
143
            Card::IMAGE => 'https://via.placeholder.com/150',
144
            Card::CONTENT => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
145
        ]
146
    );
147
    $spinner = $frameworkComposer->element(
148
        'Spinner',
149
        [
150
        ]
151
    );
152
153
    $contents = '<h3 class="kitchen">Upload</h3>' . $upload . "\n" .
154
        '<h3 class="kitchen">Button</h3>' . $submitButton . "\n" .
155
        '<h3 class="kitchen">Table</h3>' .  $table . "\n" .
156
        '<h3 class="kitchen">Pagination</h3>' .  $pagination . "\n" .
157
        '<h3 class="kitchen">Card</h3><div style="width: 180px">' .  $card . "</div>\n" .
158
        '<h3 class="kitchen">Spinner</h3><div>'. $spinner . "</div>\n";
159
160
    return templatify($frameworkComposer, $templateName, $contents, 'Elements');
161
}
162
163
function kitchenSink(array $frameworks, string $templateName)
164
{
165
    $frameworkComposer = FrameworkComposer::create($frameworks);
166
167
    /*
168
     * kitchen sink fields
169
     */
170
    $fields = [];
171
    $datatypes = DatatypeFactory::getNames();
172
173
    // make a default for all types
174
    foreach ($datatypes as $d) {
175
        try {
176
            DatatypeFactory::factory($d);
177
            $fields[$d] = [
178
                'datatype' => $d,
179
                'renderable' => [
180
                    Renderable::LABEL => 'Type ' . $d
181
                ],
182
            ];
183
        } catch (ClassNotFoundException $e) {
184
            // Abstract class
185
        }
186
    }
187
188
    // generate kitchen sink model
189
    $model = Model::fromStruct(
190
        [
191
            'name' => 'TestModel',
192
            'fields' => $fields
193
        ]
194
    );
195
    $randomData = [];
196
    foreach ($model->getFields() as $f) {
197
        if ($f->getDatatype()->getBasetype() !== 'constant') {
198
            $randomData[$f->getName()] = $f->getDatatype()->getRandom();
199
        }
200
    }
201
    $modelViewable = $model->viewable($frameworkComposer, $randomData);
0 ignored issues
show
Unused Code introduced by
The assignment to $modelViewable is dead and can be removed.
Loading history...
202
    $modelEditable = $model->editable($frameworkComposer);
203
204
    return templatify($frameworkComposer, $templateName, $modelEditable, 'Model editable');
205
}
206
207
function main()
208
{
209
    @mkdir(__DIR__ . '/../docs/');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

209
    /** @scrutinizer ignore-unhandled */ @mkdir(__DIR__ . '/../docs/');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
210
    @mkdir(__DIR__ . '/../docs/kitchensink/');
211
    $path = __DIR__ . '/../Formularium/Frontend/';
212
    $dir = scandir($path);
213
    if ($dir === false) {
214
        echo 'Cannot find frontend';
215
        return 1;
216
    }
217
    $frameworks = array_diff($dir, array('.', '..'));
0 ignored issues
show
Unused Code introduced by
The assignment to $frameworks is dead and can be removed.
Loading history...
218
    $index = <<<EOF
219
<!DOCTYPE html>
220
<html>
221
<head>
222
<head>
223
    <meta charset="UTF-8">
224
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
225
    <meta name="viewport" content="width=device-width, initial-scale=1">
226
227
    <title>Formularium Kitchen Sink</title>
228
    <meta property="og:title" content="Formularium">
229
    <meta property="og:locale" content="en_US">
230
    <meta name="description" content="Form validation and generation for PHP with custom frontend generators">
231
    <meta property="og:description" content="Form validation and generation for PHP with custom frontend generators">
232
    <link rel="canonical" href="https://corollarium.github.io/Formularium/">
233
    <meta property="og:url" content="https://corollarium.github.io/Formularium/">
234
    <meta property="og:site_name" content="Formularium">
235
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
236
</head>
237
<body>
238
<div class="container">
239
<h1>Formularium Examples</h1>
240
<p>These files are all automatically generated from the same data models, using different frameworks.</p>
241
242
<div>
243
EOF;
244
    $frameworks = [
245
        ['framework' => ['HTML', 'HTMLValidation', 'Quill'], 'template' => 'base'],
246
        ['framework' => ['HTML', 'HTMLValidation', 'Bulma', 'Quill'], 'template' => 'bulma'],
247
        ['framework' => ['HTML', 'HTMLValidation', 'Bootstrap', 'Quill'], 'template' => 'base'],
248
        ['framework' => ['HTML', 'HTMLValidation', 'Bootstrap', 'Quill', 'Parsley'], 'template' => 'base'],
249
        ['framework' => ['HTML', 'HTMLValidation', 'Bootstrap', 'Vue', 'Vuelidate'], 'template' => 'base'],
250
        ['framework' => ['HTML', 'HTMLValidation', 'Bootstrapvue', 'Vue'], 'template' => 'base'],
251
        ['framework' => ['HTML', 'HTMLValidation', 'Materialize'], 'template' => 'base'],
252
        ['framework' => ['HTML', 'HTMLValidation', 'Bulma', 'Quill', 'Vue'], 'template' => 'bulma'],
253
        ['framework' => ['HTML', 'HTMLValidation', 'Bootstrap', 'Vue'], 'template' => 'base'],
254
        ['framework' => ['HTML', 'HTMLValidation', 'Buefy', 'Vue'], 'template' => 'bulma'],
255
        ['framework' => ['HTML', 'HTMLValidation', 'React'], 'template' => 'base'],
256
        ['framework' => ['HTML', 'HTMLValidation', 'Bootstrap', 'React'], 'template' => 'base'],
257
        ['framework' => ['HTML', 'HTMLValidation', 'Vuetify', 'Vue'], 'template' => 'base'],
258
    ];
259
    foreach ($frameworks as $f) {
260
        $name = join('', $f['framework']);
261
        $prettyName = join(' + ', array_slice($f['framework'], 2));
262
        echo "Building $name...\n";
263
        
264
        $index .= "<h2>$prettyName</h2><ul>";
265
266
        $html = generateBase($f['framework'], $f['template'] . '_demo.html');
267
        $filename = __DIR__ . '/../docs/kitchensink/demo_' . $name . '.html';
268
        file_put_contents($filename, $html);
269
        $index .= "<li><a href='{$filename}'>Basic example</a></li>";
270
271
        $html = generateElements($f['framework'], $f['template'] . '_demo.html');
272
        $filename = __DIR__ . '/../docs/kitchensink/elements_' . $name . '.html';
273
        file_put_contents($filename, $html);
274
        $index .= "<li><a href='{$filename}'>Elements</a></li>";
275
276
        $html = kitchenSink($f['framework'], $f['template'] . '_demo.html');
277
        $filename = __DIR__ . '/../docs/kitchensink/editable_' . $name . '.html';
278
        file_put_contents($filename, $html);
279
        $index .= "<li><a href='{$filename}'>Editable (all types)</a></li>";
280
281
        // $html = kitchenSink($f['framework'], $f['template']);
282
        // file_put_contents(__DIR__ . '/../docs/kitchensink/' . $name . '.html', $html);
283
284
        $index .= "</ul>";
285
    }
286
    $index .= "</div>
287
<footer>
288
    <a href='https://github.com/Corollarium/Formularium/'>Source code</a> and <a href='https://corollarium.github.io/Formularium/'>Documentation</a>
289
</footer>
290
</div>
291
</body></html>";
292
    file_put_contents(__DIR__ . '/../docs/kitchensink/index.html', $index);
293
}
294
295
main();
296