Abstract.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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$
  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(), $e->getCode(), $e);
  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. if (is_array($this->_units[$this->getType()][0])) {
  211. foreach ($this->_units[$this->getType()][0] as $key => $found) {
  212. switch ($key) {
  213. case "/":
  214. if ($found != 0) {
  215. $value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  216. }
  217. break;
  218. case "+":
  219. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  220. break;
  221. case "-":
  222. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  223. break;
  224. default:
  225. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  226. break;
  227. }
  228. }
  229. } else {
  230. $value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
  231. }
  232. // Convert to expected value
  233. if (is_array($this->_units[$type][0])) {
  234. foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
  235. switch ($key) {
  236. case "/":
  237. $value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
  238. break;
  239. case "+":
  240. $value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
  241. break;
  242. case "-":
  243. $value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
  244. break;
  245. default:
  246. if ($found != 0) {
  247. $value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
  248. }
  249. break;
  250. }
  251. }
  252. } else {
  253. $value = @call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
  254. }
  255. $slength = strlen($value);
  256. $length = 0;
  257. for($i = 1; $i <= 25; ++$i) {
  258. if ($value[$slength - $i] != '0') {
  259. $length = 26 - $i;
  260. break;
  261. }
  262. }
  263. $this->_value = Zend_Locale_Math::round($value, $length);
  264. $this->_type = $type;
  265. }
  266. return $this;
  267. }
  268. /**
  269. * Compare if the value and type is equal
  270. *
  271. * @param Zend_Measure_Detailtype $object object to compare
  272. * @return boolean
  273. */
  274. public function equals($object)
  275. {
  276. if ((string) $object == $this->toString()) {
  277. return true;
  278. }
  279. return false;
  280. }
  281. /**
  282. * Returns a string representation
  283. *
  284. * @param integer $round (Optional) Runds the value to an given exception
  285. * @param string|Zend_Locale $locale (Optional) Locale to set for the number
  286. * @return string
  287. */
  288. public function toString($round = -1, $locale = null)
  289. {
  290. if ($locale === null) {
  291. $locale = $this->_locale;
  292. }
  293. return $this->getValue($round, $locale) . ' ' . $this->_units[$this->getType()][1];
  294. }
  295. /**
  296. * Returns a string representation
  297. *
  298. * @return string
  299. */
  300. public function __toString()
  301. {
  302. return $this->toString();
  303. }
  304. /**
  305. * Returns the conversion list
  306. *
  307. * @return array
  308. */
  309. public function getConversionList()
  310. {
  311. return $this->_units;
  312. }
  313. /**
  314. * Alias function for setType returning the converted unit
  315. *
  316. * @param string $type Constant Type
  317. * @param integer $round (Optional) Rounds the value to a given precision
  318. * @param string|Zend_Locale $locale (Optional) Locale to set for the number
  319. * @return string
  320. */
  321. public function convertTo($type, $round = 2, $locale = null)
  322. {
  323. $this->setType($type);
  324. return $this->toString($round, $locale);
  325. }
  326. /**
  327. * Adds an unit to another one
  328. *
  329. * @param $object object of same unit type
  330. * @return Zend_Measure object
  331. */
  332. public function add($object)
  333. {
  334. $object->setType($this->getType());
  335. $value = $this->getValue(-1) + $object->getValue(-1);
  336. $this->setValue($value, $this->getType(), $this->_locale);
  337. return $this;
  338. }
  339. /**
  340. * Substracts an unit from another one
  341. *
  342. * @param $object object of same unit type
  343. * @return Zend_Measure object
  344. */
  345. public function sub($object)
  346. {
  347. $object->setType($this->getType());
  348. $value = $this->getValue(-1) - $object->getValue(-1);
  349. $this->setValue($value, $this->getType(), $this->_locale);
  350. return $this;
  351. }
  352. /**
  353. * Compares two units
  354. *
  355. * @param $object object of same unit type
  356. * @return boolean
  357. */
  358. public function compare($object)
  359. {
  360. $object->setType($this->getType());
  361. $value = $this->getValue(-1) - $object->getValue(-1);
  362. if ($value < 0) {
  363. return -1;
  364. } else if ($value > 0) {
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. }