xref: /dokuwiki/inc/Form/Element.php (revision 6d0ceaf93ca31dfb83fd4325ef2eecd9cef733c0)
112a4e4d1SAndreas Gohr<?php
212a4e4d1SAndreas Gohrnamespace dokuwiki\Form;
312a4e4d1SAndreas Gohr
412a4e4d1SAndreas Gohr
512a4e4d1SAndreas Gohr/**
612a4e4d1SAndreas Gohr * Class Element
712a4e4d1SAndreas Gohr *
812a4e4d1SAndreas Gohr * The basic building block of a form
912a4e4d1SAndreas Gohr *
1012a4e4d1SAndreas Gohr * @package dokuwiki\Form
1112a4e4d1SAndreas Gohr */
1212a4e4d1SAndreas Gohrabstract class Element {
1312a4e4d1SAndreas Gohr
1412a4e4d1SAndreas Gohr    /**
1512a4e4d1SAndreas Gohr     * @var array the attributes of this element
1612a4e4d1SAndreas Gohr     */
1712a4e4d1SAndreas Gohr    protected $attributes = array();
1812a4e4d1SAndreas Gohr
1912a4e4d1SAndreas Gohr    /**
2012a4e4d1SAndreas Gohr     * @var string The type of this element
2112a4e4d1SAndreas Gohr     */
2212a4e4d1SAndreas Gohr    protected $type;
2312a4e4d1SAndreas Gohr
2412a4e4d1SAndreas Gohr    /**
2512a4e4d1SAndreas Gohr     * @param string $type The type of this element
2612a4e4d1SAndreas Gohr     * @param array $attributes
2712a4e4d1SAndreas Gohr     */
2812a4e4d1SAndreas Gohr    public function __construct($type, $attributes = array()) {
2912a4e4d1SAndreas Gohr        $this->type = $type;
3012a4e4d1SAndreas Gohr        $this->attributes = $attributes;
3112a4e4d1SAndreas Gohr    }
3212a4e4d1SAndreas Gohr
3312a4e4d1SAndreas Gohr    /**
3412a4e4d1SAndreas Gohr     * Gets or sets an attribute
3512a4e4d1SAndreas Gohr     *
3612a4e4d1SAndreas Gohr     * When no $value is given, the current content of the attribute is returned.
3712a4e4d1SAndreas Gohr     * An empty string is returned for unset attributes.
3812a4e4d1SAndreas Gohr     *
3912a4e4d1SAndreas Gohr     * When a $value is given, the content is set to that value and the Element
4012a4e4d1SAndreas Gohr     * itself is returned for easy chaining
4112a4e4d1SAndreas Gohr     *
4212a4e4d1SAndreas Gohr     * @param string $name Name of the attribute to access
4312a4e4d1SAndreas Gohr     * @param null|string $value New value to set
4412a4e4d1SAndreas Gohr     * @return string|$this
4512a4e4d1SAndreas Gohr     */
4612a4e4d1SAndreas Gohr    public function attr($name, $value = null) {
4712a4e4d1SAndreas Gohr        // set
4812a4e4d1SAndreas Gohr        if($value !== null) {
4912a4e4d1SAndreas Gohr            $this->attributes[$name] = $value;
5012a4e4d1SAndreas Gohr            return $this;
5112a4e4d1SAndreas Gohr        }
5212a4e4d1SAndreas Gohr
5312a4e4d1SAndreas Gohr        // get
5412a4e4d1SAndreas Gohr        if(isset($this->attributes[$name])) {
5512a4e4d1SAndreas Gohr            return $this->attributes[$name];
5612a4e4d1SAndreas Gohr        } else {
5712a4e4d1SAndreas Gohr            return '';
5812a4e4d1SAndreas Gohr        }
5912a4e4d1SAndreas Gohr    }
6012a4e4d1SAndreas Gohr
6112a4e4d1SAndreas Gohr    /**
6212a4e4d1SAndreas Gohr     * Removes the given attribute if it exists
6312a4e4d1SAndreas Gohr     *
6412a4e4d1SAndreas Gohr     * @param $name
6512a4e4d1SAndreas Gohr     * @return $this
6612a4e4d1SAndreas Gohr     */
6712a4e4d1SAndreas Gohr    public function rmattr($name) {
6812a4e4d1SAndreas Gohr        if(isset($this->attributes[$name])) {
6912a4e4d1SAndreas Gohr            unset($this->attributes[$name]);
7012a4e4d1SAndreas Gohr        }
7112a4e4d1SAndreas Gohr        return $this;
7212a4e4d1SAndreas Gohr    }
7312a4e4d1SAndreas Gohr
7412a4e4d1SAndreas Gohr    /**
7512a4e4d1SAndreas Gohr     * Gets or adds a all given attributes at once
7612a4e4d1SAndreas Gohr     *
7712a4e4d1SAndreas Gohr     * @param array|null $attributes
7812a4e4d1SAndreas Gohr     * @return array|$this
7912a4e4d1SAndreas Gohr     */
8012a4e4d1SAndreas Gohr    public function attrs($attributes = null) {
8112a4e4d1SAndreas Gohr        // set
8212a4e4d1SAndreas Gohr        if($attributes) {
8312a4e4d1SAndreas Gohr            foreach((array) $attributes as $key => $val) {
8412a4e4d1SAndreas Gohr                $this->attr($key, $val);
8512a4e4d1SAndreas Gohr            }
8612a4e4d1SAndreas Gohr            return $this;
8712a4e4d1SAndreas Gohr        }
8812a4e4d1SAndreas Gohr        // get
8912a4e4d1SAndreas Gohr        return $this->attributes;
9012a4e4d1SAndreas Gohr    }
9112a4e4d1SAndreas Gohr
9212a4e4d1SAndreas Gohr    /**
9312a4e4d1SAndreas Gohr     * Adds a class to the class attribute
9412a4e4d1SAndreas Gohr     *
9512a4e4d1SAndreas Gohr     * This is the preferred method of setting the element's class
9612a4e4d1SAndreas Gohr     *
9712a4e4d1SAndreas Gohr     * @param string $class the new class to add
9812a4e4d1SAndreas Gohr     * @return $this
9912a4e4d1SAndreas Gohr     */
10012a4e4d1SAndreas Gohr    public function addClass($class) {
10112a4e4d1SAndreas Gohr        $classes = explode(' ', $this->attr('class'));
10212a4e4d1SAndreas Gohr        $classes[] = $class;
10312a4e4d1SAndreas Gohr        $classes = array_unique($classes);
104*6d0ceaf9SAndreas Gohr        $classes = array_filter($classes);
10512a4e4d1SAndreas Gohr        $this->attr('class', join(' ', $classes));
10612a4e4d1SAndreas Gohr        return $this;
10712a4e4d1SAndreas Gohr    }
10812a4e4d1SAndreas Gohr
10912a4e4d1SAndreas Gohr    /**
11012a4e4d1SAndreas Gohr     * Get or set the element's ID
11112a4e4d1SAndreas Gohr     *
11212a4e4d1SAndreas Gohr     * This is the preferred way of setting the element's ID
11312a4e4d1SAndreas Gohr     *
11412a4e4d1SAndreas Gohr     * @param null|string $id
11512a4e4d1SAndreas Gohr     * @return string|$this
11612a4e4d1SAndreas Gohr     */
11712a4e4d1SAndreas Gohr    public function id($id = null) {
11812a4e4d1SAndreas Gohr        if(strpos($id, '__') === false) {
11912a4e4d1SAndreas Gohr            throw new \InvalidArgumentException('IDs in DokuWiki have to contain two subsequent underscores');
12012a4e4d1SAndreas Gohr        }
12112a4e4d1SAndreas Gohr
12212a4e4d1SAndreas Gohr        return $this->attr('id', $id);
12312a4e4d1SAndreas Gohr    }
12412a4e4d1SAndreas Gohr
12512a4e4d1SAndreas Gohr    /**
12612a4e4d1SAndreas Gohr     * Get or set the element's value
12712a4e4d1SAndreas Gohr     *
12812a4e4d1SAndreas Gohr     * This is the preferred way of setting the element's value
12912a4e4d1SAndreas Gohr     *
13012a4e4d1SAndreas Gohr     * @param null|string $value
13112a4e4d1SAndreas Gohr     * @return string|$this
13212a4e4d1SAndreas Gohr     */
13312a4e4d1SAndreas Gohr    public function val($value = null) {
13412a4e4d1SAndreas Gohr        return $this->attr('value', $value);
13512a4e4d1SAndreas Gohr    }
13612a4e4d1SAndreas Gohr
13712a4e4d1SAndreas Gohr    /**
13812a4e4d1SAndreas Gohr     * The HTML representation of this element
13912a4e4d1SAndreas Gohr     *
14012a4e4d1SAndreas Gohr     * @return string
14112a4e4d1SAndreas Gohr     */
14212a4e4d1SAndreas Gohr    abstract public function toHTML();
14312a4e4d1SAndreas Gohr}
144