Passed
Push — main ( 5c15a6...ae0e18 )
by Sammy
01:48
created

Element::attributesToString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * show off @method
5
 *
6
 * @method string abbr(string $content, array $attr) creates an abbreviation or an acronym
7
 * @method string acronym(string $content, array $attr) creates an acronym (not supported in HTML5. use abbr() instead)
8
 * @method string address(string $content, array $attr) creates contact information for the author/owner of a document
9
 * @method string applet(string $content, array $attr) creates an embedded applet (not supported in HTML5. use embed() or <object> instead)
10
 * @method string area(string $content, array $attr) creates an area inside an image map
11
 * @method string article(string $content, array $attr) creates an article
12
 * @method string aside(string $content, array $attr) creates content aside from the page content
13
 * @method string audio(string $content, array $attr) creates embedded sound content
14
 * @method string b(string $content, array $attr) creates bold text
15
 * @method string base(string $content, array $attr) creates an element that specifies the base url/target for all relative urls in a document
16
 * @method string basefont(string $content, array $attr) creates an element that specifies a default color, size, and font for all text in a document (not supported in HTML5. use CSS instead)
17
 * @method string bdi(string $content, array $attr) creates an element that isolates a part of text that might be formatted in a different direction from other text outside it
18
 * @method string bdo(string $content, array $attr) creates an element that overrides the current text direction
19
 * @method string big(string $content, array $attr) creates an element that defines big text (not supported in HTML5. use CSS instead)
20
 * @method string blockquote(string $content, array $attr) creates a section that is quoted from another source
21
 * @method string body(string $content, array $attr) creates the document's body
22
 * @method string br(string $content, array $attr) creates a single line break
23
 * @method string button(string $content, array $attr) creates a clickable button
24
 * @method string canvas(string $content, array $attr) used to draw graphics, on the fly, via scripting (usually javascript)
25
 * @method string caption(string $content, array $attr) creates a table caption
26
 * @method string center(string $content, array $attr) defines centered text (not supported in HTML5. use CSS instead)
27
 * @method string cite(string $content, array $attr) creates the title of a work
28
 * @method string code(string $content, array $attr) creates a piece of computer code
29
 * @method string col(string $content, array $attr) specifies column properties for each column within a <colgroup> element
30
 * @method string colgroup(string $content, array $attr) specifies a group of one or more columns in a table for formatting
31
 * @method string data(string $content, array $attr) adds a machine-readable translation of a given content
32
 * @method string datalist(string $content, array $attr) specifies a list of pre-defined options for input controls
33
 * @method string dd(string $content, array $attr) creates a description/value of a term in a description list
34
 * @method string del(string $content, array $attr) creates text that has been deleted from a document
35
 * @method string details(string $content, array $attr) creates additional details that the user can view or hide
36
 * @method string dfn(string $content, array $attr) specifies a term that is going to be defined within the content
37
 * @method string dialog(string $content, array $attr) creates a dialog box or window
38
 * @method string dir(string $content, array $attr) defines a directory list (not supported in HTML5. use ul() instead)
39
 * @method string div(string $content, array $attr) creates a section in a document
40
 * @method string dl(string $content, array $attr) creates a description list
41
 * @method string dt(string $content, array $attr) creates a term/name in a description list
42
 * @method string em(string $content, array $attr) creates emphasized text
43
 * @method string embed(string $content, array $attr) creates a container for an external application
44
 * @method string fieldset(string $content, array $attr) groups related elements in a form
45
 * @method string figcaption(string $content, array $attr) creates a caption for a <figure> element
46
 * @method string figure(string $content, array $attr) specifies self-contained content
47
 * @method string font(string $content, array $attr) defines font, color, and size for text    (not supported in HTML5. use CSS instead)
48
 * @method string footer(string $content, array $attr) creates a footer for a document or section
49
 * @method string form(string $content, array $attr) creates an HTML form for user input
50
 * @method string frame(string $content, array $attr) defines a window (a frame) in a frameset (not supported in HTML5)
51
 * @method string frameset(string $content, array $attr) defines a set of frames   (not supported in HTML5)
