name = $name; $this->type = $type; $this->label = $label; $this->value = $value; $this->context = $context; $this->valueUnit = $valueUnit; $this->setFormat($format); } /** * @param string $name * * @return WidgetData */ public function setName($name) { $this->name = $name; return $this; } /** * @param string $label * * @return WidgetData */ public function setLabel($label) { $this->label = $label; return $this; } /** * @param string $format * * @return void * * @throws InvalidArgumentException */ public function setFormat($format) { if (!in_array($format, self::$formats, true)) { throw new InvalidArgumentException(sprintf('Unknown format "%s".', $format)); } $this->format = $format; } /** * @return array formatted Value(s) */ public function getFormattedValue() { if (empty($this->value)) { return []; } $result = $this->value; switch ($this->format) { case self::FORMAT_TEXT: foreach ($result as $key => &$val) { if(is_array($val)) { $val = implode(',', $val); } else { $val = (string)$val; } } unset($val); break; case self::FORMAT_CURRENCY: foreach ($result as $key => &$val) { if (is_numeric($val)) { $val = number_format($val, 2, ',', '.'); } else { $val = (string)$val; } } unset($val); break; case self::FORMAT_DECIMAL: foreach ($result as $key => &$val) { if (is_numeric($val)) { $val = number_format($val, 2, ',', ''); } else { $val = (string)$val; } } unset($val); break; case self::FORMAT_HOURS: foreach ($result as $key => &$val) { if (is_numeric($val)) { $min = $val * 60; $hours = floor($min / 60); $min %= 60; $val = sprintf('%02dh %02dm', $hours, $min); } else { $val = (string)$val; } } unset($val); break; default: $result = []; } return $result; } /** * @return array */ public function toArray() { if ($this->type === self::WIDGET_TYPE_CONTRAST || $this->type === self::WIDGET_TYPE_CONTRAST_BIG) { $trend = $this->getContrastTrend(); $this->value['trend'] = $trend; } return [ 'name' => $this->name, 'type' => $this->type, 'label' => $this->label, 'value' => $this->value, 'formattedValue' => $this->getFormattedValue(), 'valueUnit' => $this->valueUnit, 'format' => $this->format, 'context' => $this->context, ]; } /** * @return string */ private function getContrastTrend() { if (!isset($this->value['current'], $this->value['previous'])) { return self::WIDGET_TREND_NONE; } if ($this->value['current'] > $this->value['previous']) { return self::WIDGET_TREND_RISE; } if ($this->value['current'] < $this->value['previous']) { return self::WIDGET_TREND_FALL; } return self::WIDGET_TREND_EQUAL; } }