Skip to content

Commit

Permalink
Added filesize filter, strftime filter accepts tstamp
Browse files Browse the repository at this point in the history
  • Loading branch information
tbreuss committed Dec 21, 2014
1 parent 97380c7 commit c0e875b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Twig/HerbieExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected function renderError($message)
public function getFilters()
{
return [
new Twig_SimpleFilter('filesize', [$this, 'filterFilesize'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('markdown', [$this, 'filterMarkdown'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('strftime', [$this, 'filterStrftime']),
new Twig_SimpleFilter('textile', [$this, 'filterTextile'], ['is_safe' => ['html']]),
Expand Down Expand Up @@ -167,6 +168,26 @@ public function getTests()
];
}

/**
* @param integer $size
* @return string
*/
public function filterFilesize($size)
{
if( $size <= 0 ) {
return '0';
}
if( $size === 1 ) {
return '1 Byte';
}
$mod = 1024;
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
for( $i = 0; $size > $mod && $i < count($units) - 1; ++$i ) {
$size /= $mod;
}
return str_replace(',', '.', round($size, 1)) . ' ' . $units[$i];
}

/**
* @param string $content
* @return string
Expand All @@ -184,6 +205,10 @@ public function filterMarkdown($content)
*/
public function filterStrftime($date, $format = '%x')
{
// timestamp?
if(is_numeric($date)) {
$date = date('Y-m-d H:i:s', $date);
}
$dateTime = new \DateTime($date);
return strftime($format, $dateTime->getTimestamp());
}
Expand Down

0 comments on commit c0e875b

Please sign in to comment.