xref: /plugin/struct/meta/Value.php (revision a1603abdf31ce3c771afa9c516cbb6119c21af7d)
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        if($column->isMulti() && !is_array($value)) {
28            $value = array($value);
29        }
30        $this->value = $value;
31        $this->column = $column;
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     * Render the value using the given renderer and mode
50     *
51     * automativally picks the right mechanism depending on multi or single value
52     *
53     * @param \Doku_Renderer $R
54     * @param string $mode
55     * @return bool
56     */
57    public function render(\Doku_Renderer $R, $mode) {
58        if($this->column->isMulti()) {
59            return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
60        } else {
61            return $this->column->getType()->renderValue($this->value, $R, $mode);
62        }
63    }
64
65    /**
66     * Return the value editor for this value field
67     *
68     * @param string $name The field name to use in the editor
69     * @return string The HTML for the editor
70     */
71    public function getValueEditor($name) {
72        if($this->column->isMulti()) {
73            return $this->column->getType()->multiValueEditor($name, $this->value);
74        } else {
75            return $this->column->getType()->valueEditor($name, $this->value);
76        }
77    }
78}
79