Add `TAutoCompleteFormField`
authorMatthias Schmidt <gravatronics@live.com>
Sat, 19 Dec 2020 12:15:03 +0000 (13:15 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Mon, 21 Dec 2020 17:51:23 +0000 (18:51 +0100)
wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php
new file mode 100644 (file)
index 0000000..4494ed6
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+namespace wcf\system\form\builder\field;
+
+/**
+ * Provides default implementations of `IAutoCompleteFormField` methods.
+ *
+ * @author      Matthias Schmidt
+ * @copyright   2001-2020 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package     WoltLabSuite\Core\System\Form\Builder\Field
+ * @since       5.4
+ */
+trait TAutoCompleteFormField {
+       /** @var ?string */
+       protected $autoComplete;
+       
+       /**
+        * Sets the `autocomplete` attribute of the form field.
+        *
+        * Multiple tokens can be separated by spaces and if `null` is given, the attribute is unset.
+        *
+        * @throws      \InvalidArgumentException       if an invalid `autocomplete` token is included in the attribute value
+        */
+       public function autocomplete(?string $autoComplete): self {
+               if ($autoComplete !== null && $autoComplete !== 'on' && $autoComplete !== 'off') {
+                       if (preg_match('~^(?:section-\w+ )?(?:(shipping|billing) )?(?P<token>.+)$~', $autoComplete, $matches)) {
+                               if (!in_array($matches['token'], $this->getValidAutoCompleteTokens())) {
+                                       throw new \InvalidArgumentException("Invalid autocomplete attribute '{$autoComplete}'.");
+                               }
+                       }
+                       else {
+                               throw new \InvalidArgumentException("Invalid autocomplete attribute '{$autoComplete}'.");
+                       }
+               }
+               
+               $this->autoComplete = $autoComplete;
+               
+               return $this;
+       }
+       
+       /**
+        * Returns the `autocomplete` attribute of the form field.
+        *
+        * If `null` is returned, no `autocomplete` attribute will be set.
+        */
+       public function getAutoComplete(): ?string {
+               return $this->autoComplete;
+       }
+       
+       /**
+        * Returns all valid `autocomplete` tokens. If an empty array is returned, all tokens are considered valid.
+        *
+        * @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#inappropriate-for-the-control
+        */
+       protected function getValidAutoCompleteTokens(): array {
+               return [];
+       }
+}