xref: /dokuwiki/inc/Form/Element.php (revision 6fd0861fbb9ed43b45640925d9acfe3dba3aad7b)
112a4e4d1SAndreas Gohr<?php
29d01c1d9SSatoshi Sahara
312a4e4d1SAndreas Gohrnamespace dokuwiki\Form;
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 */
129d01c1d9SSatoshi Saharaabstract class Element
139d01c1d9SSatoshi Sahara{
1412a4e4d1SAndreas Gohr    /**
1512a4e4d1SAndreas Gohr     * @var array the attributes of this element
1612a4e4d1SAndreas Gohr     */
17*6fd0861fSAndreas Gohr    protected $attributes = [];
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     */
28*6fd0861fSAndreas Gohr    public function __construct($type, $attributes = [])
299d01c1d9SSatoshi Sahara    {
3012a4e4d1SAndreas Gohr        $this->type = $type;
3112a4e4d1SAndreas Gohr        $this->attributes = $attributes;
3212a4e4d1SAndreas Gohr    }
3312a4e4d1SAndreas Gohr
3412a4e4d1SAndreas Gohr    /**
35de19515fSAndreas Gohr     * Type of this element
36de19515fSAndreas Gohr     *
37de19515fSAndreas Gohr     * @return string
38de19515fSAndreas Gohr     */
399d01c1d9SSatoshi Sahara    public function getType()
409d01c1d9SSatoshi Sahara    {
41de19515fSAndreas Gohr        return $this->type;
42de19515fSAndreas Gohr    }
43de19515fSAndreas Gohr
44de19515fSAndreas Gohr    /**
4512a4e4d1SAndreas Gohr     * Gets or sets an attribute
4612a4e4d1SAndreas Gohr     *
4712a4e4d1SAndreas Gohr     * When no $value is given, the current content of the attribute is returned.
4812a4e4d1SAndreas Gohr     * An empty string is returned for unset attributes.
4912a4e4d1SAndreas Gohr     *
5012a4e4d1SAndreas Gohr     * When a $value is given, the content is set to that value and the Element
5112a4e4d1SAndreas Gohr     * itself is returned for easy chaining
5212a4e4d1SAndreas Gohr     *
5312a4e4d1SAndreas Gohr     * @param string $name Name of the attribute to access
5412a4e4d1SAndreas Gohr     * @param null|string $value New value to set
5512a4e4d1SAndreas Gohr     * @return string|$this
5612a4e4d1SAndreas Gohr     */
579d01c1d9SSatoshi Sahara    public function attr($name, $value = null)
589d01c1d9SSatoshi Sahara    {
5912a4e4d1SAndreas Gohr        // set
6012a4e4d1SAndreas Gohr        if ($value !== null) {
6112a4e4d1SAndreas Gohr            $this->attributes[$name] = $value;
6212a4e4d1SAndreas Gohr            return $this;
6312a4e4d1SAndreas Gohr        }
6412a4e4d1SAndreas Gohr
6512a4e4d1SAndreas Gohr        // get
6612a4e4d1SAndreas Gohr        if (isset($this->attributes[$name])) {
6712a4e4d1SAndreas Gohr            return $this->attributes[$name];
6812a4e4d1SAndreas Gohr        } else {
6912a4e4d1SAndreas Gohr            return '';
7012a4e4d1SAndreas Gohr        }
7112a4e4d1SAndreas Gohr    }
7212a4e4d1SAndreas Gohr
7312a4e4d1SAndreas Gohr    /**
7412a4e4d1SAndreas Gohr     * Removes the given attribute if it exists
7512a4e4d1SAndreas Gohr     *
767ec97767SGerrit Uitslag     * @param string $name
7712a4e4d1SAndreas Gohr     * @return $this
7812a4e4d1SAndreas Gohr     */
799d01c1d9SSatoshi Sahara    public function rmattr($name)
809d01c1d9SSatoshi Sahara    {
8112a4e4d1SAndreas Gohr        if (isset($this->attributes[$name])) {
8212a4e4d1SAndreas Gohr            unset($this->attributes[$name]);
8312a4e4d1SAndreas Gohr        }
8412a4e4d1SAndreas Gohr        return $this;
8512a4e4d1SAndreas Gohr    }
8612a4e4d1SAndreas Gohr
8712a4e4d1SAndreas Gohr    /**
8812a4e4d1SAndreas Gohr     * Gets or adds a all given attributes at once
8912a4e4d1SAndreas Gohr     *
9012a4e4d1SAndreas Gohr     * @param array|null $attributes
9112a4e4d1SAndreas Gohr     * @return array|$this
9212a4e4d1SAndreas Gohr     */
939d01c1d9SSatoshi Sahara    public function attrs($attributes = null)
949d01c1d9SSatoshi Sahara    {
9512a4e4d1SAndreas Gohr        // set
9612a4e4d1SAndreas Gohr        if ($attributes) {
9712a4e4d1SAndreas Gohr            foreach ((array) $attributes as $key => $val) {
9812a4e4d1SAndreas Gohr                $this->attr($key, $val);
9912a4e4d1SAndreas Gohr            }
10012a4e4d1SAndreas Gohr            return $this;
10112a4e4d1SAndreas Gohr        }
10212a4e4d1SAndreas Gohr        // get
10312a4e4d1SAndreas Gohr        return $this->attributes;
10412a4e4d1SAndreas Gohr    }
10512a4e4d1SAndreas Gohr
10612a4e4d1SAndreas Gohr    /**
10712a4e4d1SAndreas Gohr     * Adds a class to the class attribute
10812a4e4d1SAndreas Gohr     *
10912a4e4d1SAndreas Gohr     * This is the preferred method of setting the element's class
11012a4e4d1SAndreas Gohr     *
11112a4e4d1SAndreas Gohr     * @param string $class the new class to add
11212a4e4d1SAndreas Gohr     * @return $this
11312a4e4d1SAndreas Gohr     */
1149d01c1d9SSatoshi Sahara    public function addClass($class)
1159d01c1d9SSatoshi Sahara    {
11612a4e4d1SAndreas Gohr        $classes = explode(' ', $this->attr('class'));
11712a4e4d1SAndreas Gohr        $classes[] = $class;
11812a4e4d1SAndreas Gohr        $classes = array_unique($classes);
1196d0ceaf9SAndreas Gohr        $classes = array_filter($classes);
120*6fd0861fSAndreas Gohr        $this->attr('class', implode(' ', $classes));
12112a4e4d1SAndreas Gohr        return $this;
12212a4e4d1SAndreas Gohr    }
12312a4e4d1SAndreas Gohr
12412a4e4d1SAndreas Gohr    /**
12512a4e4d1SAndreas Gohr     * Get or set the element's ID
12612a4e4d1SAndreas Gohr     *
12712a4e4d1SAndreas Gohr     * This is the preferred way of setting the element's ID
12812a4e4d1SAndreas Gohr     *
12912a4e4d1SAndreas Gohr     * @param null|string $id
13012a4e4d1SAndreas Gohr     * @return string|$this
13112a4e4d1SAndreas Gohr     */
1329d01c1d9SSatoshi Sahara    public function id($id = null)
1339d01c1d9SSatoshi Sahara    {
13412a4e4d1SAndreas Gohr        if (strpos($id, '__') === false) {
13512a4e4d1SAndreas Gohr            throw new \InvalidArgumentException('IDs in DokuWiki have to contain two subsequent underscores');
13612a4e4d1SAndreas Gohr        }
13712a4e4d1SAndreas Gohr
13812a4e4d1SAndreas Gohr        return $this->attr('id', $id);
13912a4e4d1SAndreas Gohr    }
14012a4e4d1SAndreas Gohr
14112a4e4d1SAndreas Gohr    /**
14212a4e4d1SAndreas Gohr     * Get or set the element's value
14312a4e4d1SAndreas Gohr     *
14412a4e4d1SAndreas Gohr     * This is the preferred way of setting the element's value
14512a4e4d1SAndreas Gohr     *
14612a4e4d1SAndreas Gohr     * @param null|string $value
14712a4e4d1SAndreas Gohr     * @return string|$this
14812a4e4d1SAndreas Gohr     */
1499d01c1d9SSatoshi Sahara    public function val($value = null)
1509d01c1d9SSatoshi Sahara    {
15112a4e4d1SAndreas Gohr        return $this->attr('value', $value);
15212a4e4d1SAndreas Gohr    }
15312a4e4d1SAndreas Gohr
15412a4e4d1SAndreas Gohr    /**
15512a4e4d1SAndreas Gohr     * The HTML representation of this element
15612a4e4d1SAndreas Gohr     *
15712a4e4d1SAndreas Gohr     * @return string
15812a4e4d1SAndreas Gohr     */
15912a4e4d1SAndreas Gohr    abstract public function toHTML();
16012a4e4d1SAndreas Gohr}
161