xref: /plugin/struct/meta/Value.php (revision 17560ecbb3470cf2f90ee861205d92f6adf36b5b)
138fa36fbSAndreas Gohr<?php
238fa36fbSAndreas Gohr
338fa36fbSAndreas Gohrnamespace 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 *
1038fa36fbSAndreas Gohr * @package 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
2038fa36fbSAndreas Gohr    /**
2138fa36fbSAndreas Gohr     * Value constructor.
2238fa36fbSAndreas Gohr     *
2338fa36fbSAndreas Gohr     * @param Column $column
2438fa36fbSAndreas Gohr     * @param array|int|string $value
2538fa36fbSAndreas Gohr     */
2638fa36fbSAndreas Gohr    public function __construct(Column $column, $value) {
2738fa36fbSAndreas Gohr        $this->column = $column;
28*17560ecbSAndreas Gohr        $this->setValue($value);
2938fa36fbSAndreas Gohr    }
3038fa36fbSAndreas Gohr
3138fa36fbSAndreas Gohr    /**
3238fa36fbSAndreas Gohr     * @return Column
3338fa36fbSAndreas Gohr     */
3438fa36fbSAndreas Gohr    public function getColumn() {
3538fa36fbSAndreas Gohr        return $this->column;
3638fa36fbSAndreas Gohr    }
3738fa36fbSAndreas Gohr
3838fa36fbSAndreas Gohr    /**
3938fa36fbSAndreas Gohr     * @return array|int|string
4038fa36fbSAndreas Gohr     */
4138fa36fbSAndreas Gohr    public function getValue() {
4238fa36fbSAndreas Gohr        return $this->value;
4338fa36fbSAndreas Gohr    }
4438fa36fbSAndreas Gohr
4538fa36fbSAndreas Gohr    /**
46*17560ecbSAndreas Gohr     * Allows overwriting the current value
47*17560ecbSAndreas Gohr     *
48*17560ecbSAndreas Gohr     * @param array|int|string $value
49*17560ecbSAndreas Gohr     */
50*17560ecbSAndreas Gohr    public function setValue($value) {
51*17560ecbSAndreas Gohr        if($this->column->isMulti() && !is_array($value)) {
52*17560ecbSAndreas Gohr            $value = array($value);
53*17560ecbSAndreas Gohr        }
54*17560ecbSAndreas Gohr        $this->value = $value;
55*17560ecbSAndreas Gohr    }
56*17560ecbSAndreas Gohr
57*17560ecbSAndreas Gohr    /**
5838fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
5938fa36fbSAndreas Gohr     *
60053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
61053212b1SAndreas Gohr     *
6238fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
6338fa36fbSAndreas Gohr     * @param string $mode
64053212b1SAndreas Gohr     * @return bool
6538fa36fbSAndreas Gohr     */
6638fa36fbSAndreas Gohr    public function render(\Doku_Renderer $R, $mode) {
6738fa36fbSAndreas Gohr        if($this->column->isMulti()) {
68053212b1SAndreas Gohr            return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
6938fa36fbSAndreas Gohr        } else {
70053212b1SAndreas Gohr            return $this->column->getType()->renderValue($this->value, $R, $mode);
71053212b1SAndreas Gohr        }
72053212b1SAndreas Gohr    }
73053212b1SAndreas Gohr
74053212b1SAndreas Gohr    /**
75053212b1SAndreas Gohr     * Return the value editor for this value field
76053212b1SAndreas Gohr     *
77053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
78053212b1SAndreas Gohr     * @return string The HTML for the editor
79053212b1SAndreas Gohr     */
80053212b1SAndreas Gohr    public function getValueEditor($name) {
81053212b1SAndreas Gohr        if($this->column->isMulti()) {
82053212b1SAndreas Gohr            return $this->column->getType()->multiValueEditor($name, $this->value);
83053212b1SAndreas Gohr        } else {
84053212b1SAndreas Gohr            return $this->column->getType()->valueEditor($name, $this->value);
8538fa36fbSAndreas Gohr        }
8638fa36fbSAndreas Gohr    }
8738fa36fbSAndreas Gohr}
88