PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/num.php

https://bitbucket.org/arkross/venus
PHP | 338 lines | 137 code | 37 blank | 164 comment | 13 complexity | 5727d5830cf9cfd967e3b9faf244f1cd MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Numeric helper class. Provides additional formatting methods for working with
  15. * numeric values.
  16. *
  17. * Credit is left where credit is due.
  18. *
  19. * Techniques and inspiration were taken from all over, including:
  20. * Kohana Framework: kohanaframework.org
  21. * CakePHP: cakephp.org
  22. *
  23. * @package Fuel
  24. * @category Core
  25. * @author Chase "Syntaqx" Hutchins
  26. */
  27. class Num
  28. {
  29. /**
  30. * Cached byte units
  31. *
  32. * @var array
  33. */
  34. protected static $byte_units;
  35. /**
  36. * Cached configuration values
  37. *
  38. * @var array
  39. */
  40. protected static $config;
  41. /**
  42. * Class initialization callback
  43. *
  44. * @return void
  45. */
  46. public static function _init()
  47. {
  48. \Lang::load('byte_units', true);
  49. static::$config = \Config::load('num', true);
  50. static::$byte_units = \Lang::get('byte_units');
  51. }
  52. /**
  53. * Converts a file size number to a byte value. File sizes are defined in
  54. * the format: SB, where S is the size (1, 8.5, 300, etc.) and B is the
  55. * byte unit (K, MiB, GB, etc.). All valid byte units are defined in
  56. * static::$byte_units
  57. *
  58. * Usage:
  59. * <code>
  60. * echo Num::bytes('200K'); // 204800
  61. * echo static::bytes('5MiB'); // 5242880
  62. * echo static::bytes('1000'); // 1000
  63. * echo static::bytes('2.5GB'); // 2684354560
  64. * </code>
  65. *
  66. * @author Kohana Team
  67. * @copyright (c) 2009-2011 Kohana Team
  68. * @license http://kohanaframework.org/license
  69. * @param string file size in SB format
  70. * @return float
  71. */
  72. public static function bytes($size = 0)
  73. {
  74. // Prepare the size
  75. $size = trim((string) $size);
  76. // Construct an OR list of byte units for the regex
  77. $accepted = implode('|', array_keys(static::$byte_units));
  78. // Construct the regex pattern for verifying the size format
  79. $pattern = '/^([0-9]+(?:\.[0-9]+)?)('.$accepted.')?$/Di';
  80. // Verify the size format and store the matching parts
  81. if (!preg_match($pattern, $size, $matches))
  82. {
  83. throw new Exception('The byte unit size, "'.$size.'", is improperly formatted.');
  84. }
  85. // Find the float value of the size
  86. $size = (float) $matches[1];
  87. // Find the actual unit, assume B if no unit specified
  88. $unit = Arr::element($matches, 2, 'B');
  89. // Convert the size into bytes
  90. $bytes = $size * pow(2, static::$byte_units[$unit]);
  91. return $bytes;
  92. }
  93. /**
  94. * Converts a number of bytes to a human readable number by taking the
  95. * number of that unit that the bytes will go into it. Supports TB value.
  96. *
  97. * Note: Integers in PHP are limited to 32 bits, unless they are on 64 bit
  98. * architectures, then they have 64 bit size. If you need to place the
  99. * larger size then what the PHP integer type will hold, then use a string.
  100. * It will be converted to a double, which should always have 64 bit length.
  101. *
  102. * @param integer
  103. * @param integer
  104. * @return boolean|string
  105. */
  106. public static function format_bytes($bytes = 0, $decimals = 0)
  107. {
  108. $quant = array(
  109. 'TB' => 1099511627776, // pow( 1024, 4)
  110. 'GB' => 1073741824, // pow( 1024, 3)
  111. 'MB' => 1048576, // pow( 1024, 2)
  112. 'kB' => 1024, // pow( 1024, 1)
  113. 'B ' => 1, // pow( 1024, 0)
  114. );
  115. foreach ($quant as $unit => $mag )
  116. {
  117. if (doubleval($bytes) >= $mag)
  118. {
  119. return sprintf('%01.'.$decimals.'f', ($bytes / $mag)).' '.$unit;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * Converts a number into a more readable human-type number.
  126. *
  127. * Usage:
  128. * <code>
  129. * echo Num::quantity(7000); // 7K
  130. * echo Num::quantity(7500); // 8K
  131. * echo Num::quantity(7500, 1); // 7.5K
  132. * </code>
  133. *
  134. * @param integer
  135. * @param integer
  136. * @return string
  137. */
  138. public static function quantity($num, $decimals = 0)
  139. {
  140. if ($num >= 1000 && $num < 1000000)
  141. {
  142. return sprintf('%01.'.$decimals.'f', (sprintf('%01.0f', $num) / 1000)).'K';
  143. }
  144. elseif ($num >= 1000000 && $num < 1000000000)
  145. {
  146. return sprintf('%01.'.$decimals.'f', (sprintf('%01.0f', $num) / 1000000)).'M';
  147. }
  148. elseif ($num >= 1000000000)
  149. {
  150. return sprintf('%01.'.$decimals.'f', (sprintf('%01.0f', $num) / 1000000000)).'B';
  151. }
  152. return $num;
  153. }
  154. /**
  155. * Formats a number by injecting non-numeric characters in a specified
  156. * format into the string in the positions they appear in the format.
  157. *
  158. * Usage:
  159. * <code>
  160. * echo Num::format('1234567890', '(000) 000-0000'); // (123) 456-7890
  161. * echo Num::format('1234567890', '000.000.0000'); // 123.456.7890
  162. * </code>
  163. *
  164. * @link http://snippets.symfony-project.org/snippet/157
  165. * @param string the string to format
  166. * @param string the format to apply
  167. * @return string
  168. */
  169. public static function format($string = '', $format = '')
  170. {
  171. if(empty($format) or empty($string))
  172. {
  173. return $string;
  174. }
  175. $result = '';
  176. $fpos = 0;
  177. $spos = 0;
  178. while ((strlen($format) - 1) >= $fpos)
  179. {
  180. if (ctype_alnum(substr($format, $fpos, 1)))
  181. {
  182. $result .= substr($string, $spos, 1);
  183. $spos++;
  184. }
  185. else
  186. {
  187. $result .= substr($format, $fpos, 1);
  188. }
  189. $fpos++;
  190. }
  191. return $result;
  192. }
  193. /**
  194. * Transforms a number by masking characters in a specified mask format, and
  195. * ignoring characters that should be injected into the string without
  196. * matching a character from the original string (defaults to space).
  197. *
  198. * Usage:
  199. * <code>
  200. * echo Num::mask_string('1234567812345678', '************0000'); ************5678
  201. * echo Num::mask_string('1234567812345678', '**** **** **** 0000'); // **** **** **** 5678
  202. * echo Num::mask_string('1234567812345678', '**** - **** - **** - 0000', ' -'); // **** - **** - **** - 5678
  203. * </code>
  204. *
  205. * @link http://snippets.symfony-project.org/snippet/157
  206. * @param string the string to transform
  207. * @param string the mask format
  208. * @param string a string (defaults to a single space) containing characters to ignore in the format
  209. * @return string the masked string
  210. */
  211. public static function mask_string($string = '', $format = '', $ignore = ' ')
  212. {
  213. if(empty($format) or empty($string))
  214. {
  215. return $string;
  216. }
  217. $result = '';
  218. $fpos = 0;
  219. $spos = 0;
  220. while ((strlen($format) - 1) >= $fpos)
  221. {
  222. if (ctype_alnum(substr($format, $fpos, 1)))
  223. {
  224. $result .= substr($string, $spos, 1);
  225. $spos++;
  226. }
  227. else
  228. {
  229. $result .= substr($format, $fpos, 1);
  230. if (strpos($ignore, substr($format, $fpos, 1)) === false)
  231. {
  232. ++$spos;
  233. }
  234. }
  235. ++$fpos;
  236. }
  237. return $result;
  238. }
  239. /**
  240. * Formats a phone number.
  241. *
  242. * @link http://snippets.symfony-project.org/snippet/157
  243. * @param string the unformatted phone number to format
  244. * @param string the format to use, defaults to '(000) 000-0000'
  245. * @return string the formatted string
  246. * @see format
  247. */
  248. public static function format_phone($string = '', $format = null)
  249. {
  250. is_null($format) and $format = static::$config['formatting']['phone'];
  251. return static::format($string, $format);
  252. }
  253. /**
  254. * Formats a variable length phone number, using a standard format.
  255. *
  256. * Usage:
  257. * <code>
  258. * echo Num::smart_format_phone('1234567'); // 123-4567
  259. * echo Num::smart_format_phone('1234567890'); // (123) 456-7890
  260. * echo Num::smart_format_phone('91234567890'); // 9 (123) 456-7890
  261. * echo Num::smart_format_phone('123456'); // => 123456
  262. * </code>
  263. *
  264. * @param string the unformatted phone number to format
  265. * @see format
  266. */
  267. public static function smart_format_phone($string)
  268. {
  269. $formats = static::$config['formatting']['smart_phone'];
  270. if(is_array($formats) and isset($formats[strlen($string)]))
  271. {
  272. return static::format($string, $formats[strlen($string)]);
  273. }
  274. return $string;
  275. }
  276. /**
  277. * Formats a credit card expiration string. Expects 4-digit string (MMYY).
  278. *
  279. * @param string the unformatted expiration string to format
  280. * @param string the format to use, defaults to '00-00'
  281. * @see format
  282. */
  283. public static function format_exp($string, $format = null)
  284. {
  285. is_null($format) and $format = static::$config['formatting']['exp'];
  286. return static::format($string, $format);
  287. }
  288. /**
  289. * Formats (masks) a credit card.
  290. *
  291. * @param string the unformatted credit card number to format
  292. * @param string the format to use, defaults to '**** **** **** 0000'
  293. * @see mask_string
  294. */
  295. public static function mask_credit_card($string, $format = null)
  296. {
  297. is_null($format) and $format = static::$config['formatting']['credit_card'];
  298. return static::mask_string($string, $format);
  299. }
  300. }
  301. /* End of file num.php */