PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/zend/1.11.9/library/Zend/Measure/Abstract.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 418 lines | 217 code | 46 blank | 155 comment | 38 complexity | 791c74f824ed2aa67f514e4bc09815a6 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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 mixed $value Value as string, integer, real or float
  70. * @param int $type OPTIONAL a measure type f.e. Zend_Measure_Length::METER
  71. * @param Zend_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. * @return integer|string
  136. */
  137. public function getValue($round = -1, $locale = null)
  138. {
  139. if ($round < 0) {
  140. $return = $this->_value;
  141. } else {
  142. $return = Zend_Locale_Math::round($this->_value, $round);
  143. }
  144. if ($locale !== null) {
  145. $this->setLocale($locale, true);
  146. return Zend_Locale_Format::toNumber($return, array('locale' => $locale));
  147. }
  148. return $return;
  149. }
  150. /**
  151. * Set a new value
  152. *
  153. * @param integer|string $value Value as string, integer, real or float
  154. * @param string $type OPTIONAL A measure type f.e. Zend_Measure_Length::METER
  155. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
  156. * @throws Zend_Measure_Exception
  157. * @return Zend_Measure_Abstract
  158. */
  159. public function setValue($value, $type = null, $locale = null)
  160. {
  161. if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
  162. $locale = $type;
  163. $type = null;
  164. }
  165. if ($locale === null) {
  166. $locale = $this->_locale;
  167. }
  168. $this->setLocale($locale, true);
  169. if ($type === null) {
  170. $type = $this->_units['STANDARD'];
  171. }
  172. if (empty($this->_units[$type])) {
  173. require_once 'Zend/Measure/Exception.php';
  174. throw new Zend_Measure_Exception("Type ($type) is unknown");
  175. }
  176. try {
  177. $value = Zend_Locale_Format::getNumber($value, array('locale' => $locale));
  178. } catch(Exception $e) {
  179. require_once 'Zend/Measure/Exception.php';
  180. throw new Zend_Measure_Exception($e->getMessage(), $e->getCode(), $e);
  181. }
  182. $this->_value = $value;
  183. $this->setType($type);
  184. return $this;
  185. }
  186. /**
  187. * Returns the original type
  188. *
  189. * @return type
  190. */
  191. public function getType()
  192. {
  193. return $this->_type;
  194. }
  195. /**
  196. * Set a new type, and convert the value
  197. *
  198. * @param string $type New type to set
  199. * @throws Zend_Measure_Exception
  200. * @return Zend_Measure_Abstract
  201. */
  202. public function setType($type)
  203. {
  204. if (empty($this->_units[$type])) {
  205. require_once 'Zend/Measure/Exception.php';
  206. throw new Zend_Measure_Exception("Type ($type) is unknown");
  207. }
  208. if (empty($this->_type)) {
  209. $this->_type = $type;
  210. } else {
  211. // Convert to standard value
  212. $value = $this->_value;
  213. if (is_array($this->_units[$this->getType()][0])) {
  214. foreach ($this->_units[$this->getType()][0] as $key => $found) {
  215. switch ($key) {
  216. case "/":
  217. if ($found != 0) {
  218. $value = call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  219. }
  220. break;
  221. case "+":
  222. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  223. break;
  224. case "-":
  225. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  226. break;
  227. default:
  228. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  229. break;
  230. }
  231. }
  232. } else {
  233. $value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
  234. }
  235. // Convert to expected value
  236. if (is_array($this->_units[$type][0])) {
  237. foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
  238. switch ($key) {
  239. case "/":
  240. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  241. break;
  242. case "+":
  243. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  244. break;
  245. case "-":
  246. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  247. break;
  248. default:
  249. if ($found != 0) {
  250. $value = call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  251. }
  252. break;
  253. }
  254. }
  255. } else {
  256. $value = call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
  257. }
  258. $slength = strlen($value);
  259. $length = 0;
  260. for($i = 1; $i <= $slength; ++$i) {
  261. if ($value[$slength - $i] != '0') {
  262. $length = 26 - $i;
  263. break;
  264. }
  265. }
  266. $this->_value = Zend_Locale_Math::round($value, $length);
  267. $this->_type = $type;
  268. }
  269. return $this;
  270. }
  271. /**
  272. * Compare if the value and type is equal
  273. *
  274. * @param Zend_Measure_Abstract $object object to compare
  275. * @return boolean
  276. */
  277. public function equals($object)
  278. {
  279. if ((string) $object == $this->toString()) {
  280. return true;
  281. }
  282. return false;
  283. }
  284. /**
  285. * Returns a string representation
  286. *
  287. * @param integer $round (Optional) Runds the value to an given exception
  288. * @param string|Zend_Locale $locale (Optional) Locale to set for the number
  289. * @return string
  290. */
  291. public function toString($round = -1, $locale = null)
  292. {
  293. if ($locale === null) {
  294. $locale = $this->_locale;
  295. }
  296. return $this->getValue($round, $locale) . ' ' . $this->_units[$this->getType()][1];
  297. }
  298. /**
  299. * Returns a string representation
  300. *
  301. * @return string
  302. */
  303. public function __toString()
  304. {
  305. return $this->toString();
  306. }
  307. /**
  308. * Returns the conversion list
  309. *
  310. * @return array
  311. */
  312. public function getConversionList()
  313. {
  314. return $this->_units;
  315. }
  316. /**
  317. * Alias function for setType returning the converted unit
  318. *
  319. * @param string $type Constant Type
  320. * @param integer $round (Optional) Rounds the value to a given precision
  321. * @param string|Zend_Locale $locale (Optional) Locale to set for the number
  322. * @return string
  323. */
  324. public function convertTo($type, $round = 2, $locale = null)
  325. {
  326. $this->setType($type);
  327. return $this->toString($round, $locale);
  328. }
  329. /**
  330. * Adds an unit to another one
  331. *
  332. * @param Zend_Measure_Abstract $object object of same unit type
  333. * @return Zend_Measure_Abstract
  334. */
  335. public function add($object)
  336. {
  337. $object->setType($this->getType());
  338. $value = $this->getValue(-1) + $object->getValue(-1);
  339. $this->setValue($value, $this->getType(), $this->_locale);
  340. return $this;
  341. }
  342. /**
  343. * Substracts an unit from another one
  344. *
  345. * @param Zend_Measure_Abstract $object object of same unit type
  346. * @return Zend_Measure_Abstract
  347. */
  348. public function sub($object)
  349. {
  350. $object->setType($this->getType());
  351. $value = $this->getValue(-1) - $object->getValue(-1);
  352. $this->setValue($value, $this->getType(), $this->_locale);
  353. return $this;
  354. }
  355. /**
  356. * Compares two units
  357. *
  358. * @param Zend_Measure_Abstract $object object of same unit type
  359. * @return boolean
  360. */
  361. public function compare($object)
  362. {
  363. $object->setType($this->getType());
  364. $value = $this->getValue(-1) - $object->getValue(-1);
  365. if ($value < 0) {
  366. return -1;
  367. } else if ($value > 0) {
  368. return 1;
  369. }
  370. return 0;
  371. }
  372. }