xref: /plugin/struct/meta/Value.php (revision 053212b194c66249cf544d22d6235bed793fcf3f)
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        if($column->isMulti() && !is_array($value)) {
2838fa36fbSAndreas Gohr            $value = array($value);
2938fa36fbSAndreas Gohr        }
3038fa36fbSAndreas Gohr        $this->value = $value;
3138fa36fbSAndreas Gohr        $this->column = $column;
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    /**
4938fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
5038fa36fbSAndreas Gohr     *
51*053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
52*053212b1SAndreas Gohr     *
5338fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
5438fa36fbSAndreas Gohr     * @param string $mode
55*053212b1SAndreas Gohr     * @return bool
5638fa36fbSAndreas Gohr     */
5738fa36fbSAndreas Gohr    public function render(\Doku_Renderer $R, $mode) {
5838fa36fbSAndreas Gohr        if($this->column->isMulti()) {
59*053212b1SAndreas Gohr            return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
6038fa36fbSAndreas Gohr        } else {
61*053212b1SAndreas Gohr            return $this->column->getType()->renderValue($this->value, $R, $mode);
62*053212b1SAndreas Gohr        }
63*053212b1SAndreas Gohr    }
64*053212b1SAndreas Gohr
65*053212b1SAndreas Gohr    /**
66*053212b1SAndreas Gohr     * Return the value editor for this value field
67*053212b1SAndreas Gohr     *
68*053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
69*053212b1SAndreas Gohr     * @return string The HTML for the editor
70*053212b1SAndreas Gohr     */
71*053212b1SAndreas Gohr    public function getValueEditor($name) {
72*053212b1SAndreas Gohr        if($this->column->isMulti()) {
73*053212b1SAndreas Gohr            return $this->column->getType()->multiValueEditor($name, $this->value);
74*053212b1SAndreas Gohr        } else {
75*053212b1SAndreas Gohr            return $this->column->getType()->valueEditor($name, $this->value);
7638fa36fbSAndreas Gohr        }
7738fa36fbSAndreas Gohr    }
7838fa36fbSAndreas Gohr}
79