HtmlAttrHelper::checkOn()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Jaxon\App\View;
4
5
/**
6
 * HtmlAttrHelper.php
7
 *
8
 * Formatter for Jaxon custom HTML attributes.
9
 *
10
 * @package jaxon-core
11
 * @author Thierry Feuzeu
12
 * @copyright 2024 Thierry Feuzeu <[email protected]>
13
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
14
 * @link https://github.com/jaxon-php/jaxon-core
15
 */
16
17
use Jaxon\App\Component;
18
use Jaxon\App\Pagination;
19
use Jaxon\Di\ClassContainer;
20
use Jaxon\Script\JsExpr;
21
use Jaxon\Script\JxnCall;
22
23
use function count;
24
use function htmlentities;
25
use function is_a;
26
use function is_array;
27
use function is_string;
28
use function Jaxon\rq;
29
use function json_encode;
30
use function trim;
31
32
class HtmlAttrHelper
33
{
34
    /**
35
     * @var string
36
     */
37
    private $sPaginationComponent;
38
39
    /**
40
     * The constructor
41
     *
42
     * @param ClassContainer $cls
43
     */
44
    public function __construct(protected ClassContainer $cls)
45
    {
46
        $this->sPaginationComponent = rq(Pagination::class)->_class();
47
    }
48
49
    /**
50
     * Get the component HTML code
51
     *
52
     * @param JxnCall $xJsCall
53
     *
54
     * @return string
55
     */
56
    public function html(JxnCall $xJsCall): string
57
    {
58
        $sClassName = $xJsCall->_class();
59
        if(!$sClassName)
60
        {
61
            return '';
62
        }
63
64
        $xCallable = $this->cls->makeRegisteredObject($sClassName);
65
        return is_a($xCallable, Component::class) ? (string)$xCallable->html() : '';
66
    }
67
68
    /**
69
     * Attach a component to a DOM node
70
     *
71
     * @param JxnCall $xJsCall
72
     * @param string $item
73
     *
74
     * @return string
75
     */
76
    public function bind(JxnCall $xJsCall, string $item = ''): string
77
    {
78
        $item = trim($item);
79
        return 'jxn-bind="' . $xJsCall->_class() . (!$item ? '"' : '" jxn-item="' . $item . '"');
80
    }
81
82
    /**
83
     * Attach the pagination component to a DOM node
84
     *
85
     * @param JxnCall $xJsCall
86
     *
87
     * @return string
88
     */
89
    public function pagination(JxnCall $xJsCall): string
90
    {
91
        // The pagination is always rendered with the same Pagination component.
92
        return 'jxn-bind="' . $this->sPaginationComponent . '" jxn-item="' . $xJsCall->_class() . '"';
93
    }
94
95
    /**
96
     * Set a node as a target for event handler definitions
97
     *
98
     * @param string $name
99
     *
100
     * @return string
101
     */
102
    public function target(string $name = ''): string
103
    {
104
        return 'jxn-target="' . trim($name) . '"';
105
    }
106
107
    /**
108
     * @param array $on
109
     *
110
     * @return bool
111
     */
112
    private function checkOn(array $on)
113
    {
114
        // Only accept arrays of 2 entries.
115
        $count = count($on);
116
        if($count !== 2)
117
        {
118
            return false;
119
        }
120
121
        // Only accept arrays with int index from 0, and string value.
122
        for($i = 0; $i < $count; $i++)
123
        {
124
            if(!isset($on[$i]) || !is_string($on[$i]))
125
            {
126
                return false;
127
            }
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Get the event handler attributes
135
     *
136
     * @param string $select
137
     * @param string $event
138
     * @param string $attr
139
     * @param JsExpr $xJsExpr
140
     *
141
     * @return string
142
     */
143
    private function eventAttr(string $select, string $event, string $attr, JsExpr $xJsExpr): string
144
    {
145
        $sCall = htmlentities(json_encode($xJsExpr->jsonSerialize()));
146
147
        return "$attr=\"$event\" jxn-call=\"$sCall\"" .
148
            ($select !== '' ? "jxn-select=\"$select\" " : '');
149
    }
150
151
    /**
152
     * Set an event handler with the "on" keywork
153
     *
154
     * @param string|array $on
155
     * @param JsExpr $xJsExpr
156
     *
157
     * @return string
158
     */
159
    public function on(string|array $on, JsExpr $xJsExpr): string
160
    {
161
        $select = '';
162
        $event = $on;
163
        if(is_array($on))
0 ignored issues
show
introduced by
The condition is_array($on) is always true.
Loading history...
164
        {
165
            if(!$this->checkOn($on))
166
            {
167
                return '';
168
            }
169
170
            $select = $on[0];
171
            $event = $on[1];
172
        }
173
174
        return $this->eventAttr(trim($select), trim($event), 'jxn-on', $xJsExpr);
175
    }
176
177
    /**
178
     * Shortcut to set a click event handler
179
     *
180
     * @param JsExpr $xJsExpr
181
     *
182
     * @return string
183
     */
184
    public function click(JsExpr $xJsExpr): string
185
    {
186
        return $this->on('click', $xJsExpr);
187
    }
188
189
    /**
190
     * Set an event handler with the "event" keywork
191
     *
192
     * @param array $on
193
     * @param JsExpr $xJsExpr
194
     *
195
     * @return string
196
     */
197
    public function event(array $on, JsExpr $xJsExpr): string
198
    {
199
        if(!$this->checkOn($on))
200
        {
201
            return '';
202
        }
203
204
        return $this->eventAttr(trim($on[0]), trim($on[1]), 'jxn-event', $xJsExpr);
205
    }
206
}
207