public function testStringFormatting() { $this->assertEquals("?", WMUtility::sprintf("%d", null)); $this->assertEquals("1", WMUtility::sprintf("%d", 1)); $this->assertEquals("1y", WMUtility::sprintf("%t", 31536000)); $this->assertEquals("1d", WMUtility::sprintf("%t", 86400)); $this->assertEquals("1h", WMUtility::sprintf("%t", 3600)); $this->assertEquals("5m", WMUtility::sprintf("%t", 300)); $this->assertEquals("1y 1d", WMUtility::sprintf("%t", 31622400)); $this->assertEquals("1y 1d", WMUtility::sprintf("%t", 31622400)); $this->assertEquals("1y 1d 1h", WMUtility::sprintf("%t", 31626000)); $this->assertEquals("1y 1d", WMUtility::sprintf("%2t", 31626000)); $this->assertEquals("1y1d", WMUtility::sprintf("%-2t", 31626000)); $this->assertEquals("1d", WMUtility::sprintf("%T", 8640000)); $this->assertEquals("1K", WMUtility::sprintf("%k", 1000)); $this->assertEquals("1K", WMUtility::sprintf("%k", 1024, 1024)); $this->assertEquals("1M", WMUtility::sprintf("%k", 1000 * 1000)); $this->assertEquals("1M", WMUtility::sprintf("%k", 1024 * 1024, 1024)); $this->assertEquals("1G", WMUtility::sprintf("%k", 1000 * 1000 * 1000)); $this->assertEquals("1G", WMUtility::sprintf("%k", 1024 * 1024 * 1024, 1024)); $this->assertEquals("1T", WMUtility::sprintf("%k", 1000 * 1000 * 1000 * 1000)); $this->assertEquals("1T", WMUtility::sprintf("%k", 1024 * 1024 * 1024 * 1024, 1024)); $this->assertEquals("2.4T", WMUtility::sprintf("%k", 2.4 * 1024 * 1024 * 1024 * 1024, 1024)); }
/** * Given a token from ProcessString(), and the context for it, figure out the actual value and format @inheritdoc * * @param $include_notes * @param $keyContents * @param $key * @param $contextDescription * @param $value * @return array */ private function processStringToken($include_notes, $keyContents, $key, $context) { $value = ""; $contextDescription = $this->getContextDescription($context); $parts = explode(":", $keyContents); $type = array_shift($parts); $args = join(":", $parts); $partCount = count($parts); if ($partCount > 0 && $type == 'map') { $theItem = $this; $args = $parts[0]; $format = isset($parts[1]) ? $parts[1] : ""; } if ($partCount > 1 && ($type == 'link' || $type == 'node')) { $itemName = $parts[0]; $args = $parts[1]; $format = isset($parts[2]) ? $parts[2] : ""; $theItem = $this->processStringFindReferredObject($context, $itemName, $type); } if (is_null($theItem)) { wm_warn("ProcessString: {$key} refers to unknown item (context is {$contextDescription}) [WMWARN05]\n"); return ""; } wm_debug("ProcessString: Found appropriate item: {$theItem}\n"); $value = $this->findItemValue($theItem, $args, $value, $include_notes); // format, and sanitise the value string here, before returning it wm_debug("ProcessString: replacing %s with %s \n", $key, $value); if ($format != '') { $value = WMUtility::sprintf($format, $value, $this->kilo); wm_debug("ProcessString: formatted {$format} to {$value}\n"); } return $value; }