xref: /plugin/struct/meta/Value.php (revision 6af24d3eff33249280549e60e18474e2cc0bf9d0)
1<?php
2
3namespace 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 plugin\struct\meta
11 */
12class Value {
13
14    /** @var Column */
15    protected $column;
16
17    /** @var  array|int|string */
18    protected $value;
19
20    /**
21     * Value constructor.
22     *
23     * @param Column $column
24     * @param array|int|string $value
25     */
26    public function __construct(Column $column, $value) {
27        $this->column = $column;
28        $this->setValue($value);
29    }
30
31    /**
32     * @return Column
33     */
34    public function getColumn() {
35        return $this->column;
36    }
37
38    /**
39     * @return array|int|string
40     */
41    public function getValue() {
42        return $this->value;
43    }
44
45    /**
46     * Allows overwriting the current value
47     *
48     * @param array|int|string $value
49     */
50    public function setValue($value) {
51        if($this->column->isMulti() && !is_array($value)) {
52            $value = array($value);
53        }
54        $this->value = $value;
55    }
56
57    /**
58     * Render the value using the given renderer and mode
59     *
60     * automativally picks the right mechanism depending on multi or single value
61     *
62     * @param \Doku_Renderer $R
63     * @param string $mode
64     * @return bool
65     */
66    public function render(\Doku_Renderer $R, $mode) {
67        if($this->column->isMulti()) {
68            return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
69        } else {
70            return $this->column->getType()->renderValue($this->value, $R, $mode);
71        }
72    }
73
74    /**
75     * Return the value editor for this value field
76     *
77     * @param string $name The field name to use in the editor
78     * @return string The HTML for the editor
79     */
80    public function getValueEditor($name) {
81        if($this->column->isMulti()) {
82            return $this->column->getType()->multiValueEditor($name, $this->value);
83        } else {
84            return $this->column->getType()->valueEditor($name, $this->value);
85        }
86    }
87}
88