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