Add interface and trait for form fields supporting maximum lengths
authorMatthias Schmidt <gravatronics@live.com>
Sun, 31 Dec 2017 16:23:59 +0000 (17:23 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 31 Dec 2017 16:23:59 +0000 (17:23 +0100)
See #2509

wcfsetup/install/files/lib/system/form/builder/field/IMaximumLengthFormField.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/form/builder/field/IMaximumLengthFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/IMaximumLengthFormField.class.php
new file mode 100644 (file)
index 0000000..992706d
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+namespace wcf\system\form\builder\field;
+
+/**
+ * Represents a form field that supports setting the maximum length of the field value.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder\Field
+ * @since      3.2
+ */
+interface IMaximumLengthFormField {
+       /**
+        * Returns the maximum length of the values of this field or `null` if no placeholder
+        * has been set.
+        * 
+        * @return      null|int
+        */
+       public function getMaximumLength();
+       
+       /**
+        * Sets the maximum length of the values of this field. If `null` is passed, the
+        * maximum length is removed.
+        * 
+        * @param       null|int        $maximumLength  maximum field value length
+        * @return      static                          this field
+        * 
+        * @throws      \InvalidArgumentException       if the given maximum length is no integer or otherwise invalid
+        */
+       public function maximumLength($maximumLength = null);
+}
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php
new file mode 100644 (file)
index 0000000..64fe8d1
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+namespace wcf\system\form\builder\field;
+
+/**
+ * Provides default implementations of `IMaximumLengthFormField` methods.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder\Field
+ * @since      3.2
+ */
+trait TMaximumLengthFormField {
+       /**
+        * maximum length of the field value
+        * @var null|int
+        */
+       protected $__maximumLength;
+       
+       /**
+        * Returns the maximum length of the values of this field or `null` if no placeholder
+        * has been set.
+        *
+        * @return      null|int
+        */
+       public function getMaximumLength() {
+               return $this->__maximumLength;
+       }
+       
+       /**
+        * Sets the maximum length of the values of this field. If `null` is passed, the
+        * maximum length is removed.
+        *
+        * @param       null|int        $maximumLength  maximum field value length
+        * @return      static                          this field
+        *
+        * @throws      \InvalidArgumentException       if the given maximum length is no integer or otherwise invalid
+        */
+       public function maximumLength($maximumLength = null) {
+               if ($maximumLength !== null) {
+                       if (!is_int($maximumLength)) {
+                               throw new \InvalidArgumentException("Given maximum length is no int, '" . gettype($maximumLength) . "' given.");
+                       }
+                       
+                       if ($maximumLength <= 0) {
+                               throw new \InvalidArgumentException("Maximum length must be positive, '" . $maximumLength . "' given.");
+                       }
+               }
+               
+               $this->__maximumLength = $maximumLength;
+               
+               return $this;
+       }
+}
\ No newline at end of file