Merge branch 'develop' of git+ssh://git.sihnon.net/home/git/public/sihnon-php-lib into develop
This commit is contained in:
@@ -32,6 +32,12 @@ class SihnonFramework_Config {
|
|||||||
*/
|
*/
|
||||||
const TYPE_STRING_LIST = 'array(string)';
|
const TYPE_STRING_LIST = 'array(string)';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hash type with string keys and mixed-type values
|
||||||
|
* @var array(string=>mixed)
|
||||||
|
*/
|
||||||
|
const TYPE_HASH = 'hash';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Backend to be used for this Config object
|
* Backend to be used for this Config object
|
||||||
* @var Sihnon_Config_IPlugin
|
* @var Sihnon_Config_IPlugin
|
||||||
@@ -58,9 +64,11 @@ class SihnonFramework_Config {
|
|||||||
|
|
||||||
protected static function pack($type, $value) {
|
protected static function pack($type, $value) {
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case static::TYPE_STRING_LIST: {
|
case static::TYPE_STRING_LIST:
|
||||||
return join("\n", $value);
|
return join("\n", $value);
|
||||||
} break;
|
|
||||||
|
case static::TYPE_HASH:
|
||||||
|
return join("\n", array_map(function($k, $v) { return "{$k}:{$v}"; }, array_keys($value), array_values($value)));
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
return $value;
|
return $value;
|
||||||
@@ -70,9 +78,17 @@ class SihnonFramework_Config {
|
|||||||
|
|
||||||
protected static function unpack($type, $value) {
|
protected static function unpack($type, $value) {
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case self::TYPE_STRING_LIST:
|
case static::TYPE_STRING_LIST:
|
||||||
|
// foo
|
||||||
|
// bar
|
||||||
return array_map('trim', explode("\n", $value));
|
return array_map('trim', explode("\n", $value));
|
||||||
|
|
||||||
|
case static::TYPE_HASH:
|
||||||
|
// foo:bar
|
||||||
|
// baz:quz
|
||||||
|
preg_match_all("/^([^:]+):(.+)$/m", $value, $pairs);
|
||||||
|
return array_combine($pairs[1], $pairs[2]);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -333,32 +333,74 @@ class SihnonFramework_Main {
|
|||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function formatDuration($time) {
|
public static function formatDuration($seconds, $fuzziness = 0) {
|
||||||
if (is_null($time)) {
|
if (is_null($seconds)) {
|
||||||
return 'unknown';
|
return 'indeterminate time';
|
||||||
}
|
}
|
||||||
|
|
||||||
$labels = array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years');
|
$labels = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
|
||||||
$limits = array(1, 60, 3600, 86400, 604800, 2592000, 31556926, PHP_INT_MAX);
|
$pluralLabels = array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years');
|
||||||
|
$limits = array(1, 60, 3600, 86400, 604800, 2592000, 31556926, PHP_INT_MAX);
|
||||||
|
$components = array(0, 0, 0, 0, 0, 0, 0);
|
||||||
|
|
||||||
$working_time = $time;
|
$workingTime = $seconds;
|
||||||
|
|
||||||
$result = "";
|
$result = "";
|
||||||
$ptr = count($labels) - 1;
|
$ptr = count($labels) - 1;
|
||||||
|
|
||||||
while ($ptr >= 0 && $working_time < $limits[$ptr]) {
|
while ($ptr >= 0 && $workingTime < $limits[$ptr]) {
|
||||||
--$ptr;
|
--$ptr;
|
||||||
}
|
}
|
||||||
|
$mostSignificantPtr = $ptr;
|
||||||
|
|
||||||
while ($ptr >= 0) {
|
// Convert the value into components using the remaining labels
|
||||||
$unit_time = floor($working_time / $limits[$ptr]);
|
while ($ptr >= 0) {
|
||||||
$working_time -= $unit_time * $limits[$ptr];
|
$unitTime = floor($workingTime / $limits[$ptr]);
|
||||||
$result = $result . ' ' . $unit_time . ' ' . $labels[$ptr];
|
$workingTime -= $unitTime * $limits[$ptr];
|
||||||
--$ptr;
|
$components[$ptr] = $unitTime;
|
||||||
}
|
--$ptr;
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
$componentsUsed = 0;
|
||||||
}
|
$approximate = false;
|
||||||
|
$lastComponent = false;
|
||||||
|
$ptr = $mostSignificantPtr;
|
||||||
|
while ($ptr >= 0) {
|
||||||
|
if ($fuzziness && $componentsUsed >= $fuzziness) {
|
||||||
|
break;
|
||||||
|
} elseif ($ptr == 0 || ($fuzziness && $componentsUsed == $fuzziness-1)) {
|
||||||
|
$lastComponent = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$component = $components[$ptr];
|
||||||
|
if ($component) {
|
||||||
|
// If we're going to hide the next value, take its component into account here
|
||||||
|
if ($lastComponent && $ptr > 0) {
|
||||||
|
$component += round($components[$ptr-1] / $limits[$ptr]);
|
||||||
|
$approximate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($lastComponent && $ptr < $mostSignificantPtr) {
|
||||||
|
$result .= ' and';
|
||||||
|
}
|
||||||
|
|
||||||
|
$result .= ' ' . $component . ' ' . ($component == 1 ? $labels[$ptr] : $pluralLabels[$ptr]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment even if we've hidden this component because it's zero
|
||||||
|
// Then we don't end up with overly precise times like '2 years and 1 second'
|
||||||
|
++$componentsUsed;
|
||||||
|
|
||||||
|
--$ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($approximate) {
|
||||||
|
$result = 'approximately ' . $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
public static function formatFilesize($bytes) {
|
public static function formatFilesize($bytes) {
|
||||||
if (is_null($bytes)) {
|
if (is_null($bytes)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user