PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZfcDatagrid/Column/Formatter/FileSize.php

https://github.com/fYnatIn/ZfcDatagrid
PHP | 55 lines | 36 code | 9 blank | 10 comment | 4 complexity | 009b9e7436b445dcb566e03588f3d268 MD5 | raw file
Possible License(s): WTFPL
  1. <?php
  2. namespace ZfcDatagrid\Column\Formatter;
  3. use ZfcDatagrid\Column\AbstractColumn;
  4. class FileSize extends AbstractFormatter
  5. {
  6. /**
  7. * We implement isApply here ourself, because it's always valid!
  8. *
  9. * @var unknown
  10. */
  11. protected $validRenderers = array();
  12. protected static $prefixes = array(
  13. '',
  14. 'K',
  15. 'M',
  16. 'G',
  17. 'T',
  18. 'P',
  19. 'E',
  20. 'Z',
  21. 'Y'
  22. );
  23. public function isApply()
  24. {
  25. return true;
  26. }
  27. /**
  28. * The value should be in bytes
  29. *
  30. * @see \ZfcDatagrid\Column\Formatter\AbstractFormatter::getFormattedValue()
  31. */
  32. public function getFormattedValue(AbstractColumn $column)
  33. {
  34. $row = $this->getRowData();
  35. $value = $row[$column->getUniqueId()];
  36. if ($value == '') {
  37. return $value;
  38. }
  39. $index = 0;
  40. while ($value >= 1024 && $index < count(self::$prefixes)) {
  41. $value = $value / 1024;
  42. $index ++;
  43. }
  44. return sprintf('%1.2f %sB', $value, self::$prefixes[$index]);
  45. }
  46. }