Passed
Branch main (0138e9)
by Sammy
06:29
created

Element::isValidValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
nc 3
nop 1
dl 0
loc 3
rs 10
c 1
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
    protected $tag = '';
138
    protected $attributes = [];
139
    protected $content = '';
140
141
    public function __construct($tag, $content = null, $attributes = [])
142
    {
143
        $this->tag = $tag;
144
        $this->attributes = $attributes;
145
        $this->content = is_string($content) ? $content : '';
146
    }
147
148
    //::span('inner text', $attributes)
149
    public static function __callStatic($element_type, $arguments)
150
    {
151
        $i = 0;
152
        // first argument is the inner text
153
        $element_inner = $arguments[$i++] ?? null;
154
        // second argument, an array for HTML attributes
155
        $attributes = $arguments[$i++] ?? [];
156
157
        return (new Element($element_type, $element_inner, $attributes))->__toString();
158
    }
159
160
    public function __toString()
161
    {
162
        $ret = '';
163
        if ($this->isVoid()) {
164
            $ret = sprintf(
165
                '<%s%s/>',
166
                $this->tag,
167
                $this->attributesToString()
168
            );
169
        } else {
170
            $ret = sprintf(
171
                '<%s%s>%s</%s>',
172
                $this->tag,
173
                $this->attributesToString(),
174
                $this->content,
175
                $this->tag
176
            );
177
        }
178
        return $ret;
179
    }
180
181
    public function isVoid()
182
    {
183
        return in_array($this->tag, self::VOID_ELEMENTS);
184
    }
185
186
187
    private function isBooleanAttribute($k)
188
    {
189
        return is_int($k);
190
    }
191
192
    private function isValidValue($v)
193
    {
194
        return !(is_null($v) && $v === '' && is_array($v));
195
    }
196
197
    private function attributesToString()
198
    {
199
        $ret = '';
200
        foreach ($this->attributes as $k => $v) {
201
            if ($this->isValidValue($v)) {
202
                $ret .=  ' ' . ($this->isBooleanAttribute($k) ? $v : sprintf('%s="%s"', $k, $v));
203
            }
204
        }
205
        return $ret;
206
    }
207
}
208