From c751d131685ee8ea22e64470d2a726cbcf757592 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 20 Dec 2020 10:44:51 +0100 Subject: [PATCH] Add `TCssClassFormField` --- .../field/TCssClassFormField.class.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/TCssClassFormField.class.php diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TCssClassFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TCssClassFormField.class.php new file mode 100644 index 0000000000..69f5c9ce1c --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/TCssClassFormField.class.php @@ -0,0 +1,93 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.4 + */ +trait TCssClassFormField { + /** + * CSS classes of this node + * @var string[] + */ + protected $fieldClasses = []; + + /** + * Adds the given CSS class to the actual field element and returns this field. + * + * @throws \InvalidArgumentException if the given class is invalid + */ + public function addFieldClass(string $class): self { + static::validateClass($class); + + if (!in_array($class, $this->fieldClasses)) { + $this->fieldClasses[] = $class; + } + + return $this; + } + + /** + * Adds the given CSS classes to the actual field element and returns this field. + * + * @throws \InvalidArgumentException if any of the given classes is invalid + */ + public function addFieldClasses(array $classes): self { + foreach ($classes as $class) { + $this->addFieldClass($class); + } + + return $this; + } + + /** + * Returns all CSS classes of the actual field element. + */ + public function getFieldClasses(): array { + return $this->fieldClasses; + } + + /** + * Returns `true` if a CSS class of the actual field element with the given name exists and returns `false` otherwise. + * + * @throws \InvalidArgumentException if the given class is invalid + */ + public function hasFieldClass(string $class): bool { + static::validateClass($class); + + return array_search($class, $this->fieldClasses) !== false; + } + + /** + * Removes the given CSS class of the actual field element and returns this field. + * + * If the actual field element does not have the given CSS class, this method silently ignores that fact. + * + * @throws \InvalidArgumentException if the given class is invalid + */ + public function removeFieldClass(string $class): self { + static::validateClass($class); + + $index = array_search($class, $this->fieldClasses); + if ($index !== false) { + unset($this->fieldClasses[$index]); + } + + return $this; + } + + /** + * Checks if the given parameter class is a valid CSS class. + * + * @param string $class checked class + * + * @throws \InvalidArgumentException if the given class is invalid + */ + abstract public static function validateClass($class); +} -- 2.20.1