xref: /plugin/struct/meta/Value.php (revision 90421550b38165507f70359a142d6e0480d3b89f)
138fa36fbSAndreas Gohr<?php
238fa36fbSAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
438fa36fbSAndreas Gohr
538fa36fbSAndreas Gohr/**
638fa36fbSAndreas Gohr * Class Value
738fa36fbSAndreas Gohr *
838fa36fbSAndreas Gohr * Holds the value for a single "cell". That value may be an array for multi value columns
938fa36fbSAndreas Gohr *
10ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
1138fa36fbSAndreas Gohr */
1238fa36fbSAndreas Gohrclass Value {
1338fa36fbSAndreas Gohr
1438fa36fbSAndreas Gohr    /** @var Column */
1538fa36fbSAndreas Gohr    protected $column;
1638fa36fbSAndreas Gohr
1738fa36fbSAndreas Gohr    /** @var  array|int|string */
1838fa36fbSAndreas Gohr    protected $value;
1938fa36fbSAndreas Gohr
20*90421550SAndreas Gohr    /** @var  array|int|string */
21*90421550SAndreas Gohr    protected $rawvalue = null;
22*90421550SAndreas Gohr
2338fa36fbSAndreas Gohr    /**
2438fa36fbSAndreas Gohr     * Value constructor.
2538fa36fbSAndreas Gohr     *
2638fa36fbSAndreas Gohr     * @param Column $column
2738fa36fbSAndreas Gohr     * @param array|int|string $value
2838fa36fbSAndreas Gohr     */
2938fa36fbSAndreas Gohr    public function __construct(Column $column, $value) {
3038fa36fbSAndreas Gohr        $this->column = $column;
3117560ecbSAndreas Gohr        $this->setValue($value);
3238fa36fbSAndreas Gohr    }
3338fa36fbSAndreas Gohr
3438fa36fbSAndreas Gohr    /**
3538fa36fbSAndreas Gohr     * @return Column
3638fa36fbSAndreas Gohr     */
3738fa36fbSAndreas Gohr    public function getColumn() {
3838fa36fbSAndreas Gohr        return $this->column;
3938fa36fbSAndreas Gohr    }
4038fa36fbSAndreas Gohr
4138fa36fbSAndreas Gohr    /**
4238fa36fbSAndreas Gohr     * @return array|int|string
4338fa36fbSAndreas Gohr     */
4438fa36fbSAndreas Gohr    public function getValue() {
4538fa36fbSAndreas Gohr        return $this->value;
4638fa36fbSAndreas Gohr    }
4738fa36fbSAndreas Gohr
4838fa36fbSAndreas Gohr    /**
49*90421550SAndreas Gohr     * Access the raw value
50*90421550SAndreas Gohr     *
51*90421550SAndreas Gohr     * @return array|string (array on multi)
52*90421550SAndreas Gohr     */
53*90421550SAndreas Gohr    public function getRawValue() {
54*90421550SAndreas Gohr        return $this->rawvalue;
55*90421550SAndreas Gohr    }
56*90421550SAndreas Gohr
57*90421550SAndreas Gohr    /**
5817560ecbSAndreas Gohr     * Allows overwriting the current value
5917560ecbSAndreas Gohr     *
6019065d4eSAndreas Gohr     * Cleans the value(s) of empties
6119065d4eSAndreas Gohr     *
6217560ecbSAndreas Gohr     * @param array|int|string $value
6317560ecbSAndreas Gohr     */
6417560ecbSAndreas Gohr    public function setValue($value) {
65*90421550SAndreas Gohr        // treat all givens the same
66*90421550SAndreas Gohr        if(!is_array($value)) {
6717560ecbSAndreas Gohr            $value = array($value);
6817560ecbSAndreas Gohr        }
697937b821SAndreas Gohr
70*90421550SAndreas Gohr        // reset/init
71*90421550SAndreas Gohr        $this->value = array();
72*90421550SAndreas Gohr        $this->rawvalue = array();
73*90421550SAndreas Gohr
7419065d4eSAndreas Gohr        // remove all blanks
75*90421550SAndreas Gohr        foreach($value as $val) {
76*90421550SAndreas Gohr            $val = trim($val);
77*90421550SAndreas Gohr            $raw = $this->column->getType()->rawValue($val);
78*90421550SAndreas Gohr            if('' === (string) $raw) continue;
79*90421550SAndreas Gohr            $this->value[] = $val;
80*90421550SAndreas Gohr            $this->rawvalue[] = $raw;
81*90421550SAndreas Gohr        }
827937b821SAndreas Gohr
83*90421550SAndreas Gohr        // make single value again
847937b821SAndreas Gohr        if(!$this->column->isMulti()) {
85*90421550SAndreas Gohr            $this->value = (string) array_shift($this->value);
86*90421550SAndreas Gohr            $this->rawvalue = (string) array_shift($this->rawvalue);
877937b821SAndreas Gohr        }
8819065d4eSAndreas Gohr    }
897937b821SAndreas Gohr
90*90421550SAndreas Gohr    /**
91*90421550SAndreas Gohr     * Is this empty?
92*90421550SAndreas Gohr     *
93*90421550SAndreas Gohr     * @return bool
94*90421550SAndreas Gohr     */
95*90421550SAndreas Gohr    public function isEmpty() {
96*90421550SAndreas Gohr        return ($this->rawvalue === '' || $this->rawvalue === array());
9717560ecbSAndreas Gohr    }
9817560ecbSAndreas Gohr
9917560ecbSAndreas Gohr    /**
10038fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
10138fa36fbSAndreas Gohr     *
102053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
103053212b1SAndreas Gohr     *
10419065d4eSAndreas Gohr     * values are only rendered when there is a value
10519065d4eSAndreas Gohr     *
10638fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
10738fa36fbSAndreas Gohr     * @param string $mode
108053212b1SAndreas Gohr     * @return bool
10938fa36fbSAndreas Gohr     */
11038fa36fbSAndreas Gohr    public function render(\Doku_Renderer $R, $mode) {
11138fa36fbSAndreas Gohr        if($this->column->isMulti()) {
11219065d4eSAndreas Gohr            if(count($this->value)) {
113053212b1SAndreas Gohr                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
11419065d4eSAndreas Gohr            }
11538fa36fbSAndreas Gohr        } else {
11619065d4eSAndreas Gohr            if($this->value !== '') {
117053212b1SAndreas Gohr                return $this->column->getType()->renderValue($this->value, $R, $mode);
118053212b1SAndreas Gohr            }
119053212b1SAndreas Gohr        }
12019065d4eSAndreas Gohr        return true;
12119065d4eSAndreas Gohr    }
122053212b1SAndreas Gohr
123053212b1SAndreas Gohr    /**
124053212b1SAndreas Gohr     * Return the value editor for this value field
125053212b1SAndreas Gohr     *
126053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
127053212b1SAndreas Gohr     * @return string The HTML for the editor
128053212b1SAndreas Gohr     */
129053212b1SAndreas Gohr    public function getValueEditor($name) {
130053212b1SAndreas Gohr        if($this->column->isMulti()) {
131053212b1SAndreas Gohr            return $this->column->getType()->multiValueEditor($name, $this->value);
132053212b1SAndreas Gohr        } else {
133053212b1SAndreas Gohr            return $this->column->getType()->valueEditor($name, $this->value);
13438fa36fbSAndreas Gohr        }
13538fa36fbSAndreas Gohr    }
13619065d4eSAndreas Gohr
13719065d4eSAndreas Gohr    /**
13819065d4eSAndreas Gohr     * Filter callback to strip empty values
13919065d4eSAndreas Gohr     *
14019065d4eSAndreas Gohr     * @param string $input
14119065d4eSAndreas Gohr     * @return bool
14219065d4eSAndreas Gohr     */
14319065d4eSAndreas Gohr    public function filter($input) {
14419065d4eSAndreas Gohr        return '' !== ((string) $input);
14519065d4eSAndreas Gohr    }
14638fa36fbSAndreas Gohr}
147