blob: 019d79f8561d703fc4cffbd4dbdf6f7b6f92942a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?php
namespace Psso;
class Input {
// input-type constants that map to their HTML equivalents
/** Plain text. */
public const string TEXT = 'text';
/** Hidden/masked text. */
public const string SECRET = 'password';
/** Integer. */
public const string NUMERIC = 'number';
public protected(set) string $id;
public protected(set) string $type;
public protected(set) string $label;
public protected(set) bool $required;
public private(set) string $serial;
public function __construct(
string $id, string $type, string $label, bool $required = true
) {
$this->id = $id;
$this->type = $type;
$this->label = $label;
$this->required = $required;
$this->serial = base64_encode(random_bytes(9));
}
private function getUniqueName(): string {
return $this->id . '__' . $this->serial;
}
public function addAsHtml(\SimpleXMLElement $parent): void {
$label = $parent->addChild('label', L($this->label));
$label->addAttribute('for', 'input-' . $this->getUniqueName());
$input = $parent->addChild('input');
$input->addAttribute('type', $this->type);
$input->addAttribute('name', $this->getUniqueName());
$input->addAttribute('id', 'input-' . $this->getUniqueName());
if ($this->required) $input->addAttribute('required', '');
}
}
|