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