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

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Measure/Abstract.php

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