/library/Zend/Measure/Abstract.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 436 lines · 217 code · 53 blank · 166 comment · 38 complexity · ed58d95c9fc1d080dc7f88e4e1e57c0f MD5 · raw file

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