1<?php 2namespace plugin\struct\types; 3 4use plugin\struct\meta\ValidationException; 5 6/** 7 * Class Decimal 8 * 9 * A field accepting decimal numbers 10 * 11 * @package plugin\struct\types 12 */ 13class Decimal extends AbstractMultiBaseType { 14 15 protected $config = array( 16 'format' => '%f', 17 'min' => '', 18 'max' => '', 19 'decpoint' => '.' 20 ); 21 22 /** 23 * Output the stored data 24 * 25 * @param string|int $value the value stored in the database 26 * @param \Doku_Renderer $R the renderer currently used to render the data 27 * @param string $mode The mode the output is rendered in (eg. XHTML) 28 * @return bool true if $mode could be satisfied 29 */ 30 public function renderValue($value, \Doku_Renderer $R, $mode) { 31 $value = sprintf($this->config['format'], $value); 32 $value = str_replace('.', $this->config['decpoint'], $value); 33 $R->cdata($value); 34 return true; 35 } 36 37 /** 38 * @param int|string $value 39 * @return int|string 40 * @throws ValidationException 41 */ 42 public function validate($value) { 43 $value = parent::validate($value); 44 $value = str_replace(',', '.', $value); // we accept both 45 46 if((string) $value != (string) floatval($value)) { 47 throw new ValidationException('Decimal needed'); 48 } 49 50 if($this->config['min'] !== '' && floatval($value) <= floatval($this->config['min'])) { 51 throw new ValidationException('Decimal min', floatval($this->config['min'])); 52 } 53 54 if($this->config['max'] !== '' && floatval($value) >= floatval($this->config['max'])) { 55 throw new ValidationException('Decimal max', floatval($this->config['max'])); 56 } 57 58 return $value; 59 } 60 61} 62