Passed
Push — main ( c68bbb...afde82 )
by Sammy
06:55
created

Marker::__callStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
  * HTML generator
5
  * Marker, hommage to Christian François Bouche-Villeneuve aka Chris Marker
6
  */
7
8
declare(strict_types=1);
9
10
namespace HexMakina\Marker;
11
12
class Marker extends Element
13
{
14
15
    /**
16
      * ? makes more sense to write
17
      * Marker::img('path/to/img.jpg', 'An alternative text', ['width' => 34, 'height' => 34])
18
      * than
19
      * Marker::img(null, ['src' => 'path/to/img.jpg', 'alt' => 'An alternative text', 'width' => 34, 'height' => 34])
20
      */
21
    public static function img(string $src, string $alt, array $attributes = []): Element
22
    {
23
        $attributes['src'] ??= $src;
24
        $attributes['alt'] ??= $alt;
25
        $attributes['title'] ??= $alt;
26
27
        return new Element('img', null, $attributes);
28
    }
29
30
    /**
31
      * ? makes more sense to write
32
      * Marker::a('controller/task/id', 'Click here', ['class' => 'nav'])
33
      * than
34
      * Marker::a('Click here', ['href' => controller/task/id', 'class' => 'nav'])
35
      */
36
    public static function a(string $href, string $label, array $attributes = []): Element
37
    {
38
        $attributes['href'] ??= $href;
39
40
        return new Element('a', $label, $attributes);
41
    }
42
43
    // TODO labels should mandatory, accessibility
44
    // TODO implement all options of font-awesome
45
    public static function fas(string $icon, string $title = null, array $attributes = []): Element
46
    {
47
        $attributes['title'] ??= $title; // attributes take precedence
48
        $attributes['class'] = sprintf('fas fa-%s %s', $icon, $attributes['class'] ?? '');
49
        return new Element('i', '', $attributes);
50
    }
51
52
    public static function checkbutton(string $name, mixed $value, string $label, array $attributes = []): Element
53
    {
54
        if (!isset($attributes['id'])) {
55
            $attributes['id'] = $name;
56
        }
57
58
        if (!isset($attributes['type'])) {
59
            $attributes['type'] = 'checkbox'; // default
60
        }
61
62
        if (isset($attributes['is_checked']) && $attributes['is_checked'] === true) { // for boolean checkbuttons
63
            $attributes['checked'] = 'checked';
64
            unset($attributes['is_checked']);
65
        }
66
67
        return
68
        Marker::div(
69
            Marker::label(
70
                Form::input($name, $value, $attributes) . Marker::span($label),
71
                ['for' => $attributes['id']]
72
            ),
73
            ['class' => 'checkbutton']
74
        );
75
    }
76
}
77