Completed
Pull Request — master (#16)
by
unknown
12:57
created

WebComponentsHelper::element()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 2
b 1
f 0
nc 2
nop 3
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2019 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\WebTools\View\Helper;
17
18
use Cake\View\Helper\HtmlHelper;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BEdita\WebTools\View\Helper\HtmlHelper. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
20
/**
21
 * Helper to handle Web Components initialization with properties.
22
 */
23
class WebComponentsHelper extends HtmlHelper
24
{
25
    private $ids = [];
26
27
    /**
28
     * Pass properties to an HTMLElement using attributes for plain values and inline scripts for array.
29
     *
30
     * @param array $properties A list of properties to set.
31
     * @return string An attributes string list like `data-wc="0" attr1="value"`.
32
     */
33
    public function props(array $properties): string
34
    {
35
        if (empty($properties)) {
36
            return '';
37
        }
38
39
        $id = count($this->ids);
40
        $this->ids[] = $id;
41
42
        $attributes = [];
43
        $statements = [];
44
        foreach ($properties as $key => $value) {
45
            if (is_string($value) || is_numeric($value)) {
46
                $attributes[] = sprintf('%s="%s"', $key, $value);
47
            } else {
48
                $statements[] = sprintf('elem[\'%s\'] = %s;', $key, json_encode($value));
49
            }
50
        }
51
52
        if (!empty($statements)) {
53
            $content = sprintf('(function(){var elem = document.querySelector(\'[data-wc="%s"]\');%s}());if(document.currentScript)document.currentScript.parentNode.removeChild(document.currentScript);', $id, join('', $statements));
54
            $this->scriptBlock($content, [ 'block' => 'scriptsComponents' ]);
55
        }
56
57
        return trim(sprintf('data-wc="%s" %s', $id, join(' ', $attributes)));
58
    }
59
60
    /**
61
     * Initialize a Custom Element which extends a native node.
62
     *
63
     * @param string $tagName The defined Custom Element name to set as `is` attribute.
64
     * @param array $properties A list of properties to set.
65
     * @param string $scriptPath The path of the definition script to import.
66
     * @return string An attributes string list like `is="my-element" data-wc="0"`.
67
     */
68
    public function is(string $tagName, array $properties = [], string $scriptPath = ''): string
69
    {
70
        if (!empty($scriptPath)) {
71
            $this->script($scriptPath, [ 'block' => 'scriptsComponents' ]);
72
        }
73
74
        return trim(sprintf('is="%s" %s', $tagName, $this->props($properties)));
75
    }
76
77
    /**
78
     * Initialize a Custom Element.
79
     *
80
     * @param string $tagName The defined Custom Element name to use as tag name.
81
     * @param array $properties A list of properties to set.
82
     * @param string $scriptPath The path of the definition script to import.
83
     * @return string An HTML node string like `<my-element data-wc="0"></my-element>`.
84
     */
85
    public function element(string $tagName, array $properties = [], $scriptPath = ''): string
86
    {
87
        if (!empty($scriptPath)) {
88
            $this->script($scriptPath, [ 'block' => 'scriptsComponents' ]);
89
        }
90
91
        return trim(sprintf('<%s %s></%s>', $tagName, $this->props($properties), $tagName));
92
    }
93
}
94