PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/DevApp/library/Zend/Measure/Abstract.php

http://firephp.googlecode.com/
PHP | 357 lines | 187 code | 42 blank | 128 comment | 31 complexity | 352958c42a57e965bac1b604d28cb30b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Measure
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Abstract.php 9795 2008-06-26 20:54:38Z thomas $
  20. */
  21. require_once 'Zend/Locale.php';
  22. require_once 'Zend/Locale/Math.php';
  23. require_once 'Zend/Locale/Format.php';
  24. /**
  25. * Abstract class for all measurements
  26. *
  27. * @category Zend
  28. * @package Zend_Measure
  29. * @subpackage Zend_Measure_Abstract
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Measure_Abstract
  34. {
  35. /**
  36. * Plain value in standard unit
  37. *
  38. * @var string $_value
  39. */
  40. protected $_value;
  41. /**
  42. * Original type for this unit
  43. *
  44. * @var string $_type
  45. */
  46. protected $_type;
  47. /**
  48. * Locale identifier
  49. *
  50. * @var string $_locale
  51. */
  52. protected $_locale = null;
  53. /**
  54. * Unit types for this measurement
  55. */
  56. protected $_units = array();
  57. /**
  58. * Zend_Measure_Abstract is an abstract class for the different measurement types
  59. *
  60. * @param $value mixed - Value as string, integer, real or float
  61. * @param $type type - OPTIONAL a Zend_Measure_Area Type
  62. * @param $locale locale - OPTIONAL a Zend_Locale Type
  63. * @throws Zend_Measure_Exception
  64. */
  65. public function __construct($value, $type = null, $locale = null)
  66. {
  67. if (Zend_Locale::isLocale($type)) {
  68. $locale = $type;
  69. $type = null;
  70. }
  71. if ($locale === null) {
  72. $locale = new Zend_Locale();
  73. }
  74. if ($locale instanceof Zend_Locale) {
  75. $locale = $locale->toString();
  76. }
  77. if (!$this->_locale = Zend_Locale::isLocale($locale, true)) {
  78. require_once 'Zend/Measure/Exception.php';
  79. throw new Zend_Measure_Exception("Language ($locale) is unknown");
  80. }
  81. $this->_locale = $locale;
  82. if ($type === null) {
  83. $type = $this->_units['STANDARD'];
  84. }
  85. if (isset($this->_units[$type]) === false) {
  86. require_once 'Zend/Measure/Exception.php';
  87. throw new Zend_Measure_Exception("Type ($type) is unknown");
  88. }
  89. $this->setValue($value, $type, $this->_locale);
  90. }
  91. /**
  92. * Returns the internal value
  93. *
  94. * @param integer $round (Optional) Rounds the value to an given precision,
  95. * Default is 2, -1 returns without rounding
  96. */
  97. public function getValue($round = 2)
  98. {
  99. if ($round < 0) {
  100. return $this->_value;
  101. }
  102. return Zend_Locale_Math::round($this->_value, $round);
  103. }
  104. /**
  105. * Set a new value
  106. *
  107. * @param integer|string $value Value as string, integer, real or float
  108. * @param string $type OPTIONAL A Zend_Measure_Acceleration Type
  109. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
  110. * @throws Zend_Measure_Exception
  111. */
  112. public function setValue($value, $type = null, $locale = null)
  113. {
  114. if (Zend_Locale::isLocale($type)) {
  115. $locale = $type;
  116. $type = null;
  117. }
  118. if ($locale === null) {
  119. $locale = $this->_locale;
  120. }
  121. if ($locale instanceof Zend_Locale) {
  122. $locale = $locale->toString();
  123. }
  124. if (!Zend_Locale::isLocale($locale)) {
  125. require_once 'Zend/Measure/Exception.php';
  126. throw new Zend_Measure_Exception("Language ($locale) is unknown");
  127. }
  128. if ($type === null) {
  129. $type = $this->_units['STANDARD'];
  130. }
  131. if (empty($this->_units[$type])) {
  132. require_once 'Zend/Measure/Exception.php';
  133. throw new Zend_Measure_Exception("Type ($type) is unknown");
  134. }
  135. try {
  136. $value = Zend_Locale_Format::getNumber($value, array('locale' => $locale));
  137. } catch(Exception $e) {
  138. require_once 'Zend/Measure/Exception.php';
  139. throw new Zend_Measure_Exception($e->getMessage());
  140. }
  141. $this->_value = $value;
  142. $this->setType($type);
  143. }
  144. /**
  145. * Returns the original type
  146. *
  147. * @return type
  148. */
  149. public function getType()
  150. {
  151. return $this->_type;
  152. }
  153. /**
  154. * Set a new type, and convert the value
  155. *
  156. * @param string $type New type to set
  157. * @throws Zend_Measure_Exception
  158. */
  159. public function setType($type)
  160. {
  161. if (empty($this->_units[$type])) {
  162. require_once 'Zend/Measure/Exception.php';
  163. throw new Zend_Measure_Exception("Type ($type) is unknown");
  164. }
  165. if (empty($this->_type)) {
  166. $this->_type = $type;
  167. } else {
  168. // Convert to standard value
  169. $value = $this->getValue(-1);
  170. if (is_array($this->_units[$this->getType()][0])) {
  171. foreach ($this->_units[$this->getType()][0] as $key => $found) {
  172. switch ($key) {
  173. case "/":
  174. if ($found != 0) {
  175. $value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  176. }
  177. break;
  178. case "+":
  179. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  180. break;
  181. case "-":
  182. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  183. break;
  184. default:
  185. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  186. break;
  187. }
  188. }
  189. } else {
  190. $value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
  191. }
  192. // Convert to expected value
  193. if (is_array($this->_units[$type][0])) {
  194. foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
  195. switch ($key) {
  196. case "/":
  197. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  198. break;
  199. case "+":
  200. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  201. break;
  202. case "-":
  203. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  204. break;
  205. default:
  206. if ($found != 0) {
  207. $value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  208. }
  209. break;
  210. }
  211. }
  212. } else {
  213. $value = @call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
  214. }
  215. $this->_value = $value;
  216. $this->_type = $type;
  217. }
  218. }
  219. /**
  220. * Compare if the value and type is equal
  221. *
  222. * @param Zend_Measure_Detailtype $object object to compare
  223. * @return boolean
  224. */
  225. public function equals($object)
  226. {
  227. if ($object->toString() == $this->toString()) {
  228. return true;
  229. }
  230. return false;
  231. }
  232. /**
  233. * Returns a string representation
  234. *
  235. * @param integer $round OPTIONAL rounds the value to an given exception
  236. * @return string
  237. */
  238. public function toString($round = -1)
  239. {
  240. return $this->getValue($round) . ' ' . $this->_units[$this->getType()][1];
  241. }
  242. /**
  243. * Returns a string representation
  244. *
  245. * @return string
  246. */
  247. public function __toString()
  248. {
  249. return $this->toString();
  250. }
  251. /**
  252. * Returns the conversion list
  253. *
  254. * @return array
  255. */
  256. public function getConversionList()
  257. {
  258. return $this->_units;
  259. }
  260. /**
  261. * Alias function for setType returning the converted unit
  262. *
  263. * @param $type type
  264. * @param $round integer OPTIONAL rounds the value to a given precision
  265. * @return string
  266. */
  267. public function convertTo($type, $round = 2)
  268. {
  269. $this->setType($type);
  270. return $this->toString($round);
  271. }
  272. /**
  273. * Adds an unit to another one
  274. *
  275. * @param $object object of same unit type
  276. * @return Zend_Measure object
  277. */
  278. public function add($object)
  279. {
  280. $object->setType($this->getType());
  281. $value = $this->getValue(-1) + $object->getValue(-1);
  282. $this->setValue($value, $this->getType(), $this->_locale);
  283. return $this;
  284. }
  285. /**
  286. * Substracts an unit from another one
  287. *
  288. * @param $object object of same unit type
  289. * @return Zend_Measure object
  290. */
  291. public function sub($object)
  292. {
  293. $object->setType($this->getType());
  294. $value = $this->getValue(-1) - $object->getValue(-1);
  295. $this->setValue($value, $this->getType(), $this->_locale);
  296. return $this;
  297. }
  298. /**
  299. * Compares two units
  300. *
  301. * @param $object object of same unit type
  302. * @return boolean
  303. */
  304. public function compare($object)
  305. {
  306. $object->setType($this->getType());
  307. $value = $this->getValue(-1) - $object->getValue(-1);
  308. if ($value < 0) {
  309. return -1;
  310. } else if ($value > 0) {
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. }