Convert.php

Go to the documentation of this file.
00001 <?php
00002 
00033 class Robo47_Convert
00034 {
00035 
00045     public static function shortHandToBytes($value)
00046     {
00047         if (is_numeric($value)) {
00048             if ($value >= PHP_INT_MAX) {
00049                 $message = 'input is greater than PHP_INT_MAX on this ' .
00050                     'plattform (' . PHP_INT_MAX . ')';
00051                 throw new Robo47_Convert_Exception($message);
00052             }
00053             return (int) $value;
00054         } else {
00055             $lastSign = strtolower($value{strlen($value) - 1});
00056             $valueBytes = (int) mb_substr($value, 0, strlen($value) - 1);
00057             switch ($lastSign) {
00058                 case 'k':
00059                     $valueBytes *= 1024;
00060                     break;
00061                 case 'm':
00062                     $valueBytes *= 1024 * 1024;
00063                     break;
00064                 case 'g':
00065                     $valueBytes *= 1024 * 1024 * 1024;
00066                     break;
00067                 default:
00068                     $message = 'invalid last sign in ' . $value;
00069                     throw new Robo47_Convert_Exception($message);
00070                     break;
00071             }
00072             if ($valueBytes >= PHP_INT_MAX) {
00073                 $message = 'input is greater than PHP_INT_MAX on this ' .
00074                     'plattform (' . PHP_INT_MAX . ')';
00075                 throw new Robo47_Convert_Exception($message);
00076             }
00077             return $valueBytes;
00078         }
00079     }
00080 }