52
 * @method string h1(string $content, array $attr) creates HTML headings 1
53
 * @method string h2(string $content, array $attr) creates HTML headings 2
54
 * @method string h3(string $content, array $attr) creates HTML headings 3
55
 * @method string h4(string $content, array $attr) creates HTML headings 4
56
 * @method string h5(string $content, array $attr) creates HTML headings 5
57
 * @method string h6(string $content, array $attr) creates HTML headings 6
58
 * @method string head(string $content, array $attr) contains metadata/information for the document
59
 * @method string header(string $content, array $attr) creates a header for a document or section
60
 * @method string hr(string $content, array $attr) creates a thematic change in the content
61
 * @method string html(string $content, array $attr) creates the root of an HTML document
62
 * @method string i(string $content, array $attr) creates a part of text in an alternate voice or mood
63
 * @method string iframe(string $content, array $attr) creates an inline frame
64
 * @method string input(string $content, array $attr) creates an input control
65
 * @method string ins(string $content, array $attr) creates a text that has been inserted into a document
66
 * @method string kbd(string $content, array $attr) creates keyboard input
67
 * @method string label(string $content, array $attr) creates a label for an <input> element
68
 * @method string legend(string $content, array $attr) creates a caption for a <fieldset> element
69
 * @method string li(string $content, array $attr) creates a list item
70
 * @method string link(string $content, array $attr) creates the relationship between a document and an external resource (most used to link to style sheets)
71
 * @method string main(string $content, array $attr) specifies the main content of a document
72
 * @method string map(string $content, array $attr) creates an image map
73
 * @method string mark(string $content, array $attr) creates marked/highlighted text
74
 * @method string meta(string $content, array $attr) creates metadata about an HTML document
75
 * @method string meter(string $content, array $attr) creates a scalar measurement within a known range (a gauge)
76
 * @method string nav(string $content, array $attr) creates navigation links
77
 * @method string noframes(string $content, array $attr) defines an alternate content for users that do not support frames (not supported in HTML5)
78
 * @method string noscript(string $content, array $attr) creates an alternate content for users that do not support client-side scripts
79
 * @method string object(string $content, array $attr) creates a container for an external application
80
 * @method string ol(string $content, array $attr) creates an ordered list
81
 * @method string optgroup(string $content, array $attr) creates a group of related options in a drop-down list
82
 * @method string option(string $content, array $attr) creates an option in a drop-down list
83
 * @method string output(string $content, array $attr) creates the result of a calculation
84
 * @method string p(string $content, array $attr) creates a paragraph
85
 * @method string param(string $content, array $attr) creates a parameter for an object
86
 * @method string picture(string $content, array $attr) creates a container for multiple image resources
87
 * @method string pre(string $content, array $attr) creates preformatted text
88
 * @method string progress(string $content, array $attr) represents the progress of a task
89
 * @method string q(string $content, array $attr) creates a short quotation
90
 * @method string rp(string $content, array $attr) creates what to show in browsers that do not support ruby annotations
91
 * @method string rt(string $content, array $attr) creates an explanation/pronunciation of characters (for east asian typography)
92
 * @method string ruby(string $content, array $attr) creates a ruby annotation (for east asian typography)
93
 * @method string s(string $content, array $attr) creates text that is no longer correct
94
 * @method string samp(string $content, array $attr) creates sample output from a computer program
95
 * @method string script(string $content, array $attr) creates a client-side script
96
 * @method string section(string $content, array $attr) creates a section in a document
97
 * @method string select(string $content, array $attr) creates a drop-down list
98
 * @method string small(string $content, array $attr) creates smaller text
99
 * @method string source(string $content, array $attr) creates multiple media resources for media elements (<video> and <audio>)
100
 * @method string span(string $content, array $attr) creates a section in a document
101
 * @method string strike(string $content, array $attr) defines strikethrough text  not supported in HTML5. use del() or s() instead.
102
 * @method string strong(string $content, array $attr) creates important text
103
 * @method string style(string $content, array $attr) creates style information for a document
104
 * @method string sub(string $content, array $attr) creates subscripted text
105
 * @method string summary(string $content, array $attr) creates a visible heading for a <details> element
