Source for file ByteSize.class.php

Documentation is available at ByteSize.class.php

  1. <?php
  2.  
  3. /**
  4. * @since 7/14/05
  5. * @package harmoni.primitives.numbers
  6. *
  7. * @copyright Copyright &copy; 2005, Middlebury College
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  9. *
  10. * @version $Id: ByteSize.class.php,v 1.5 2007/09/04 20:25:28 adamfranco Exp $
  11. */
  12.  
  13. require_once(dirname(__FILE__)."/Integer.class.php");
  14.  
  15. /**
  16. * A representation of a Byte size. Provides easy conversion between B, KB, MB, etc
  17. * as well as a pretty string reprentation
  18. *
  19. * @package harmoni.primitives.numbers
  20. *
  21. * @copyright Copyright &copy; 2005, Middlebury College
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  23. *
  24. * @version $Id: ByteSize.class.php,v 1.5 2007/09/04 20:25:28 adamfranco Exp $
  25. */
  26. class ByteSize
  27. extends Integer
  28. {
  29.  
  30. /*******************************************************
  31. * Class Methods
  32. *********************************************************/
  33.  
  34. /**
  35. * Answer the string suffix for the desired muliple of 2^10 bytes
  36. * i.e. 0 -> B, 10 -> kB, 20 -> MB, 30 -> GB, etc.
  37. *
  38. * @param integer $power A multiple of 10; Range, 0-80
  39. * @return string
  40. * @access public
  41. * @since 10/11/05
  42. */
  43. function suffixForPower ($power) {
  44. $multiple = intval($power/10);
  45. if ($multiple < 0 || $multiple > 8)
  46. throwError(new Error("Invalid power, $power. Valid values are multiples of ten, 0-80.",
  47. "ByteSize", true));
  48. $suffixes = array("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  49. return $suffixes[$multiple];
  50. }
  51. /*******************************************************
  52. * Class Methods - Instance Creation
  53. *********************************************************/
  54.  
  55.  
  56. /**
  57. * Answer a new object with the string value specified.
  58. *
  59. * @param string $stringValue String representation of the size
  60. * @param optional string $class The class to instantiate. Do NOT use outside
  61. * of this package.
  62. * @return object ByteSize
  63. * @access public
  64. * @since 7/14/05
  65. */
  66. function fromString ( $stringValue, $class = 'ByteSize') {
  67. if (preg_match("/([0-9\.]+)\s*(B|kB|MB|GB|TB|PB|EB|ZB|YB)/i",
  68. $stringValue, $matches))
  69. {
  70. $suffixes = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  71. $bytes = $matches[1] * pow(2,
  72. (10 * array_search(strtoupper($matches[2]), $suffixes)));
  73. } else if (preg_match("/^[0-9]+$/", $stringValue)) {
  74. $bytes = intval($stringValue);
  75. } else {
  76. print "not found, going with 0";
  77. $bytes = 0;
  78. }
  79. eval('$result = '.$class.'::withValue($bytes, $class);');
  80. return $result;
  81. }
  82. /**
  83. * Answer a new object with the value specified
  84. *
  85. * @param integer $value Integer number of Bytes
  86. * @param optional string $class The class to instantiate. Do NOT use outside
  87. * of this package.
  88. * @return object ByteSize
  89. * @access public
  90. * @since 7/14/05
  91. */
  92. function withValue ( $value, $class = 'ByteSize') {
  93. return parent::withValue($value, $class);
  94. }
  95. /**
  96. * Answer a new object with the value zero
  97. *
  98. * @param optional string $class The class to instantiate. Do NOT use outside
  99. * of this package.
  100. * @return object ByteSize
  101. * @access public
  102. * @since 7/14/05
  103. */
  104. function zero ( $class = 'ByteSize') {
  105. return parent::zero($class);
  106. }
  107. /*******************************************************
  108. * Instance Methods - Printing
  109. *********************************************************/
  110.  
  111. /**
  112. * Answer a String whose characters are a description of the receiver.
  113. * Override this method as needed to provide a better representation
  114. *
  115. * @return string
  116. * @access public
  117. * @since 7/11/05
  118. */
  119. function printableString () {
  120. for ($i = 0; $i <= 80; $i = $i + 10) {
  121. if ($this->value() < pow(2, ($i + 10)))
  122. break;
  123. }
  124. if ($i == 0)
  125. $numString = $this->multipleOfPowerOf2($i);
  126. else
  127. $numString = sprintf("%01.2f", $this->multipleOfPowerOf2($i));
  128. return $numString." ".$this->suffixForPower($i);
  129. }
  130. /**
  131. * Answer the string in kilo bytes (kB)
  132. *
  133. * @return string
  134. * @access public
  135. * @since 10/11/05
  136. */
  137. function asKBString () {
  138. return round($this->multipleOfPowerOf2(10), 2).$this->suffixForPower(10);
  139. }
  140. /**
  141. * Answer the string in mega bytes (MB)
  142. *
  143. * @return string
  144. * @access public
  145. * @since 10/11/05
  146. */
  147. function asMBString () {
  148. return round($this->multipleOfPowerOf2(20), 2).$this->suffixForPower(20);
  149. }
  150.  
  151. /*******************************************************
  152. * Instance Methods - Accessing
  153. *********************************************************/
  154.  
  155. /**
  156. * Answer the PHP primitive value of the reciever.
  157. * We need to store our internal representation as a float to allow for
  158. * very large integers, but we never want to return a decimal number of
  159. * bytes.
  160. *
  161. * @return integer
  162. * @access public
  163. * @since 7/14/05
  164. */
  165. function value () {
  166. return round($this->_value, 0);
  167. }
  168. /**
  169. * Answer the value as a multiple of 2^$power.
  170. * Ex: for the number in kilo bytes (in computer-terms), $power = 10;
  171. *
  172. * @param integer $power 0 for bytes, 10 for kilobyes, 20 for megabytes, etc.
  173. * @return float
  174. * @access public
  175. * @since 10/11/05
  176. */
  177. function multipleOfPowerOf2 ( $power ) {
  178. return $this->value() / pow(2, $power);
  179. }
  180.  
  181. /**
  182. * Answer the value in Kilo Bytes KB
  183. *
  184. * @return integer
  185. * @access public
  186. * @since 10/11/05
  187. */
  188. function kiloBytes () {
  189. return $this->multipleOfPowerOf2(10);
  190. }
  191.  
  192. /**
  193. * Answer the value in Mega Bytes (MB)
  194. *
  195. * @return integer
  196. * @access public
  197. * @since 10/11/05
  198. */
  199. function megaBytes () {
  200. return $this->multipleOfPowerOf2(20);
  201. }
  202. /*******************************************************
  203. * Instance Methods - Private
  204. *********************************************************/
  205.  
  206. /**
  207. * Set the internal value to a PHP primitive.
  208. *
  209. * @param mixed $value
  210. * @return void
  211. * @access private
  212. * @since 7/14/05
  213. */
  214. function _setValue ( $value ) {
  215. $this->_value = $value;
  216. }
  217. }

Documentation generated on Wed, 19 Sep 2007 10:21:56 -0400 by phpDocumentor 1.3.0RC3