HexMakina /
Marker
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * Class WCAGElement |
||||
| 5 | * |
||||
| 6 | * This class extends the Element class and provides methods to enforce WCAG (Web Content Accessibility Guidelines) compliance for certain HTML elements. |
||||
| 7 | * |
||||
| 8 | * Methods: |
||||
| 9 | * |
||||
| 10 | * - img(string $src, string $alt, array $attributes = []): string |
||||
| 11 | * Enforces the 'alt' attribute for <img> tags. Throws an InvalidArgumentException if the 'alt' attribute is empty. |
||||
| 12 | * |
||||
| 13 | * - figure(string $content, string $caption, array $attributes = []): string |
||||
| 14 | * Enforces the presence of a <figcaption> inside a <figure>. Throws an InvalidArgumentException if the caption is empty. |
||||
| 15 | * |
||||
| 16 | * - button(string $content, array $attributes = []): string |
||||
| 17 | * Enforces that the button has content. Throws an InvalidArgumentException if the content is empty. |
||||
| 18 | * |
||||
| 19 | * - audio(string $src, array $attributes = []): string |
||||
| 20 | * Enforces the 'controls' attribute for <audio> tags. Throws an InvalidArgumentException if the 'src' attribute is empty. |
||||
| 21 | * |
||||
| 22 | * - video(string $src, array $attributes = []): string |
||||
| 23 | * Enforces the 'controls' attribute for <video> tags. Throws an InvalidArgumentException if the 'src' attribute is empty. |
||||
| 24 | * |
||||
| 25 | * - iframe(string $src, string $title, array $attributes = []): string |
||||
| 26 | * Enforces the 'title' attribute for <iframe> tags. Throws an InvalidArgumentException if the 'title' attribute is empty. |
||||
| 27 | * |
||||
| 28 | * - a(string $href, string $content, array $attributes = []): string |
||||
| 29 | * Enforces the 'href' attribute for <a> tags. Throws an InvalidArgumentException if the 'href' attribute is empty. |
||||
| 30 | * |
||||
| 31 | * - area(string $alt, array $attributes = []): string |
||||
| 32 | * Enforces the 'alt' attribute for <area> tags. Throws an InvalidArgumentException if the 'alt' attribute is empty. |
||||
| 33 | * |
||||
| 34 | * - input(string $type, string $name, array $attributes = []): string |
||||
| 35 | * Enforces the 'type' and 'name' attributes for <input> tags. Throws an InvalidArgumentException if the 'type' or 'name' attribute is empty. |
||||
| 36 | * |
||||
| 37 | * - select(string $name, array $options, array $attributes = []): string |
||||
| 38 | * Enforces the 'name' attribute for <select> tags. Throws an InvalidArgumentException if the 'name' attribute is empty. |
||||
| 39 | * |
||||
| 40 | * - textarea(string $name, array $attributes = []): string |
||||
| 41 | * Enforces the 'name' attribute for <textarea> tags. Throws an InvalidArgumentException if the 'name' attribute is empty. |
||||
| 42 | * |
||||
| 43 | * - label(string $for, string $content, array $attributes = []): string |
||||
| 44 | * Enforces the 'for' attribute for <label> tags. Throws an InvalidArgumentException if the 'for' attribute is empty. |
||||
| 45 | * |
||||
| 46 | * - html(string $content, array $attributes = []): string |
||||
| 47 | * Enforces content being passed to the <html> tag. Throws an InvalidArgumentException if the content is empty. |
||||
| 48 | * Enforces the 'lang' attribute for <html> tags. Throws an InvalidArgumentException if the 'lang' attribute is empty. |
||||
| 49 | * |
||||
| 50 | * - th(string $scope, string $content, array $attributes = []): string |
||||
| 51 | */ |
||||
| 52 | |||||
| 53 | namespace HexMakina\Marker; |
||||
| 54 | |||||
| 55 | class WCAGElement extends Element |
||||
| 56 | { |
||||
| 57 | /** |
||||
| 58 | * Enforce 'alt' attribute for <img> tags. |
||||
| 59 | * WCAG 2.1 Level A: 1.1.1 Non-text Content |
||||
| 60 | * - Describes the image for screen readers. |
||||
| 61 | * - Must be empty (alt="") if the image is decorative. |
||||
| 62 | * |
||||
| 63 | * @param string $src Image source |
||||
| 64 | * @param string $alt Alternative text (required, leave empty for decorative images |
||||
| 65 | * @param array $attributes Additional attributes |
||||
| 66 | * @return string |
||||
| 67 | */ |
||||
| 68 | public static function img(string $src, string $alt='', $attributes = []) |
||||
| 69 | { |
||||
| 70 | $attributes['src'] = $src; |
||||
| 71 | $attributes['alt'] = $alt; |
||||
| 72 | return parent::img(null, $attributes); |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Enforce <figcaption> inside <figure>. |
||||
| 77 | * |
||||
| 78 | * @param string $content Content inside the figure |
||||
| 79 | * @param string $caption Caption text (required) |
||||
| 80 | * @param array $attributes Additional attributes for figure |
||||
| 81 | * @return string |
||||
| 82 | */ |
||||
| 83 | public static function figure(string $content, string $caption, $attributes = []) |
||||
| 84 | { |
||||
| 85 | if (empty($caption)) { |
||||
| 86 | throw new \InvalidArgumentException("The <figcaption> is required inside <figure>."); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | $content .= parent::figcaption($caption); |
||||
|
0 ignored issues
–
show
The method
figcaption() does not exist on HexMakina\Marker\Element.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 90 | $attributes['role'] = 'figure'; |
||||
| 91 | return parent::figure($content, $attributes, function ($value) { |
||||
|
0 ignored issues
–
show
The method
figure() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 92 | return $value; |
||||
| 93 | }); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * General method to create a <button> tag with enforced content. |
||||
| 98 | * |
||||
| 99 | * @param string $content Button text/content (required) |
||||
| 100 | * @param array $attributes Additional attributes |
||||
| 101 | * @return string |
||||
| 102 | */ |
||||
| 103 | public static function button(string $content, $attributes = []) |
||||
| 104 | { |
||||
| 105 | if (empty($content)) { |
||||
| 106 | throw new \InvalidArgumentException("Button content is required."); |
||||
| 107 | |||||
| 108 | } |
||||
| 109 | $attributes['role'] ??= 'button'; |
||||
| 110 | return parent::button($content, $attributes); |
||||
|
0 ignored issues
–
show
The method
button() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 111 | } |
||||
| 112 | |||||
| 113 | /** |
||||
| 114 | * <audio> |
||||
| 115 | * Required Attribute: controls |
||||
| 116 | * Ensures users can control playback (pause, stop, etc.). |
||||
| 117 | * WCAG 2.1 Level A: 1.2.1 Audio-Only and Video-Only (Prerecorded) |
||||
| 118 | */ |
||||
| 119 | public static function audio(string $src, $attributes = []) |
||||
| 120 | { |
||||
| 121 | if(empty($src)) { |
||||
| 122 | throw new \InvalidArgumentException("Source attribute is required"); |
||||
| 123 | } |
||||
| 124 | |||||
| 125 | $attributes['src'] = $src; |
||||
| 126 | $attributes['controls'] ??= 'controls'; |
||||
| 127 | return parent::audio(null, $attributes); |
||||
|
0 ignored issues
–
show
The method
audio() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 128 | } |
||||
| 129 | |||||
| 130 | /** |
||||
| 131 | * <video> |
||||
| 132 | * Required Attribute: controls |
||||
| 133 | * Ensures users can control playback (pause, stop, etc.). |
||||
| 134 | * WCAG 2.1 Level A: 1.2.1 Audio-Only and Video-Only (Prerecorded) |
||||
| 135 | */ |
||||
| 136 | public static function video(string $src, $attributes = []) |
||||
| 137 | { |
||||
| 138 | if (empty($src)) { |
||||
| 139 | throw new \InvalidArgumentException("Source attribute is required"); |
||||
| 140 | } |
||||
| 141 | $attributes['src'] = $src; |
||||
| 142 | $attributes['controls'] ??= 'controls'; |
||||
| 143 | return parent::video(null, $attributes); |
||||
|
0 ignored issues
–
show
The method
video() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * <iframe> |
||||
| 148 | * Required Attribute: title |
||||
| 149 | * WCAG 2.1 Level A: 2.4.1 Bypass Blocks |
||||
| 150 | */ |
||||
| 151 | public static function iframe(string $src, string $title, $attributes = []) |
||||
| 152 | { |
||||
| 153 | if(empty($title)) { |
||||
| 154 | throw new \InvalidArgumentException("Iframe title is required"); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | $attributes['src'] = $src; |
||||
| 158 | $attributes['title'] = $title; |
||||
| 159 | return parent::iframe(null, $attributes); |
||||
|
0 ignored issues
–
show
The method
iframe() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 160 | } |
||||
| 161 | |||||
| 162 | /** |
||||
| 163 | * <a> |
||||
| 164 | * Required Attribute: href |
||||
| 165 | * WCAG 2.1 Level A: 2.4.4 Link Purpose (In Context) |
||||
| 166 | */ |
||||
| 167 | public static function a(string $href, string $content, $attributes = []) |
||||
| 168 | { |
||||
| 169 | if(empty($href)) { |
||||
| 170 | throw new \InvalidArgumentException("Anchor href is required"); |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | $attributes['href'] = $href; |
||||
| 174 | return parent::a($content, $attributes); |
||||
|
0 ignored issues
–
show
The method
a() does not exist on HexMakina\Marker\Element. Maybe you want to declare this class abstract?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 175 | } |
||||
| 176 | |||||
| 177 | /** |
||||
| 178 | * <area> |
||||
| 179 | * Required Attribute: alt |
||||
| 180 | * WCAG 2.1 Level A: 1.1.1 Non-text Content |
||||
| 181 | */ |
||||
| 182 | public static function area(string $alt, $attributes = []) |
||||
| 183 | { |
||||
| 184 | if(empty($alt)) { |
||||
| 185 | throw new \InvalidArgumentException("Area alt is required"); |
||||
| 186 | } |
||||
| 187 | $attributes['alt'] = $alt; |
||||
| 188 | return parent::area(null, $attributes); |
||||
|
0 ignored issues
–
show
The method
area() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 189 | } |
||||
| 190 | |||||
| 191 | /** |
||||
| 192 | * <input> |
||||
| 193 | * Required Attribute: type |
||||
| 194 | * WCAG 2.1 Level A: 4.1.2 Name, Role, Value |
||||
| 195 | */ |
||||
| 196 | public static function input(string $type, string $name, $attributes = []) |
||||
| 197 | { |
||||
| 198 | if(empty($type)) { |
||||
| 199 | throw new \InvalidArgumentException("Input type is required"); |
||||
| 200 | } |
||||
| 201 | |||||
| 202 | if(empty($name)) { |
||||
| 203 | throw new \InvalidArgumentException("Input name is required"); |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | $attributes['name'] = $name; |
||||
| 207 | $attributes['type'] = $type; |
||||
| 208 | |||||
| 209 | if(self::inputIsRequired($attributes)) { |
||||
| 210 | $attributes['aria-required'] = 'true'; |
||||
| 211 | } |
||||
| 212 | |||||
| 213 | return parent::input(null, $attributes); |
||||
|
0 ignored issues
–
show
The method
input() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 214 | } |
||||
| 215 | |||||
| 216 | private static function inputIsRequired($attributes): bool |
||||
| 217 | { |
||||
| 218 | return array_key_exists('required', $attributes) && $attributes['required'] === true |
||||
| 219 | || in_array('required', $attributes); |
||||
| 220 | } |
||||
| 221 | /** |
||||
| 222 | * <select> |
||||
| 223 | * Required Attribute: name |
||||
| 224 | * WCAG 2.1 Level A: 4.1.2 Name, Role, Value |
||||
| 225 | */ |
||||
| 226 | public static function select(string $name, array $options, $attributes = []) |
||||
| 227 | { |
||||
| 228 | if (empty($name)) { |
||||
| 229 | throw new \InvalidArgumentException("Select name is required"); |
||||
| 230 | } |
||||
| 231 | $attributes['name'] = $name; |
||||
| 232 | $string_options = ''; |
||||
| 233 | foreach($options as $value => $label) { |
||||
| 234 | $string_options.= parent::option($label, ['value' => $value]); |
||||
|
0 ignored issues
–
show
The method
option() does not exist on HexMakina\Marker\Element.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 235 | } |
||||
| 236 | return parent::select($string_options, $attributes, function ($value) { |
||||
|
0 ignored issues
–
show
The method
select() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 237 | return $value; |
||||
| 238 | }); |
||||
| 239 | } |
||||
| 240 | |||||
| 241 | /** |
||||
| 242 | * <textarea> |
||||
| 243 | * Required Attribute: name |
||||
| 244 | * WCAG 2.1 Level A: 4.1.2 Name, Role, Value |
||||
| 245 | */ |
||||
| 246 | public static function textarea(string $name, $attributes = []) |
||||
| 247 | { |
||||
| 248 | if (empty($name)) { |
||||
| 249 | throw new \InvalidArgumentException("Textarea name is required"); |
||||
| 250 | } |
||||
| 251 | $attributes['name'] = $name; |
||||
| 252 | |||||
| 253 | return parent::textarea(null, $attributes); |
||||
|
0 ignored issues
–
show
The method
textarea() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 254 | } |
||||
| 255 | |||||
| 256 | /** |
||||
| 257 | * <label> |
||||
| 258 | * Required Attribute: for |
||||
| 259 | * WCAG 2.1 Level A: 4.1.2 Name, Role, Value |
||||
| 260 | */ |
||||
| 261 | public static function label(string $for, string $content, $attributes = []) |
||||
| 262 | { |
||||
| 263 | $attributes['for'] = $for; |
||||
| 264 | return parent::label($content, $attributes); |
||||
|
0 ignored issues
–
show
The method
label() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 265 | } |
||||
| 266 | |||||
| 267 | /** |
||||
| 268 | * <html> |
||||
| 269 | * Required Attribute: lang |
||||
| 270 | * WCAG 2.1 Level A: 3.1.1 Language of Page |
||||
| 271 | */ |
||||
| 272 | public static function html(string $content, $attributes = []) |
||||
| 273 | { |
||||
| 274 | if(empty($content)) { |
||||
| 275 | throw new \InvalidArgumentException("HTML content is required"); |
||||
| 276 | } |
||||
| 277 | |||||
| 278 | if(empty($attributes['lang'])) { |
||||
| 279 | throw new \InvalidArgumentException("Language attribute is required"); |
||||
| 280 | } |
||||
| 281 | return parent::html($content, $attributes); |
||||
|
0 ignored issues
–
show
The method
html() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 282 | } |
||||
| 283 | |||||
| 284 | /** |
||||
| 285 | * <th> |
||||
| 286 | * Required Attribute: scope |
||||
| 287 | * WCAG 2.1 Level A: 1.3.1 Info and Relationships |
||||
| 288 | */ |
||||
| 289 | public static function th(string $scope, string $content, $attributes = []) |
||||
| 290 | { |
||||
| 291 | if(empty($scope)) { |
||||
| 292 | throw new \InvalidArgumentException("Scope attribute is required"); |
||||
| 293 | } |
||||
| 294 | |||||
| 295 | $attributes['scope'] = $scope; |
||||
| 296 | return parent::th($content, $attributes); |
||||
|
0 ignored issues
–
show
The method
th() does not exist on HexMakina\Marker\Element. It seems like you code against a sub-type of HexMakina\Marker\Element such as HexMakina\Marker\WCAGElement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 297 | } |
||||
| 298 | |||||
| 299 | } |