PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-0.7.0/library/Zend/Measure/Viscosity/Kinematic.php

https://github.com/bhaumik25/zend-framework
PHP | 258 lines | 149 code | 29 blank | 80 comment | 13 complexity | dba5027cc9c57f83c846578cfda2b670 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$
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Implement needed classes
  23. */
  24. require_once 'Zend/Measure/Exception.php';
  25. require_once 'Zend/Measure/Abstract.php';
  26. require_once 'Zend/Locale.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Measure
  30. * @subpackage Zend_Measure_Viscosity_Kinematic
  31. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Measure_Viscosity_Kinematic extends Zend_Measure_Abstract
  35. {
  36. // Viscosity_Kinematic definitions
  37. const STANDARD = 'SQUARE_METER_PER_SECOND';
  38. const CENTISTOKES = 'CENTISTOKES';
  39. const LENTOR = 'LENTOR';
  40. const LITER_PER_CENTIMETER_DAY = 'LITER_PER_CENTIMETER_DAY';
  41. const LITER_PER_CENTIMETER_HOUR = 'LITER_PER_CENTIMETER_HOUR';
  42. const LITER_PER_CENTIMETER_MINUTE = 'LITER_PER_CENTIMETER_MINUTE';
  43. const LITER_PER_CENTIMETER_SECOND = 'LITER_PER_CENTIMETER_SECOND';
  44. const POISE_CUBIC_CENTIMETER_PER_GRAM = 'POISE_CUBIC_CENTIMETER_PER_GRAM';
  45. const SQUARE_CENTIMETER_PER_DAY = 'SQUARE_CENTIMETER_PER_DAY';
  46. const SQUARE_CENTIMETER_PER_HOUR = 'SQUARE_CENTIMETER_PER_HOUR';
  47. const SQUARE_CENTIMETER_PER_MINUTE = 'SQUARE_CENTIMETER_PER_MINUTE';
  48. const SQUARE_CENTIMETER_PER_SECOND = 'SQUARE_CENTIMETER_PER_SECOND';
  49. const SQUARE_FOOT_PER_DAY = 'SQUARE_FOOT_PER_DAY';
  50. const SQUARE_FOOT_PER_HOUR = 'SQUARE_FOOT_PER_HOUR';
  51. const SQUARE_FOOT_PER_MINUTE = 'SQUARE_FOOT_PER_MINUTE';
  52. const SQUARE_FOOT_PER_SECOND = 'SQUARE_FOOT_PER_SECOND';
  53. const SQUARE_INCH_PER_DAY = 'SQUARE_INCH_PER_DAY';
  54. const SQUARE_INCH_PER_HOUR = 'SQUARE_INCH_PER_HOUR';
  55. const SQUARE_INCH_PER_MINUTE = 'SQUARE_INCH_PER_MINUTE';
  56. const SQUARE_INCH_PER_SECOND = 'SQUARE_INCH_PER_SECOND';
  57. const SQUARE_METER_PER_DAY = 'SQUARE_METER_PER_DAY';
  58. const SQUARE_METER_PER_HOUR = 'SQUARE_METER_PER_HOUR';
  59. const SQUARE_METER_PER_MINUTE = 'SQUARE_METER_PER_MINUTE';
  60. const SQUARE_METER_PER_SECOND = 'SQUARE_METER_PER_SECOND';
  61. const SQUARE_MILLIMETER_PER_DAY = 'SQUARE_MILLIMETER_PER_DAY';
  62. const SQUARE_MILLIMETER_PER_HOUR = 'SQUARE_MILLIMETER_PER_HOUR';
  63. const SQUARE_MILLIMETER_PER_MINUTE = 'SQUARE_MILLIMETER_PER_MINUTE';
  64. const SQUARE_MILLIMETER_PER_SECOND = 'SQUARE_MILLIMETER_PER_SECOND';
  65. const STOKES = 'STOKES';
  66. private static $_UNITS = array(
  67. 'CENTISTOKES' => array(0.000001, 'cSt'),
  68. 'LENTOR' => array(0.0001, 'lentor'),
  69. 'LITER_PER_CENTIMETER_DAY' => array(array('' => 1, '/' => 864000), 'l/cm day'),
  70. 'LITER_PER_CENTIMETER_HOUR' => array(array('' => 1, '/' => 36000), 'l/cm h'),
  71. 'LITER_PER_CENTIMETER_MINUTE' => array(array('' => 1, '/' => 600), 'l/cm m'),
  72. 'LITER_PER_CENTIMETER_SECOND' => array(0.1, 'l/cm s'),
  73. 'POISE_CUBIC_CENTIMETER_PER_GRAM' => array(0.0001, 'P cm³/g'),
  74. 'SQUARE_CENTIMETER_PER_DAY' => array(array('' => 1, '/' => 864000000),'cm²/day'),
  75. 'SQUARE_CENTIMETER_PER_HOUR' => array(array('' => 1, '/' => 36000000),'cm²/h'),
  76. 'SQUARE_CENTIMETER_PER_MINUTE' => array(array('' => 1, '/' => 600000),'cm²/m'),
  77. 'SQUARE_CENTIMETER_PER_SECOND' => array(0.0001, 'cm²/s'),
  78. 'SQUARE_FOOT_PER_DAY' => array(0.0000010752667, 'ft²/day'),
  79. 'SQUARE_FOOT_PER_HOUR' => array(0.0000258064, 'ft²/h'),
  80. 'SQUARE_FOOT_PER_MINUTE' => array(0.001548384048, 'ft²/m'),
  81. 'SQUARE_FOOT_PER_SECOND' => array(0.09290304, 'ft²/s'),
  82. 'SQUARE_INCH_PER_DAY' => array(7.4671296e-9, 'in²/day'),
  83. 'SQUARE_INCH_PER_HOUR' => array(0.00000017921111, 'in²/h'),
  84. 'SQUARE_INCH_PER_MINUTE' => array(0.000010752667, 'in²/m'),
  85. 'SQUARE_INCH_PER_SECOND' => array(0.00064516, 'in²/s'),
  86. 'SQUARE_METER_PER_DAY' => array(array('' => 1, '/' => 86400), 'm²/day'),
  87. 'SQUARE_METER_PER_HOUR' => array(array('' => 1, '/' => 3600), 'm²/h'),
  88. 'SQUARE_METER_PER_MINUTE' => array(array('' => 1, '/' => 60), 'm²/m'),
  89. 'SQUARE_METER_PER_SECOND' => array(1, 'm²/s'),
  90. 'SQUARE_MILLIMETER_PER_DAY' => array(array('' => 1, '/' => 86400000000), 'mm²/day'),
  91. 'SQUARE_MILLIMETER_PER_HOUR' => array(array('' => 1, '/' => 3600000000), 'mm²/h'),
  92. 'SQUARE_MILLIMETER_PER_MINUTE' => array(array('' => 1, '/' => 60000000), 'mm²/m'),
  93. 'SQUARE_MILLIMETER_PER_SECOND' => array(0.000001, 'mm²/s'),
  94. 'STOKES' => array(0.0001, 'St')
  95. );
  96. private $_Locale = null;
  97. /**
  98. * Zend_Measure_Viscosity_Kinematic provides an locale aware class for
  99. * conversion and formatting of kinematic viscosity values
  100. *
  101. * Zend_Measure $input can be a locale based input string
  102. * or a value. $locale can be used to define that the
  103. * input is made in a different language than the actual one.
  104. *
  105. * @param integer|string $value Value as string, integer, real or float
  106. * @param string $type OPTIONAL A Zend_Measure_Viscosity_Kinematic Type
  107. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
  108. * @throws Zend_Measure_Exception
  109. */
  110. public function __construct($value, $type = null, $locale = null)
  111. {
  112. $this->setValue($value, $type, $locale);
  113. }
  114. /**
  115. * Compare if the value and type is equal
  116. *
  117. * @param Zend_Measure_Viscosity_Kinematic $object Viscosity Kinematic object to compare
  118. * @return boolean
  119. */
  120. public function equals($object)
  121. {
  122. if ($object->toString() == $this->toString()) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * Set a new value
  129. *
  130. * @param integer|string $value Value as string, integer, real or float
  131. * @param string $type OPTIONAL A Zend_Measure_Viscosity_Kinematic Type
  132. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
  133. * @throws Zend_Measure_Exception
  134. */
  135. public function setValue($value, $type = null, $locale = null)
  136. {
  137. if ($locale === null) {
  138. $locale = $this->_Locale;
  139. }
  140. if (!$locale = Zend_Locale::isLocale($locale, true)) {
  141. throw new Zend_Measure_Exception("language ($locale) is a unknown language");
  142. }
  143. if ($type === null) {
  144. $type = self::STANDARD;
  145. }
  146. try {
  147. $value = Zend_Locale_Format::getNumber($value, $locale);
  148. } catch(Exception $e) {
  149. throw new Zend_Measure_Exception($e->getMessage());
  150. }
  151. if (empty( self::$_UNITS[$type] )) {
  152. throw new Zend_Measure_Exception("type ($type) is a unknown kinematic viscosity");
  153. }
  154. parent::setValue($value, $type, $locale);
  155. parent::setType($type);
  156. }
  157. /**
  158. * Set a new type, and convert the value
  159. *
  160. * @param string $type New type to set
  161. * @throws Zend_Measure_Exception
  162. */
  163. public function setType($type)
  164. {
  165. if (empty(self::$_UNITS[$type])) {
  166. throw new Zend_Measure_Exception("type ($type) is a unknown kinematic viscosity");
  167. }
  168. // Convert to standard value
  169. $value = parent::getValue();
  170. if (is_array(self::$_UNITS[parent::getType()][0])) {
  171. foreach (self::$_UNITS[parent::getType()][0] as $key => $found) {
  172. switch ( $key ) {
  173. case "/":
  174. $value /= $found;
  175. break;
  176. default:
  177. $value *= $found;
  178. break;
  179. }
  180. }
  181. } else {
  182. $value = $value * (self::$_UNITS[parent::getType()][0]);
  183. }
  184. // Convert to expected value
  185. if (is_array(self::$_UNITS[$type][0])) {
  186. foreach (self::$_UNITS[$type][0] as $key => $found) {
  187. switch ($key) {
  188. case "/":
  189. $value *= $found;
  190. break;
  191. default:
  192. $value /= $found;
  193. break;
  194. }
  195. }
  196. } else {
  197. $value = $value / (self::$_UNITS[$type][0]);
  198. }
  199. parent::setValue($value, $type, $this->_Locale);
  200. parent::setType( $type );
  201. }
  202. /**
  203. * Returns a string representation
  204. *
  205. * @return string
  206. */
  207. public function toString()
  208. {
  209. return parent::getValue() . ' ' . self::$_UNITS[parent::getType()][1];
  210. }
  211. /**
  212. * Returns a string representation
  213. *
  214. * @return string
  215. */
  216. public function __toString()
  217. {
  218. return $this->toString();
  219. }
  220. /**
  221. * Returns the conversion list
  222. *
  223. * @return array
  224. */
  225. public function getConversionList()
  226. {
  227. return self::$_UNITS;
  228. }
  229. }