Passed
Pull Request — master (#16)
by
unknown
03:36
created

WebComponentsHelper::is()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 6
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
26
    /**
27
     * Pass properties to an HTMLElement using attributes with JSON values.
28
     *
29
     * @param array $properties A list of properties to set.
30
     * @return string An attributes string list like `attr1="value1" attr2="value2"`.
31
     */
32
    public function props(array $properties): string
33
    {
34
        if (empty($properties)) {
35
            return '';
36
        }
37
        $attributes = [];
38
        foreach ($properties as $key => $value) {
39
            $attributes[] = sprintf('%s="%s"', $key, htmlspecialchars(json_encode($value), ENT_QUOTES, 'UTF-8'));
40
        }
41
42
        return join(' ', $attributes);
43
    }
44
45
    /**
46
     * Initialize a Custom Element which extends a native node.
47
     *
48
     * @param string $tagName The defined Custom Element name to set as `is` attribute.
49
     * @param array $properties A list of properties to set.
50
     * @param string $scriptPath The path of the definition script to import.
51
     * @return string An attributes string list like `is="my-element" attr1="value1" attr2="value2"`.
52
     */
53
    public function is(string $tagName, array $properties = array(), string $scriptPath = ''): string
54
    {
55
        if (!empty($scriptPath)) {
56
            $this->script($scriptPath, [ 'block' => 'scriptsComponents' ]);
57
        }
58
        return sprintf('is="%s" %s', $tagName, $this->props($properties));
59
    }
60
61
    /**
62
     * Initialize a Custom Element.
63
     *
64
     * @param string $tagName The defined Custom Element name to use as tag name.
65
     * @param array $properties A list of properties to set.
66
     * @param string $scriptPath The path of the definition script to import.
67
     * @return string An HTML node string like `<my-element attr1="value1"></my-element>`.
68
     */
69
    public function element(string $tagName, array $properties = array(), $scriptPath = ''): string
70
    {
71
        if (!empty($scriptPath)) {
72
            $this->script($scriptPath, [ 'block' => 'scriptsComponents' ]);
73
        }
74
        return sprintf('<%s %s></%s>', $tagName, $this->props($properties), $tagName);
75
    }
76
}
77