xref: /plugin/struct/meta/Value.php (revision c498205a7375a8f98f1ee968c0d6d3abe545279e)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Class Value
7 *
8 * Holds the value for a single "cell". That value may be an array for multi value columns
9 *
10 * @package dokuwiki\plugin\struct\meta
11 */
12class Value {
13
14    /** @var Column */
15    protected $column;
16
17    /** @var  array|int|string */
18    protected $value;
19
20    /** @var  array|int|string */
21    protected $rawvalue = null;
22
23    /**
24     * Value constructor.
25     *
26     * @param Column $column
27     * @param array|int|string $value
28     */
29    public function __construct(Column $column, $value) {
30        $this->column = $column;
31        $this->setValue($value);
32    }
33
34    /**
35     * @return Column
36     */
37    public function getColumn() {
38        return $this->column;
39    }
40
41    /**
42     * @return array|int|string
43     */
44    public function getValue() {
45        return $this->value;
46    }
47
48    /**
49     * Access the raw value
50     *
51     * @return array|string (array on multi)
52     */
53    public function getRawValue() {
54        return $this->rawvalue;
55    }
56
57    /**
58     * Allows overwriting the current value
59     *
60     * Cleans the value(s) of empties
61     *
62     * @param array|int|string $value
63     */
64    public function setValue($value) {
65        // treat all givens the same
66        if(!is_array($value)) {
67            $value = array($value);
68        }
69
70        // reset/init
71        $this->value = array();
72        $this->rawvalue = array();
73
74        // remove all blanks
75        foreach($value as $val) {
76            $val = trim($val);
77            $raw = $this->column->getType()->rawValue($val);
78            if('' === (string) $raw) continue;
79            $this->value[] = $val;
80            $this->rawvalue[] = $raw;
81        }
82
83        // make single value again
84        if(!$this->column->isMulti()) {
85            $this->value = (string) array_shift($this->value);
86            $this->rawvalue = (string) array_shift($this->rawvalue);
87        }
88    }
89
90    /**
91     * Is this empty?
92     *
93     * @return bool
94     */
95    public function isEmpty() {
96        return ($this->rawvalue === '' || $this->rawvalue === array());
97    }
98
99    /**
100     * Render the value using the given renderer and mode
101     *
102     * automativally picks the right mechanism depending on multi or single value
103     *
104     * values are only rendered when there is a value
105     *
106     * @param \Doku_Renderer $R
107     * @param string $mode
108     * @return bool
109     */
110    public function render(\Doku_Renderer $R, $mode) {
111        if($this->column->isMulti()) {
112            if(count($this->value)) {
113                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
114            }
115        } else {
116            if($this->value !== '') {
117                return $this->column->getType()->renderValue($this->value, $R, $mode);
118            }
119        }
120        return true;
121    }
122
123    /**
124     * Return the value editor for this value field
125     *
126     * @param string $name The field name to use in the editor
127     * @return string The HTML for the editor
128     */
129    public function getValueEditor($name) {
130        if($this->column->isMulti()) {
131            return $this->column->getType()->multiValueEditor($name, $this->value);
132        } else {
133            return $this->column->getType()->valueEditor($name, $this->value);
134        }
135    }
136
137    /**
138     * Filter callback to strip empty values
139     *
140     * @param string $input
141     * @return bool
142     */
143    public function filter($input) {
144        return '' !== ((string) $input);
145    }
146}
147