Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
48 / 48 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| FormElementGenerator | |
100.00% |
48 / 48 |
|
100.00% |
5 / 5 |
26 | |
100.00% |
1 / 1 |
| generate | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
| generateInput | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
10 | |||
| generateSelect | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| generateTextarea | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| generateLabel | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Model\Html |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Model\Html; |
| 16 | |
| 17 | use phpOMS\Stdlib\Base\SmartDateTime; |
| 18 | |
| 19 | /** |
| 20 | * Form element generator class. |
| 21 | * |
| 22 | * @package phpOMS\Model\Html |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class FormElementGenerator |
| 28 | { |
| 29 | /** |
| 30 | * Generate a form element from a json object |
| 31 | * |
| 32 | * @param array $json Json object representing the form element |
| 33 | * @param mixed $value Null means the default value in the json array will be used |
| 34 | * @param string[] $lang Language array |
| 35 | * |
| 36 | * @return string |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | public static function generate(array $json, mixed $value = null, array $lang = []) : string |
| 41 | { |
| 42 | if (!isset($json['type'])) { |
| 43 | return 'INVALID'; |
| 44 | } |
| 45 | |
| 46 | if ($json['type'] === 'select') { |
| 47 | return self::generateSelect($json, $value, $lang); |
| 48 | } elseif ($json['type'] === 'input') { |
| 49 | return self::generateInput($json, $value, $lang); |
| 50 | } elseif ($json['type'] === 'label') { |
| 51 | return self::generateLabel($json, $lang); |
| 52 | } elseif ($json['type'] === 'textarea') { |
| 53 | return self::generateTextarea($json, $value); |
| 54 | } |
| 55 | |
| 56 | return 'INVALID'; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Generate a form element from a json object |
| 61 | * |
| 62 | * @param array $json Json object representing the form element |
| 63 | * @param mixed $value Null means the default value in the json array will be used |
| 64 | * @param string[] $lang Language array |
| 65 | * |
| 66 | * @return string |
| 67 | * |
| 68 | * @since 1.0.0 |
| 69 | */ |
| 70 | private static function generateInput(array $json, mixed $value = null, array $lang = []) : string |
| 71 | { |
| 72 | $element = '<input'; |
| 73 | foreach ($json['attributes'] as $attribute => $val) { |
| 74 | $element .= ' ' . $attribute . '="' . $val . '"'; |
| 75 | } |
| 76 | |
| 77 | $value ??= $json['default']['value'] ?? ''; |
| 78 | |
| 79 | $element .= (isset($json['default']) || $value !== null ? ' value="' . ($json['subtype'] === 'datetime' ? (new SmartDateTime($value))->format($json['default']['format']) : $value) . '"' : ''); |
| 80 | |
| 81 | $element .= ($json['subtype'] === 'checkbox' || $json['subtype'] === 'radio') && $json['default']['checked'] ? ' checked' : ''; |
| 82 | $element .= '>'; |
| 83 | |
| 84 | return $element |
| 85 | . ($json['subtype'] === 'checkbox' || $json['subtype'] === 'radio' |
| 86 | ? '<label for="' . $json['attributes']['id'] . '">' |
| 87 | . ($lang[$json['default']['content']] ?? $json['default']['content']) |
| 88 | . '</label>' |
| 89 | : '' |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Generate a form element from a json object |
| 95 | * |
| 96 | * @param array $json Json object representing the form element |
| 97 | * @param mixed $value Null means the default value in the json array will be used |
| 98 | * @param string[] $lang Language array |
| 99 | * |
| 100 | * @return string |
| 101 | * |
| 102 | * @since 1.0.0 |
| 103 | */ |
| 104 | private static function generateSelect(array $json, mixed $value = null, array $lang = []) : string |
| 105 | { |
| 106 | $element = '<select'; |
| 107 | foreach ($json['attributes'] as $attribute => $val) { |
| 108 | $element .= ' ' . $attribute . '="' . $val . '"'; |
| 109 | } |
| 110 | |
| 111 | $element .= '>'; |
| 112 | |
| 113 | $value ??= $json['default']['value']; |
| 114 | |
| 115 | foreach ($json['options'] as $val => $text) { |
| 116 | $element .= '<option value="' . $val . '"' . (isset($json['default']) && $val === $value ? ' selected' : '') . '>' |
| 117 | . ($lang[$text] ?? $text) |
| 118 | . '</option>'; |
| 119 | } |
| 120 | |
| 121 | return $element . '</select>'; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Generate a form element from a json object |
| 126 | * |
| 127 | * @param array $json Json object representing the form element |
| 128 | * @param mixed $value Null means the default value in the json array will be used |
| 129 | * |
| 130 | * @return string |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | */ |
| 134 | private static function generateTextarea(array $json, mixed $value = null) : string |
| 135 | { |
| 136 | $element = '<textarea'; |
| 137 | foreach ($json['attributes'] as $attribute => $val) { |
| 138 | $element .= ' ' . $attribute . '="' . $val . '"'; |
| 139 | } |
| 140 | |
| 141 | $value ??= $json['default']['value']; |
| 142 | |
| 143 | $element .= '>'; |
| 144 | $element .= isset($json['default']) ? $value : ''; |
| 145 | |
| 146 | return $element . '</textarea>'; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Generate a form element from a json object |
| 151 | * |
| 152 | * @param array $json Json object representing the form element |
| 153 | * @param string[] $lang Language array |
| 154 | * |
| 155 | * @return string |
| 156 | * |
| 157 | * @since 1.0.0 |
| 158 | */ |
| 159 | private static function generateLabel(array $json, array $lang = []) : string |
| 160 | { |
| 161 | $element = '<label'; |
| 162 | foreach ($json['attributes'] as $attribute => $val) { |
| 163 | $element .= ' ' . $attribute . '="' . $val . '"'; |
| 164 | } |
| 165 | |
| 166 | $element .= '>'; |
| 167 | $element .= $lang[$json['default']['value']] ?? $json['default']['value']; |
| 168 | |
| 169 | return $element . '</label>'; |
| 170 | } |
| 171 | } |