/inc/app/sitesearch/lib/Zend/Measure/Abstract.php

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