aboutsummaryrefslogtreecommitdiff
path: root/Psso/Input.php
diff options
context:
space:
mode:
Diffstat (limited to 'Psso/Input.php')
-rw-r--r--Psso/Input.php42
1 files changed, 42 insertions, 0 deletions
diff --git a/Psso/Input.php b/Psso/Input.php
new file mode 100644
index 0000000..019d79f
--- /dev/null
+++ b/Psso/Input.php
@@ -0,0 +1,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', '');
+ }
+}