*/ /** * patTemplate modfifier Number 2 String: N2S * * Convert number to string * * @package patTemplate * @package Modifiers * @author gERD Schaufelberger */ class patTemplate_Modifier_N2s extends patTemplate_Modifier { const CAST_TYPE_INT = 'int'; const CAST_TYPE_FLOAT = 'float'; const CAST_TYPE_DATE = 'date'; /** * Cast Type * @var string */ private $castType; /** * Modify the value * * Parameter: * - threshold: comma separated list of threshold values from lowest to highest * - string: comma separated list of string values - there must be one more string than thresholds * - cast: int, float or date * * @param string value * @return string modified value */ public function modify($value, $params = array()) { $params = array_merge(array( 'threshold' => '1', 'string' => 'lower,higher', 'cast' => self::CAST_TYPE_INT ), $params); $this->castType = $params['cast']; $threshold = array_map(array($this, 'cast'), explode(',', $params['threshold'])); $string = explode(',', $params['string']); $value = $this->cast($value); for ($i = count($threshold) - 1; $i >= 0; --$i) { if ($value >= $threshold[$i]) { return $string[$i + 1]; } } return $string[0]; } /** * Cast Type * * Cast to some numeric value to make them comparable * @param string $v * @return int|float */ private function cast($v) { switch ($this->castType) { case self::CAST_TYPE_DATE: return strtotime($v); break; case self::CAST_TYPE_FLOAT: return floatval($v); break; case self::CAST_TYPE_INT: default: break; } return intval($v); } }