Mercurial > hg > isophonics-drupal-site
annotate core/lib/Drupal/Component/Utility/Bytes.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 4c8ae668cc8c |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\Component\Utility; |
Chris@0 | 4 |
Chris@0 | 5 /** |
Chris@0 | 6 * Provides helper methods for byte conversions. |
Chris@0 | 7 */ |
Chris@0 | 8 class Bytes { |
Chris@0 | 9 |
Chris@0 | 10 /** |
Chris@0 | 11 * The number of bytes in a kilobyte. |
Chris@0 | 12 * |
Chris@0 | 13 * @see http://wikipedia.org/wiki/Kilobyte |
Chris@0 | 14 */ |
Chris@0 | 15 const KILOBYTE = 1024; |
Chris@0 | 16 |
Chris@0 | 17 /** |
Chris@0 | 18 * Parses a given byte size. |
Chris@0 | 19 * |
Chris@0 | 20 * @param mixed $size |
Chris@0 | 21 * An integer or string size expressed as a number of bytes with optional SI |
Chris@0 | 22 * or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes). |
Chris@0 | 23 * |
Chris@0 | 24 * @return int |
Chris@0 | 25 * An integer representation of the size in bytes. |
Chris@0 | 26 */ |
Chris@0 | 27 public static function toInt($size) { |
Chris@0 | 28 // Remove the non-unit characters from the size. |
Chris@0 | 29 $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); |
Chris@0 | 30 // Remove the non-numeric characters from the size. |
Chris@0 | 31 $size = preg_replace('/[^0-9\.]/', '', $size); |
Chris@0 | 32 if ($unit) { |
Chris@0 | 33 // Find the position of the unit in the ordered string which is the power |
Chris@0 | 34 // of magnitude to multiply a kilobyte by. |
Chris@0 | 35 return round($size * pow(self::KILOBYTE, stripos('bkmgtpezy', $unit[0]))); |
Chris@0 | 36 } |
Chris@0 | 37 else { |
Chris@0 | 38 return round($size); |
Chris@0 | 39 } |
Chris@0 | 40 } |
Chris@0 | 41 |
Chris@0 | 42 } |