Overview

Namespaces

  • Rundiz
    • Number

Classes

  • Rundiz\Number\Number
  • Rundiz\Number\NumberEng
  • Rundiz\Number\NumberThai
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /** 
  3:  * Number class.
  4:  * 
  5:  * @package Number
  6:  * @version 1.1.4
  7:  * @author Vee W.
  8:  * @license http://opensource.org/licenses/MIT
  9:  * 
 10:  */
 11: 
 12: namespace Rundiz\Number;
 13: 
 14: /**
 15:  * number class for convert number, convert file size from bytes to other or from other to bytes.
 16:  * 
 17:  * @since 1.1
 18:  */
 19: class Number
 20: {
 21: 
 22: 
 23:     /**
 24:      * byte units.
 25:      * 
 26:      * @var array match the byte units (B, KB, KiB, ...) to conversion number.
 27:      * @link https://en.wikipedia.org/wiki/File_size reference for conversion number. 
 28:      */
 29:     protected $byte_units = array(
 30:         'B' => 1,
 31:         'KiB' => 1024,
 32:         'KB' => 1000,
 33:         'MiB' => 1048576,
 34:         'MB' => 1000000,
 35:         'GiB' => 1073741824,
 36:         'GB' => 1000000000,
 37:         'TiB' => 1099511627776,
 38:         'TB' => 1000000000000,
 39:         'PiB' => 1125899906842624,
 40:         'PB' => 1000000000000000,
 41:         'EiB' => 1152921504606846976,
 42:         'EB' => 1000000000000000000,
 43:         'ZiB' => 1180591620717411303424,
 44:         'ZB' => 1000000000000000000000,
 45:         'YiB' => 1208925819614629174706176,
 46:         'YB' => 1000000000000000000000000,
 47:     );
 48: 
 49: 
 50:     /**
 51:      * A shorter way to convert Baht to different languages in one class. You no need to initialize `new Number[Language]()` class just `new Number()`.<br>
 52:      * You may need to require/include the Number[Language].php file if you don't use composer.
 53:      * 
 54:      * @param number $num number integer or decimal. negative or positive.
 55:      * @param boolean $display_net display net (ถ้วน). true to display, false to not display. Usually this is only works with Thai language.
 56:      * @param string $language the language you want to use. accepted Thai, Eng. this will be use in new Number[Language] object automatically.
 57:      */
 58:     public function convertBaht($num, $display_net = true, $language = 'Thai')
 59:     {
 60:         $number_lang_class_name = 'Rundiz\\Number\\Number'.$language;
 61: 
 62:         if (class_exists($number_lang_class_name)) {
 63:             $numlang = new $number_lang_class_name();
 64: 
 65:             if (method_exists($numlang, 'convertBaht')) {
 66:                 $output = $numlang->convertBaht($num, $display_net);
 67:             } else {
 68:                 $output = $numlang->convertNumber($num);
 69:                 if ($language == 'Eng') {
 70:                     $output .= ' Baht';
 71:                 }
 72:             }
 73: 
 74:             unset($number_lang_class_name, $numlang);
 75:             return $output;
 76:         }
 77: 
 78:         unset($number_lang_class_name);
 79:     }// convertBaht
 80: 
 81: 
 82:     /**
 83:      * A shorter way to convert number to different languages in one class. You no need to initialize `new Number[Language]()` class just `new Number()`.<br>
 84:      * You may need to require/include the Number[Language].php file if you don't use composer.
 85:      * 
 86:      * @param number $num number integer or decimal. negative or positive.
 87:      * @param string $language the language you want to use. accepted Thai, Eng. this will be use in new Number[Language] object automatically.
 88:      */
 89:     public function convertNumber($num, $language = 'Thai')
 90:     {
 91:         $number_lang_class_name = 'Rundiz\\Number\\Number'.$language;
 92: 
 93:         if (class_exists($number_lang_class_name)) {
 94:             $numlang = new $number_lang_class_name();
 95:             $output = $numlang->convertNumber($num);
 96:             unset($number_lang_class_name, $numlang);
 97:             return $output;
 98:         }
 99: 
100:         unset($number_lang_class_name);
101:     }// convertNumber
102: 
103: 
104:     /**
105:      * convert file size from bytes to the unit you specified or automatically detect.
106:      * 
107:      * @param integer $size file size in bytes only.
108:      * @param string $unit unit you want to convert. accepted values are AUTO, B, KB, KiB, ... more please refer to byte_units property.
109:      * @param integer $decimal number of decimal.
110:      * @return mixed return converted file size as string if success, return false if failed to convert.
111:      */
112:     public function fromBytes($size, $unit = 'AUTO', $decimal = 2)
113:     {
114:         if (!is_numeric($size)) {
115:             return false;
116:         }
117:         $size = (string) $size;
118: 
119:         if ($unit == null) {
120:             $unit = 'AUTO';
121:         }
122: 
123:         $decimal = intval($decimal);
124:         $output = false;
125: 
126:         if ($unit != 'AUTO') {
127:             if (array_key_exists($unit, $this->byte_units)) {
128:                 $size_unit_conversion = (string) $this->byte_units[$unit];
129:             } else {
130:                 $unit = 'B';
131:                 $size_unit_conversion = (string) 1;
132:             }
133: 
134:             $output = bcdiv($size, $size_unit_conversion, $decimal).' '.$unit;
135:         } else {
136:             $byte_units = array_reverse($this->byte_units);
137:             foreach ($byte_units as $size_unit => $size_unit_conversion) {
138:                 $size_unit_conversion = (string) $size_unit_conversion;
139:                 if ($size >= $size_unit_conversion && $unit == 'AUTO') {
140:                     $output = bcdiv($size, $size_unit_conversion, $decimal).' '.$size_unit;
141:                     break;
142:                 }
143:             }
144:         }
145: 
146:         unset($byte_units, $size_unit, $size_unit_conversion);
147:         return $output;
148:     }// fromBytes
149: 
150: 
151:     /**
152:      * convert file size to bytes.
153:      * 
154:      * @param string $size enter file size string such as 105 or 105B for 105 Bytes, 1KB for 1 Kilobyte, 1KiB for 1 Kibibyte. file size unit can be *B or *iB for binary and decimal.
155:      * @return mixed return false if failed to convert. return number if it is able to convert.
156:      */
157:     public function toBytes($size)
158:     {
159:         $pattern = '/^([0-9]+(?:\.[0-9]+)?)('.implode('|', array_keys($this->byte_units)).')?$/Di';
160: 
161:         if (!preg_match($pattern, trim($size), $matches)) {
162:             // unable to convert to byte because input size does not match pattern. (xxxB, xxxKB, xxxKiB, ...)
163:             return false;
164:         }
165:         unset($pattern);
166: 
167:         $size_number = 0;
168:         if (isset($matches[1])) {
169:             $size_number = $matches[1];
170:         } else {
171:             return false;
172:         }
173: 
174:         $size_unit = 'B';
175:         if (isset($matches[2])) {
176:             $size_unit = $matches[2];
177:         }
178: 
179:         $size_unit_conversion = 1;
180:         if (array_key_exists($size_unit, $this->byte_units)) {
181:             $size_unit_conversion = $this->byte_units[$size_unit];
182:         }
183:         unset($size_unit);
184: 
185:         $bytes = $size_number*$size_unit_conversion;
186:         unset($size_number, $size_unit_conversion);
187:         return $bytes;
188:     }// toBytes
189: 
190: 
191: }
API documentation generated by ApiGen