labelSingular = $labelSingular; $this->labelPlural = $labelPlural; } /** * Beautifies a range label and returns the pretty result. * * @param string $value The range string. This must be in either a '$min-$max' format * a '$min+' format. * @return string The pretty range label. */ public function beautify($value) { // if there's more than one element, handle as a range w/ an upper bound if (strpos($value, "-") !== false) { // get the range sscanf($value, "%d - %d", $lowerBound, $upperBound); // if the lower bound is the same as the upper bound make sure the singular label // is used if ($lowerBound == $upperBound) { return $this->getSingleUnitLabel($value, $lowerBound); } else { return $this->getRangeLabel($value, $lowerBound, $upperBound); } } // if there's one element, handle as a range w/ no upper bound else { // get the lower bound sscanf($value, "%d", $lowerBound); if ($lowerBound !== NULL) { $plusEncoded = urlencode('+'); $plusLen = strlen($plusEncoded); $len = strlen($value); // if the label doesn't end with a '+', append it if ($len < $plusLen || substr($value, $len - $plusLen) != $plusEncoded) { $value .= $plusEncoded; } return $this->getUnboundedLabel($value, $lowerBound); } else { // if no lower bound can be found, this isn't a valid range. in this case // we assume its a translation key and try to translate it. return Piwik_Translate(trim($value)); } } } /** * Beautifies and returns a range label whose range spans over one unit, ie * 1-1, 2-2 or 3-3. * * This function can be overridden in derived types to customize beautifcation * behavior based on the range values. * * @param string $oldLabel The original label value. * @param int $lowerBound The lower bound of the range. * @return string The pretty range label. */ public function getSingleUnitLabel($oldLabel, $lowerBound) { if ($lowerBound == 1) { return $this->labelSingular; } else { return sprintf($this->labelPlural, $lowerBound); } } /** * Beautifies and returns a range label whose range is bounded and spans over * more than one unit, ie 1-5, 5-10 but NOT 11+. * * This function can be overridden in derived types to customize beautifcation * behavior based on the range values. * * @param string $oldLabel The original label value. * @param int $lowerBound The lower bound of the range. * @param int $upperBound The upper bound of the range. * @return string The pretty range label. */ public function getRangeLabel($oldLabel, $lowerBound, $upperBound) { return sprintf($this->labelPlural, $oldLabel); } /** * Beautifies and returns a range label whose range is unbounded, ie * 5+, 10+, etc. * * This function can be overridden in derived types to customize beautifcation * behavior based on the range values. * * @param string $oldLabel The original label value. * @param int $lowerBound The lower bound of the range. * @return string The pretty range label. */ public function getUnboundedLabel($oldLabel, $lowerBound) { return sprintf($this->labelPlural, $oldLabel); } }