Conversion.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php namespace Abhimanyusharma003\Conversion;
  2. class Conversion {
  3. /**
  4. * @var array
  5. */
  6. protected static $conversionChart = array(
  7. // Acceleration
  8. 'METRE_PER_SECOND_SQUARED' => 1,
  9. // Angle
  10. 'TURN' => 6.28318531,
  11. 'RADIAN' => 1,
  12. 'DEGREE' => 0.0174532925,
  13. 'GRADIAN' => 0.015707963267949,
  14. // Area
  15. 'SQUARE_METRE' => 1,
  16. 'HECTARE' => 100,
  17. 'SQUARE_KILOMETRE' => 1000000,
  18. 'SQUARE_INCH' => 0.00064516,
  19. 'SQUARE_FEET' => 0.09290304,
  20. 'SQUARE_YARD' => 0.83612736,
  21. 'ACRE' => 247.105,
  22. 'SQUARE_MILE' => 2589988.110336,
  23. // Storage
  24. 'BIT' => 0.00012207,
  25. 'BYTE' => 0.000976563,
  26. 'KILOBIT' => 0.125,
  27. 'KILOBYTE' => 1,
  28. 'MEGABIT' => 128,
  29. 'MEGABYTE' => 1024,
  30. 'GIGABIT' => 131072,
  31. 'GIGABYTE' => 1.049e+6,
  32. 'TERABIT' => 1.342e+8,
  33. 'TERABYTE' => 1.074e+9,
  34. 'PETABIT' => 1.374e+11,
  35. 'PETABYTE' => 1.1e+12,
  36. // Current
  37. 'STATAMPERE' => 3.33564e-10,
  38. 'MICROAMPERE' => 1e-06,
  39. 'MILLIAMPERE' => 0.001,
  40. 'AMPERE' => 1,
  41. 'ABAMPERE' => 10,
  42. 'KILOAMPERE' => 1000,
  43. // Fuel
  44. 'KILOMETRES_PER_LITRE' => 1,
  45. 'LITRE_PER_100_KILOMETRES' => 100,
  46. 'MILES_PER_GALLON' => 0.354006,
  47. 'US_MILES_PER_GALLON' => 0.425144,
  48. // Length
  49. 'MILLIMETRE' => 0.001,
  50. 'CENTIMETRE' => 0.01,
  51. 'METRE' => 1,
  52. 'KILOMETRE' => 1000,
  53. 'INCH' => 0.0254,
  54. 'FOOT' => 0.3048,
  55. 'YARD' => 0.9144,
  56. 'MILE' => 1609.34,
  57. 'NAUTICAL_MILE' => 1852,
  58. // Mass
  59. 'MICROGRAM' => 1e-7,
  60. 'MILLIGRAM' => 1e-6,
  61. 'GRAM' => 0.001,
  62. 'KILOGRAM' => 1,
  63. 'METRIC_TON' => 100,
  64. 'OUNCE' => 0.0283495,
  65. 'POUND' => 0.453592,
  66. 'STONE' => 6.35029,
  67. 'SHORT_TON' => 907.185,
  68. 'LONG_TON' => 1016.0469088,
  69. // Pressure
  70. 'PASCAL' => 1,
  71. 'KILOPASCAL' => 1000,
  72. 'MEGAPASCAL' => 1e6,
  73. 'BAR' => 1e5,
  74. 'MILLIMETRES_OF_MERCURY' => 133.3224,
  75. 'INCHES_OF_MERCURY' => 3.386389e3,
  76. 'POUNDS_PER_SQUARE_INCH' => 6.894757e3,
  77. 'ATMOSPHERE' => 101325,
  78. // Speed
  79. 'METRE_PER_SECOND' => 1,
  80. 'KILOMETRES_PER_HOUR' => 0.277778,
  81. 'FEET_PER_SECOND' => 0.3048,
  82. 'MILES_PER_HOUR' => 0.44704,
  83. 'KNOT' => 0.514444,
  84. // Temperature
  85. 'CELSIUS' => 273.15,
  86. 'FAHRENHEIT' => 255.372,
  87. 'KELVIN' => 1,
  88. // Time
  89. 'NANOSECOND' => 1e-9,
  90. 'MICROSECOND' => 1e-6,
  91. 'MILLISECOND' => 0.001,
  92. 'SECOND' => 1,
  93. 'MINUTE' => 60,
  94. 'HOUR' => 3600,
  95. 'DAY' => 86400,
  96. 'WEEK' => 604800,
  97. 'MONTH' => 2.62974e6,
  98. 'YEAR' => 3.15569e7,
  99. 'DECADE' => 3.15569e8,
  100. 'CENTURY' => 3.15569e9,
  101. 'MILLENIUM' => 3.1556926e10,
  102. // Voltage
  103. 'VOLT' => 1,
  104. 'KILOVOLT' => 1000,
  105. // Volume
  106. 'MILLILITRE' => 1e-6,
  107. 'LITRE' => 0.001,
  108. 'CUBIC_METRE' => 1,
  109. 'GALLON' => 0.00454609,
  110. 'QUART' => 0.00113652,
  111. 'PINT' => 0.000568261,
  112. 'TABLESPOON' => 1.7758e-5,
  113. 'TEASPOON' => 5.9194e-6,
  114. 'US_GALLON' => 0.00378541,
  115. 'US_QUART' => 0.000946353,
  116. 'US_PINT' => 0.000473176,
  117. 'US_CUP' => 0.000236588,
  118. 'US_OUNCE' => 2.9574e-5,
  119. 'US_TABLESPOON' => 1.4787e-5,
  120. 'US_TEASPOON' => 4.9289e-6,
  121. 'CUBIC_INCH' => 1.6387e-5,
  122. 'CUBIC_FOOT' => 0.0283168,
  123. );
  124. /**
  125. * @var string
  126. */
  127. protected $value;
  128. /**
  129. * @var string
  130. */
  131. protected $unit;
  132. /**
  133. * @var
  134. */
  135. protected static $native;
  136. /**
  137. * @var null
  138. */
  139. protected $number = null;
  140. /**
  141. * @var
  142. */
  143. protected $type;
  144. /**
  145. * @param string $quantity
  146. * @param string $unit
  147. */
  148. public function __construct($quantity = '', $unit = '')
  149. {
  150. $this->unit = $unit;
  151. $this->value = $quantity;
  152. }
  153. /**
  154. * @param $quanity
  155. * @param $unit
  156. * @return Conversion
  157. */
  158. public function convert($quanity, $unit)
  159. {
  160. return new Conversion($quanity, $unit);
  161. }
  162. /**
  163. * @param $unit
  164. * @return $this
  165. */
  166. public function to($unit)
  167. {
  168. $this->value = $this->process($this->unit, $unit, $this->value);
  169. $this->unit = $unit;
  170. return $this;
  171. }
  172. /**
  173. * @param $from
  174. * @param $to
  175. * @param $value
  176. * @return float
  177. * @throws \Exception
  178. */
  179. protected function process($from, $to, $value)
  180. {
  181. return ($value * $this->getConversion($from)) / $this->getConversion($to);
  182. }
  183. /**
  184. * @param $unit
  185. * @return mixed
  186. * @throws \Exception
  187. */
  188. protected function getConversion($unit)
  189. {
  190. if ( ! isset(static::$conversionChart[strtoupper($unit)]))
  191. {
  192. throw new \Exception(sprintf(
  193. 'No conversation between "%s" and "%s" is defined.',
  194. static::$native,
  195. $unit
  196. ));
  197. }
  198. return static::$conversionChart[strtoupper($unit)];
  199. }
  200. /**
  201. * @param int $decimals
  202. * @param string $decPoint
  203. * @param string $thousandSep
  204. * @return string
  205. */
  206. public function format($decimals = 2, $decPoint = '.', $thousandSep = ',')
  207. {
  208. return number_format($this->value, $decimals, $decPoint, $thousandSep);
  209. }
  210. /**
  211. * @return string
  212. */
  213. public function __toString()
  214. {
  215. return $this->format();
  216. }
  217. }