106
 * @method string sup(string $content, array $attr) creates superscripted text
107
 * @method string svg(string $content, array $attr) creates a container for svg graphics
108
 * @method string table(string $content, array $attr) creates a table
109
 * @method string tbody(string $content, array $attr) groups the body content in a table
110
 * @method string td(string $content, array $attr) creates a cell in a table
111
 * @method string template(string $content, array $attr) creates a container for content that should be hidden when the page loads
112
 * @method string textarea(string $content, array $attr) creates a multiline input control (text area)
113
 * @method string tfoot(string $content, array $attr) groups the footer content in a table
114
 * @method string th(string $content, array $attr) creates a header cell in a table
115
 * @method string thead(string $content, array $attr) groups the header content in a table
116
 * @method string time(string $content, array $attr) creates a specific time (or datetime)
117
 * @method string title(string $content, array $attr) creates a title for the document
118
 * @method string tr(string $content, array $attr) creates a row in a table
119
 * @method string track(string $content, array $attr) creates text tracks for media elements (<video> and <audio>)
120
 * @method string tt(string $content, array $attr) creates an element that defines teletype text (not supported in HTML5. use CSS instead)
121
 * @method string u(string $content, array $attr) creates some text that is unarticulated and styled differently from normal text
122
 * @method string ul(string $content, array $attr) creates an unordered list
123
 * @method string var(string $content, array $attr) creates a variable
124
 * @method string video(string $content, array $attr) creates embedded video content
125
 * @method string wbr(string $content, array $attr) creates a possible line-break
126
 */
127
128
namespace HexMakina\Marker;
129
130
class Element
131
{
132
    public const VOID_ELEMENTS = [
133
      'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
134
      'link', 'meta', 'param', 'source', 'track', 'wbr'
135
    ];
136
137
    public const FORMAT_VOID = '<%s%s/>';
138
    public const FORMAT_ELEMENT = '<%s%s>%s</%s>';
139
    public const FORMAT_ATTRIBUTES = '%s="%s"';
140
141
142
    protected $tag = '';
143
    protected $attributes = [];
144
    protected $content = '';
145
146
    public function __construct($tag, $content = null, $attributes = [])
147
    {
148
        $this->tag = $tag;
149
        $this->attributes = $attributes;
150
        $this->content = is_string($content) ? $content : '';
151
    }
152
153
    //::span('inner text', $attributes)
154
    public static function __callStatic($element_type, $arguments)
155
    {
156
        $i = 0;
157
        // first argument is the inner text
158
        $element_inner = $arguments[$i++] ?? null;
159
        // second argument, an array for HTML attributes
160
        $attributes = $arguments[$i++] ?? [];
161
162
        return (new Element($element_type, $element_inner, $attributes))->__toString();
163
    }
164
165
    public function __toString()
166
    {
167
        $ret = '';
168
        if ($this->isVoid()) {
169
            $ret = sprintf(
170
                self::FORMAT_VOID,
171
                $this->tag,
172
                self::attributesAsString($this->attributes),
173
            );
174
        } else {
175
            $ret = sprintf(
176
                self::FORMAT_ELEMENT,
177
                $this->tag,
178
                self::attributesAsString($this->attributes),
179
                $this->content,
180
                $this->tag
181
            );
182
        }
183
        return $ret;
184
    }
185
186
    public function isVoid()
187
    {
188
        return in_array($this->tag, self::VOID_ELEMENTS);
189
    }
190
191
192
    private static function isBooleanAttribute($k)
193
    {
194
        return is_int($k);
195
    }
196
197
    private static function isValidValue($v)
198
    {
199
        return !(is_null($v) && $v === '' && is_array($v));
200
    }
201
202
    public static function attributesAsString($attributes = [])
203
    {
204
        $ret = '';
205
        foreach ($attributes as $k => $v) {
206
            if (self::isValidValue($v)) {
207
                $ret .=  ' ' . (self::isBooleanAttribute($k) ? $v : sprintf(self::FORMAT_ATTRIBUTES, $k, $v));
208
            }
209
        }
210
        return $ret;
211
    }
212
}
213