0,0 → 1,96 |
<?php |
class Formatter |
{ |
function integer($s, $def = '0') |
{ |
if($s === null) { |
return $def; |
} |
else if(!is_int($s)) { |
throw new Exception("Not an integer"); |
} |
else { |
return '' . $s; |
} |
} |
|
function string($s, $def = '') |
{ |
if(!$s) return $def; |
|
return htmlspecialchars($s); |
} |
|
function stringBegin($s, $len, $cont = '...', $def = '') |
{ |
if(!$s) return $def; |
|
if(strlen($s) > $len) { |
return $this->string(substr($s, 0, $len) . $cont, $def); |
} |
else { |
return $this->string($s, $def); |
} |
} |
|
function urlPart($s, $def = '') |
{ |
if(!$s) return $def; |
|
return urlencode($s); |
} |
|
function htmlUrlPart($s, $def = '') |
{ |
return $this->string($this->urlPart($s, $def)); |
} |
|
function date($d, $def = '') |
{ |
if(!$d) { |
return $def; |
} |
else if($d instanceof DateTime) { |
return date_format($d, 'd.m.Y H:i:s'); |
} |
else if(is_string($d)) { |
return $this->string($d, $def); |
} |
else { |
return $def; |
} |
} |
|
function size($s, $def = '') |
{ |
if(!$s) { |
return $def; |
} |
else if($s < 1024) { |
return $s . ' B'; |
} |
else if($s < 1024 * 1024) { |
return round($s / 1024 * 100) / 100 . ' KB'; |
} |
else if($s < 1024 * 1024 * 1024) { |
return round($s / 1024 / 1024 * 100) / 100 . ' MB'; |
} |
else { |
return round($s / 1024 / 1024 / 1024 * 100) / 100 . ' GB'; |
} |
} |
|
function boolean($b, $true, $false, $def = '') |
{ |
if($b === null) { |
return $def; |
} |
else if($b) { |
return $true; |
} |
else { |
return $false; |
} |
} |
} |
?> |