From 545d316d4dad434edfe5048807bfbe686f856259 Mon Sep 17 00:00:00 2001 From: diosmosis Date: Thu, 13 Nov 2014 18:22:49 -0800 Subject: Make MetricsFormatter a class with instance methods and move Html related functionality to derived class. Use FOrmatter in Metric::format calls so Metrics do not have to be aware of context of formatting, and so Metric::format methods will have less code redundancy. --- core/MetricsFormatter.php | 254 ---------------------------------------------- 1 file changed, 254 deletions(-) delete mode 100644 core/MetricsFormatter.php (limited to 'core/MetricsFormatter.php') diff --git a/core/MetricsFormatter.php b/core/MetricsFormatter.php deleted file mode 100644 index 8554663525..0000000000 --- a/core/MetricsFormatter.php +++ /dev/null @@ -1,254 +0,0 @@ - 0 && $seconds < 0.01 ? 3 : 2); - $seconds = round($seconds, $precision); - - if ($years > 0) { - $return = sprintf(Piwik::translate('General_YearsDays'), $years, $days); - } elseif ($days > 0) { - $return = sprintf(Piwik::translate('General_DaysHours'), $days, $hours); - } elseif ($hours > 0) { - $return = sprintf(Piwik::translate('General_HoursMinutes'), $hours, $minutes); - } elseif ($minutes > 0) { - $return = sprintf(Piwik::translate('General_MinutesSeconds'), $minutes, $seconds); - } else { - $return = sprintf(Piwik::translate('General_Seconds'), $seconds); - } - - if ($isNegative) { - $return = '-' . $return; - } - - if ($isHtml) { - return str_replace(' ', ' ', $return); - } - - return $return; - } - - /** - * Returns a prettified memory size value. - * - * @param number $size The size in bytes. - * @param string $unit The specific unit to use, if any. If null, the unit is determined by $size. - * @param int $precision The precision to use when rounding. - * @return string eg, `'128 M'` or `'256 K'`. - */ - public static function getPrettySizeFromBytes($size, $unit = null, $precision = 1) - { - if ($size == 0) { - return '0 M'; - } - - $units = array('B', 'K', 'M', 'G', 'T'); - foreach ($units as $currentUnit) { - if ($size >= 1024 && $unit != $currentUnit) { - $size = $size / 1024; - } else { - break; - } - } - - return round($size, $precision) . " " . $currentUnit; - } - - /** - * Returns a pretty formated monetary value using the currency associated with a site. - * - * @param int|string $value The monetary value to format. - * @param int $idSite The ID of the site whose currency will be used. - * @param bool $isHtml If true, replaces all spaces with `' '`. - * @return string - */ - public static function getPrettyMoney($value, $idSite, $isHtml = true) - { - $currencyBefore = MetricsFormatter::getCurrencySymbol($idSite); - - $space = ' '; - if ($isHtml) { - $space = ' '; - } - - $currencyAfter = ''; - // (maybe more currencies prefer this notation?) - $currencySymbolToAppend = array('€', 'kr', 'zł'); - - // manually put the currency symbol after the amount - if (in_array($currencyBefore, $currencySymbolToAppend)) { - $currencyAfter = $space . $currencyBefore; - $currencyBefore = ''; - } - - // if the input is a number (it could be a string or INPUT form), - // and if this number is not an int, we round to precision 2 - if (is_numeric($value)) { - if ($value == round($value)) { - // 0.0 => 0 - $value = round($value); - } else { - $precision = GoalManager::REVENUE_PRECISION; - $value = sprintf("%01." . $precision . "f", $value); - } - } - - $prettyMoney = $currencyBefore . $space . $value . $currencyAfter; - return $prettyMoney; - } - - /** - * Prettifies a metric value based on the column name. - * - * @param int $idSite The ID of the site the metric is for (used if the column value is an amount of money). - * @param string $columnName The metric name. - * @param mixed $value The metric value. - * @param bool $isHtml If true, replaces all spaces with `' '`. - * @return string - */ - public static function getPrettyValue($idSite, $columnName, $value, $isHtml) - { - // Display time in human readable - if (strpos($columnName, 'time') !== false) { - // Little hack: Display 15s rather than 00:00:15, only for "(avg|min|max)_generation_time" - $timeAsSentence = (substr($columnName, -16) == '_time_generation'); - return self::getPrettyTimeFromSeconds($value, $timeAsSentence); - } - - // Add revenue symbol to revenues - if (strpos($columnName, 'revenue') !== false && strpos($columnName, 'evolution') === false) { - return self::getPrettyMoney($value, $idSite, $isHtml); - } - - // Add % symbol to rates - if (strpos($columnName, '_rate') !== false) { - if (strpos($value, "%") === false) { - return $value . "%"; - } - } - - return $value; - } - - /** - * Returns the currency symbol for a site. - * - * @param int $idSite The ID of the site to return the currency symbol for. - * @return string eg, `'$'`. - */ - public static function getCurrencySymbol($idSite) - { - $symbols = MetricsFormatter::getCurrencyList(); - $site = new Site($idSite); - $currency = $site->getCurrency(); - - if (isset($symbols[$currency])) { - return $symbols[$currency][0]; - } - - return ''; - } - - /** - * Returns the list of all known currency symbols. - * - * @return array An array mapping currency codes to their respective currency symbols - * and a description, eg, `array('USD' => array('$', 'US dollar'))`. - */ - public static function getCurrencyList() - { - static $currenciesList = null; - - if (is_null($currenciesList)) { - require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Currencies.php'; - $currenciesList = $GLOBALS['Piwik_CurrencyList']; - } - - return $currenciesList; - } -} -- cgit v1.2.3