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 * Cleans the value(s) of empties 49 * 50 * @param array|int|string $value 51 */ 52 public function setValue($value) { 53 if($this->column->isMulti()) { 54 if(!is_array($value)) { 55 $value = array($value); 56 } 57 // remove all blanks 58 $value = array_map('trim', $value); 59 $value = array_filter($value, array($this, 'filter')); 60 $value = array_values($value); // reset keys 61 } else { 62 $value = trim($value); 63 } 64 $this->value = $value; 65 } 66 67 /** 68 * Render the value using the given renderer and mode 69 * 70 * automativally picks the right mechanism depending on multi or single value 71 * 72 * values are only rendered when there is a value 73 * 74 * @param \Doku_Renderer $R 75 * @param string $mode 76 * @return bool 77 */ 78 public function render(\Doku_Renderer $R, $mode) { 79 if($this->column->isMulti()) { 80 if(count($this->value)) { 81 return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 82 } 83 } else { 84 if($this->value !== '') { 85 return $this->column->getType()->renderValue($this->value, $R, $mode); 86 } 87 } 88 return true; 89 } 90 91 /** 92 * Return the value editor for this value field 93 * 94 * @param string $name The field name to use in the editor 95 * @return string The HTML for the editor 96 */ 97 public function getValueEditor($name) { 98 if($this->column->isMulti()) { 99 return $this->column->getType()->multiValueEditor($name, $this->value); 100 } else { 101 return $this->column->getType()->valueEditor($name, $this->value); 102 } 103 } 104 105 /** 106 * Filter callback to strip empty values 107 * 108 * @param string $input 109 * @return bool 110 */ 111 public function filter($input) { 112 return '' !== ((string) $input); 113 } 114} 115