From 12a4e4d1ed827c59290838d5a11d75ad32aa28f1 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 16:15:16 +0200 Subject: start of rewriting the form handling This is jsut a small beginning. Not all elements are there, yet. It's also completely untested so far. --- inc/Form/Element.php | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 inc/Form/Element.php (limited to 'inc/Form/Element.php') diff --git a/inc/Form/Element.php b/inc/Form/Element.php new file mode 100644 index 000000000..6f23e2631 --- /dev/null +++ b/inc/Form/Element.php @@ -0,0 +1,142 @@ +type = $type; + $this->attributes = $attributes; + } + + /** + * Gets or sets an attribute + * + * When no $value is given, the current content of the attribute is returned. + * An empty string is returned for unset attributes. + * + * When a $value is given, the content is set to that value and the Element + * itself is returned for easy chaining + * + * @param string $name Name of the attribute to access + * @param null|string $value New value to set + * @return string|$this + */ + public function attr($name, $value = null) { + // set + if($value !== null) { + $this->attributes[$name] = $value; + return $this; + } + + // get + if(isset($this->attributes[$name])) { + return $this->attributes[$name]; + } else { + return ''; + } + } + + /** + * Removes the given attribute if it exists + * + * @param $name + * @return $this + */ + public function rmattr($name) { + if(isset($this->attributes[$name])) { + unset($this->attributes[$name]); + } + return $this; + } + + /** + * Gets or adds a all given attributes at once + * + * @param array|null $attributes + * @return array|$this + */ + public function attrs($attributes = null) { + // set + if($attributes) { + foreach((array) $attributes as $key => $val) { + $this->attr($key, $val); + } + return $this; + } + // get + return $this->attributes; + } + + /** + * Adds a class to the class attribute + * + * This is the preferred method of setting the element's class + * + * @param string $class the new class to add + * @return $this + */ + public function addClass($class) { + $classes = explode(' ', $this->attr('class')); + $classes[] = $class; + $classes = array_unique($classes); + $this->attr('class', join(' ', $classes)); + return $this; + } + + /** + * Get or set the element's ID + * + * This is the preferred way of setting the element's ID + * + * @param null|string $id + * @return string|$this + */ + public function id($id = null) { + if(strpos($id, '__') === false) { + throw new \InvalidArgumentException('IDs in DokuWiki have to contain two subsequent underscores'); + } + + return $this->attr('id', $id); + } + + /** + * Get or set the element's value + * + * This is the preferred way of setting the element's value + * + * @param null|string $value + * @return string|$this + */ + public function val($value = null) { + return $this->attr('value', $value); + } + + /** + * The HTML representation of this element + * + * @return string + */ + abstract public function toHTML(); +} -- cgit v1.2.3 From 6d0ceaf93ca31dfb83fd4325ef2eecd9cef733c0 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 17:26:11 +0200 Subject: added a first few tests. this is far from comprehensible, but should give an idea how the new library works and how to write tests --- inc/Form/Element.php | 1 + 1 file changed, 1 insertion(+) (limited to 'inc/Form/Element.php') diff --git a/inc/Form/Element.php b/inc/Form/Element.php index 6f23e2631..3fd170a80 100644 --- a/inc/Form/Element.php +++ b/inc/Form/Element.php @@ -101,6 +101,7 @@ abstract class Element { $classes = explode(' ', $this->attr('class')); $classes[] = $class; $classes = array_unique($classes); + $classes = array_filter($classes); $this->attr('class', join(' ', $classes)); return $this; } -- cgit v1.2.3 From de19515f04567db78bd41d5bff68a88bfb8c2a22 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 19:12:21 +0200 Subject: started with the compatibility layer --- inc/Form/Element.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'inc/Form/Element.php') diff --git a/inc/Form/Element.php b/inc/Form/Element.php index 3fd170a80..7938ee009 100644 --- a/inc/Form/Element.php +++ b/inc/Form/Element.php @@ -1,7 +1,6 @@ attributes = $attributes; } + /** + * Type of this element + * + * @return string + */ + public function getType() { + return $this->type; + } + /** * Gets or sets an attribute * -- cgit v1.2.3