PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ railoapacheportable/php/PEAR/Zend/Measure/Abstract.php

http://railoapacheportable.googlecode.com/
PHP | 412 lines | 213 code | 47 blank | 152 comment | 36 complexity | fd97ca0177d4d4ccbb2e0cad2ee8556a MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, EPL-1.0, LGPL-3.0, GPL-3.0, AGPL-1.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, CC-BY-SA-3.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-2009 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 16220 2009-06-21 19:49:21Z 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-2009 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. $this->setLocale($locale);
  81. if ($type === null) {
  82. $type = $this->_units['STANDARD'];
  83. }
  84. if (isset($this->_units[$type]) === false) {
  85. require_once 'Zend/Measure/Exception.php';
  86. throw new Zend_Measure_Exception("Type ($type) is unknown");
  87. }
  88. $this->setValue($value, $type, $this->_locale);
  89. }
  90. /**
  91. * Returns the actual set locale
  92. *
  93. * @return string
  94. */
  95. public function getLocale()
  96. {
  97. return $this->_locale;
  98. }
  99. /**
  100. * Sets a new locale for the value representation
  101. *
  102. * @param string|Zend_Locale $locale (Optional) New locale to set
  103. * @param boolean $check False, check but don't set; True, set the new locale
  104. * @return Zend_Measure_Abstract
  105. */
  106. public function setLocale($locale = null, $check = false)
  107. {
  108. if (empty($locale)) {
  109. require_once 'Zend/Registry.php';
  110. if (Zend_Registry::isRegistered('Zend_Locale') === true) {
  111. $locale = Zend_Registry::get('Zend_Locale');
  112. }
  113. }
  114. if ($locale === null) {
  115. $locale = new Zend_Locale();
  116. }
  117. if (!Zend_Locale::isLocale($locale, true, false)) {
  118. if (!Zend_Locale::isLocale($locale, false, false)) {
  119. require_once 'Zend/Measure/Exception.php';
  120. throw new Zend_Measure_Exception("Language (" . (string) $locale . ") is unknown");
  121. }
  122. $locale = new Zend_Locale($locale);
  123. }
  124. if (!$check) {
  125. $this->_locale = (string) $locale;
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Returns the internal value
  131. *
  132. * @param integer $round (Optional) Rounds the value to an given precision,
  133. * Default is -1 which returns without rounding
  134. * @param string|Zend_Locale $locale (Optional) Locale for number representation
  135. */
  136. public function getValue($round = -1, $locale = null)
  137. {
  138. if ($round < 0) {
  139. $return = $this->_value;
  140. } else {
  141. $return = Zend_Locale_Math::round($this->_value, $round);
  142. }
  143. if ($locale !== null) {
  144. $this->setLocale($locale, true);
  145. return Zend_Locale_Format::toNumber($return, array('locale' => $locale));
  146. }
  147. return $return;
  148. }
  149. /**
  150. * Set a new value
  151. *
  152. * @param integer|string $value Value as string, integer, real or float
  153. * @param string $type OPTIONAL A Zend_Measure_Acceleration Type
  154. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
  155. * @throws Zend_Measure_Exception
  156. */
  157. public function setValue($value, $type = null, $locale = null)
  158. {
  159. if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
  160. $locale = $type;
  161. $type = null;
  162. }
  163. if ($locale === null) {
  164. $locale = $this->_locale;
  165. }
  166. $this->setLocale($locale, true);
  167. if ($type === null) {
  168. $type = $this->_units['STANDARD'];
  169. }
  170. if (empty($this->_units[$type])) {
  171. require_once 'Zend/Measure/Exception.php';
  172. throw new Zend_Measure_Exception("Type ($type) is unknown");
  173. }
  174. try {
  175. $value = Zend_Locale_Format::getNumber($value, array('locale' => $locale));
  176. } catch(Exception $e) {
  177. require_once 'Zend/Measure/Exception.php';
  178. throw new Zend_Measure_Exception($e->getMessage());
  179. }
  180. $this->_value = $value;
  181. $this->setType($type);
  182. return $this;
  183. }
  184. /**
  185. * Returns the original type
  186. *
  187. * @return type
  188. */
  189. public function getType()
  190. {
  191. return $this->_type;
  192. }
  193. /**
  194. * Set a new type, and convert the value
  195. *
  196. * @param string $type New type to set
  197. * @throws Zend_Measure_Exception
  198. */
  199. public function setType($type)
  200. {
  201. if (empty($this->_units[$type])) {
  202. require_once 'Zend/Measure/Exception.php';
  203. throw new Zend_Measure_Exception("Type ($type) is unknown");
  204. }
  205. if (empty($this->_type)) {
  206. $this->_type = $type;
  207. } else {
  208. // Convert to standard value
  209. $value = $this->_value;
  210. $prec = 0;
  211. if (strpos($this->_value, '.') !== false) {
  212. $prec = strlen(substr($this->_value, strpos($this->_value, '.') + 1));
  213. }
  214. if (is_array($this->_units[$this->getType()][0])) {
  215. foreach ($this->_units[$this->getType()][0] as $key => $found) {
  216. switch ($key) {
  217. case "/":
  218. if ($found != 0) {
  219. $value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  220. }
  221. break;
  222. case "+":
  223. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  224. break;
  225. case "-":
  226. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  227. break;
  228. default:
  229. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  230. break;
  231. }
  232. }
  233. } else {
  234. $value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
  235. }
  236. // Convert to expected value
  237. if (is_array($this->_units[$type][0])) {
  238. foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
  239. switch ($key) {
  240. case "/":
  241. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  242. break;
  243. case "+":
  244. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  245. break;
  246. case "-":
  247. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  248. break;
  249. default:
  250. if ($found != 0) {
  251. $value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  252. }
  253. break;
  254. }
  255. }
  256. } else {
  257. $value = @call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
  258. }
  259. $this->_value = Zend_Locale_Math::round($value, $prec);
  260. $this->_type = $type;
  261. }
  262. return $this;
  263. }
  264. /**
  265. * Compare if the value and type is equal
  266. *
  267. * @param Zend_Measure_Detailtype $object object to compare
  268. * @return boolean
  269. */
  270. public function equals($object)
  271. {
  272. if ((string) $object == $this->toString()) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. /**
  278. * Returns a string representation
  279. *
  280. * @param integer $round (Optional) Runds the value to an given exception
  281. * @param string|Zend_Locale $locale (Optional) Locale to set for the number
  282. * @return string
  283. */
  284. public function toString($round = -1, $locale = null)
  285. {
  286. if ($locale === null) {
  287. $locale = $this->_locale;
  288. }
  289. return $this->getValue($round, $locale) . ' ' . $this->_units[$this->getType()][1];
  290. }
  291. /**
  292. * Returns a string representation
  293. *
  294. * @return string
  295. */
  296. public function __toString()
  297. {
  298. return $this->toString();
  299. }
  300. /**
  301. * Returns the conversion list
  302. *
  303. * @return array
  304. */
  305. public function getConversionList()
  306. {
  307. return $this->_units;
  308. }
  309. /**
  310. * Alias function for setType returning the converted unit
  311. *
  312. * @param string $type Constant Type
  313. * @param integer $round (Optional) Rounds the value to a given precision
  314. * @param string|Zend_Locale $locale (Optional) Locale to set for the number
  315. * @return string
  316. */
  317. public function convertTo($type, $round = 2, $locale = null)
  318. {
  319. $this->setType($type);
  320. return $this->toString($round, $locale);
  321. }
  322. /**
  323. * Adds an unit to another one
  324. *
  325. * @param $object object of same unit type
  326. * @return Zend_Measure object
  327. */
  328. public function add($object)
  329. {
  330. $object->setType($this->getType());
  331. $value = $this->getValue(-1) + $object->getValue(-1);
  332. $this->setValue($value, $this->getType(), $this->_locale);
  333. return $this;
  334. }
  335. /**
  336. * Substracts an unit from another one
  337. *
  338. * @param $object object of same unit type
  339. * @return Zend_Measure object
  340. */
  341. public function sub($object)
  342. {
  343. $object->setType($this->getType());
  344. $value = $this->getValue(-1) - $object->getValue(-1);
  345. $this->setValue($value, $this->getType(), $this->_locale);
  346. return $this;
  347. }
  348. /**
  349. * Compares two units
  350. *
  351. * @param $object object of same unit type
  352. * @return boolean
  353. */
  354. public function compare($object)
  355. {
  356. $object->setType($this->getType());
  357. $value = $this->getValue(-1) - $object->getValue(-1);
  358. if ($value < 0) {
  359. return -1;
  360. } else if ($value > 0) {
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. }