├── tests └── .gitkeep ├── .gitignore ├── .travis.yml ├── src └── Abhimanyu003 │ └── Conversion │ ├── Facades │ └── Conversion.php │ ├── ConversionServiceProvider.php │ └── Conversion.php ├── phpunit.xml ├── composer.json ├── LICENSE └── README.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /src/Abhimanyu003/Conversion/Facades/Conversion.php: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abhimanyu003/conversion", 3 | "description": "Laravel Unit Conversion", 4 | "keywords": [ 5 | "laravel", "unit", "measurements", "conversion" 6 | ], 7 | "homepage": "https://github.com/abhimanyu003/conversion", 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "Abhimanyu Sharma", 12 | "email": "abhimanyusharma003@gmail.com", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.4.0", 18 | "illuminate/support": "4.*|~5.0" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "Abhimanyu003\\Conversion": "src/" 23 | } 24 | }, 25 | "minimum-stability": "stable" 26 | } 27 | -------------------------------------------------------------------------------- /src/Abhimanyu003/Conversion/ConversionServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('conversion', function ($app) { 23 | return new Conversion; 24 | }); 25 | } 26 | 27 | 28 | /** 29 | * Get the services provided by the provider. 30 | * 31 | * @return array 32 | */ 33 | public function provides() 34 | { 35 | return ['conversion']; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Abhimanyu Sharma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Laravel Unit Conversion 2 | 3 | Library help to convert units. 4 | 5 | ## Units supported 6 | 7 | * Acceleration 8 | * Angle 9 | * Area 10 | * Storage 11 | * Current 12 | * Fuel 13 | * Length 14 | * Mass 15 | * Pressure 16 | * Speed 17 | * Temperature 18 | * Time 19 | * Voltage 20 | * Volume 21 | 22 | 23 | ## Installation 24 | 25 | * Add below line to `composer.json ` 26 | 27 | ```php 28 | // ... 29 | "abhimanyusharma003/conversion": "dev-master" 30 | // ... 31 | ``` 32 | 33 | * Run `composer update` or `composer install ` 34 | 35 | * Open `config/app.php` and add the service provider to your `providers` array. 36 | 37 | ```php 38 | Abhimanyu003\Conversion\ConversionServiceProvider::class 39 | ``` 40 | 41 | * Add Aliases 42 | 43 | ```php 44 | 'Conversion' => Abhimanyu003\Conversion\Facades\Conversion::class 45 | ``` 46 | 47 | ## How to use 48 | 49 | 50 | ```php 51 | Conversion::convert($value,'type')->to('type'); 52 | ``` 53 | 54 | Formatting results use 55 | 56 | ```php 57 | Conversion::convert($value,'type')->to('type') 58 | ->format(int decimal,'decimal place modifier','thousand place modifer'); 59 | ``` 60 | 61 | ## Example 62 | 63 | * Converting MB to kb 64 | 65 | ```php 66 | Conversion::convert(1, 'megabyte')->to('kilobyte'); // output 1,024.00 ( two decimal place ) 67 | 68 | // Formatting the output 69 | Conversion::convert(1, 'megabyte')->to('kilobyte')->format(0,'.',','); // output 1,024 ( no decimal place ) 70 | ``` 71 | 72 | * Converting mm to cm 73 | 74 | ```php 75 | Conversion::convert(1000,'millimetre')->to('centimetre'); 76 | ``` 77 | 78 | 79 | * Converting kg to g 80 | 81 | ```php 82 | Conversion::convert(1,'kilogram')->to('gram'); 83 | ``` 84 | 85 | 86 | ## Conversion Chart 87 | 88 | ## Acceleration 89 | METRE_PER_SECOND_SQUARE 90 | 91 | ## Angle 92 | TURN 93 | RADIAN 94 | DEGREE 95 | GRADIAN 96 | 97 | ## Area 98 | SQUARE_METRE 99 | HECTARE 100 | SQUARE_KILOMETRE 101 | SQUARE_INCH 102 | SQUARE_FEET 103 | SQUARE_YARD 104 | ACRE 105 | SQUARE_MILE 106 | 107 | ## Storage 108 | BIT 109 | BYTE 110 | KILOBIT 111 | KILOBYTE 112 | MEGABIT 113 | MEGABYTE 114 | GIGABIT 115 | GIGABYTE 116 | TERABIT 117 | TERABYTE 118 | PETABIT 119 | PETABYTE 120 | 121 | ## Current 122 | STATAMPERE 123 | MICROAMPERE 124 | MILLIAMPERE 125 | AMPERE 126 | ABAMPERE 127 | KILOAMPERE 128 | 129 | ## Fuel 130 | KILOMETRES_PER_LITRE 131 | LITRE_PER_100_KILOMETRE 132 | MILES_PER_GALLON 133 | US_MILES_PER_GALLON 134 | 135 | ## Length 136 | MILLIMETRE 137 | CENTIMETRE 138 | METRE 139 | KILOMETRE 140 | INCH 141 | FOOT 142 | YARD 143 | MILE 144 | NAUTICAL_MILE 145 | 146 | ## Mass 147 | MICROGRAM 148 | MILLIGRAM 149 | GRAM 150 | KILOGRAM 151 | METRIC_TON 152 | OUNCE 153 | POUND 154 | STONE 155 | SHORT_TON 156 | LONG_TON 157 | 158 | ## Pressure 159 | PASCAL 160 | KILOPASCAL 161 | MEGAPASCAL 162 | BAR 163 | MILLIMETRES_OF_MERCURY 164 | INCHES_OF_MERCURY 165 | POUNDS_PER_SQUARE_INCH 166 | ATMOSPHERE 167 | 168 | ## Speed 169 | METRE_PER_SECOND 170 | KILOMETRES_PER_HOUR 171 | FEET_PER_SECOND 172 | MILES_PER_HOUR 173 | KNOT 174 | 175 | ## Temperature 176 | CELSIUS 177 | FAHRENHEIT 178 | KELVIN 179 | 180 | ## Time 181 | NANOSECOND 182 | MICROSECOND 183 | MILLISECOND 184 | SECOND 185 | MINUTE 186 | HOUR 187 | DAY 188 | WEEK 189 | MONTH 190 | YEAR 191 | DECADE 192 | CENTURY 193 | MILLENIUM 194 | 195 | ## Voltage 196 | VOLT 197 | KILOVOLT 198 | 199 | ## Volume 200 | MILLILITRE 201 | LITRE 202 | CUBIC_METRE 203 | GALLON 204 | QUART 205 | PINT 206 | TABLESPOON 207 | TEASPOON 208 | US_GALLON 209 | US_QUART 210 | US_PINT 211 | US_CUP 212 | US_OUNCE 213 | US_TABLESPOON 214 | US_TEASPOON 215 | CUBIC_INCH 216 | CUBIC_FOOT 217 | 218 | 219 | ## Contribute 220 | 221 | Feel free to contribute and update the rep. 222 | -------------------------------------------------------------------------------- /src/Abhimanyu003/Conversion/Conversion.php: -------------------------------------------------------------------------------- 1 | 1, 12 | 13 | // Angle 14 | 'TURN' => 6.28318531, 15 | 'RADIAN' => 1, 16 | 'DEGREE' => 0.0174532925, 17 | 'GRADIAN' => 0.015707963267949, 18 | 19 | // Area 20 | 'SQUARE_METRE' => 1, 21 | 'HECTARE' => 100, 22 | 'SQUARE_KILOMETRE' => 1000000, 23 | 'SQUARE_INCH' => 0.00064516, 24 | 'SQUARE_FEET' => 0.09290304, 25 | 'SQUARE_YARD' => 0.83612736, 26 | 'ACRE' => 247.105, 27 | 'SQUARE_MILE' => 2589988.110336, 28 | 29 | // Storage 30 | 'BIT' => 0.00012207, 31 | 'BYTE' => 0.000976563, 32 | 'KILOBIT' => 0.125, 33 | 'KILOBYTE' => 1, 34 | 'MEGABIT' => 128, 35 | 'MEGABYTE' => 1024, 36 | 'GIGABIT' => 131072, 37 | 'GIGABYTE' => 1.049e+6, 38 | 'TERABIT' => 1.342e+8, 39 | 'TERABYTE' => 1.074e+9, 40 | 'PETABIT' => 1.374e+11, 41 | 'PETABYTE' => 1.1e+12, 42 | 43 | // Current 44 | 'STATAMPERE' => 3.33564e-10, 45 | 'MICROAMPERE' => 1e-06, 46 | 'MILLIAMPERE' => 0.001, 47 | 'AMPERE' => 1, 48 | 'ABAMPERE' => 10, 49 | 'KILOAMPERE' => 1000, 50 | 51 | // Fuel 52 | 'KILOMETRES_PER_LITRE' => 1, 53 | 'LITRE_PER_100_KILOMETRES' => 100, 54 | 'MILES_PER_GALLON' => 0.354006, 55 | 'US_MILES_PER_GALLON' => 0.425144, 56 | 57 | // Length 58 | 'MILLIMETRE' => 0.001, 59 | 'CENTIMETRE' => 0.01, 60 | 'METRE' => 1, 61 | 'KILOMETRE' => 1000, 62 | 'INCH' => 0.0254, 63 | 'FOOT' => 0.3048, 64 | 'YARD' => 0.9144, 65 | 'MILE' => 1609.34, 66 | 'NAUTICAL_MILE' => 1852, 67 | 68 | // Mass 69 | 'MICROGRAM' => 1e-7, 70 | 'MILLIGRAM' => 1e-6, 71 | 'GRAM' => 0.001, 72 | 'KILOGRAM' => 1, 73 | 'METRIC_TON' => 100, 74 | 'OUNCE' => 0.0283495, 75 | 'POUND' => 0.453592, 76 | 'STONE' => 6.35029, 77 | 'SHORT_TON' => 907.185, 78 | 'LONG_TON' => 1016.0469088, 79 | 80 | // Pressure 81 | 'PASCAL' => 1, 82 | 'KILOPASCAL' => 1000, 83 | 'MEGAPASCAL' => 1e6, 84 | 'BAR' => 1e5, 85 | 'MILLIMETRES_OF_MERCURY' => 133.3224, 86 | 'INCHES_OF_MERCURY' => 3.386389e3, 87 | 'POUNDS_PER_SQUARE_INCH' => 6.894757e3, 88 | 'ATMOSPHERE' => 101325, 89 | 90 | // Speed 91 | 'METRE_PER_SECOND' => 1, 92 | 'KILOMETRES_PER_HOUR' => 0.277778, 93 | 'FEET_PER_SECOND' => 0.3048, 94 | 'MILES_PER_HOUR' => 0.44704, 95 | 'KNOT' => 0.514444, 96 | 97 | // Temperature 98 | 'CELSIUS' => 273.15, 99 | 'FAHRENHEIT' => 255.372, 100 | 'KELVIN' => 1, 101 | 102 | // Time 103 | 'NANOSECOND' => 1e-9, 104 | 'MICROSECOND' => 1e-6, 105 | 'MILLISECOND' => 0.001, 106 | 'SECOND' => 1, 107 | 'MINUTE' => 60, 108 | 'HOUR' => 3600, 109 | 'DAY' => 86400, 110 | 'WEEK' => 604800, 111 | 'MONTH' => 2.62974e6, 112 | 'YEAR' => 3.15569e7, 113 | 'DECADE' => 3.15569e8, 114 | 'CENTURY' => 3.15569e9, 115 | 'MILLENIUM' => 3.1556926e10, 116 | 117 | // Voltage 118 | 'VOLT' => 1, 119 | 'KILOVOLT' => 1000, 120 | 121 | // Volume 122 | 'MILLILITRE' => 1e-6, 123 | 'LITRE' => 0.001, 124 | 'CUBIC_METRE' => 1, 125 | 'GALLON' => 0.00454609, 126 | 'QUART' => 0.00113652, 127 | 'PINT' => 0.000568261, 128 | 'TABLESPOON' => 1.7758e-5, 129 | 'TEASPOON' => 5.9194e-6, 130 | 'US_GALLON' => 0.00378541, 131 | 'US_QUART' => 0.000946353, 132 | 'US_PINT' => 0.000473176, 133 | 'US_CUP' => 0.000236588, 134 | 'US_OUNCE' => 2.9574e-5, 135 | 'US_TABLESPOON' => 1.4787e-5, 136 | 'US_TEASPOON' => 4.9289e-6, 137 | 'CUBIC_INCH' => 1.6387e-5, 138 | 'CUBIC_FOOT' => 0.0283168, 139 | ]; 140 | 141 | /** 142 | * @var string 143 | */ 144 | protected $value; 145 | 146 | 147 | /** 148 | * @var string 149 | */ 150 | protected $unit; 151 | 152 | 153 | /** 154 | * @var 155 | */ 156 | protected static $native; 157 | 158 | /** 159 | * @var null 160 | */ 161 | protected $number = null; 162 | /** 163 | * @var 164 | */ 165 | protected $type; 166 | 167 | /** 168 | * @param string $quantity 169 | * @param string $unit 170 | */ 171 | public function __construct($quantity = '', $unit = '') 172 | { 173 | $this->unit = $unit; 174 | $this->value = $quantity; 175 | } 176 | 177 | /** 178 | * @param $quantity 179 | * @param $unit 180 | * @return Conversion 181 | */ 182 | public function convert($quantity, $unit) 183 | { 184 | return new Conversion($quantity, $unit); 185 | } 186 | 187 | 188 | /** 189 | * @param $unit 190 | * @return $this 191 | */ 192 | public function to($unit) 193 | { 194 | $this->value = $this->process($this->unit, $unit, $this->value); 195 | $this->unit = $unit; 196 | 197 | return $this; 198 | } 199 | 200 | 201 | /** 202 | * @param $from 203 | * @param $to 204 | * @param $value 205 | * @return float 206 | * @throws \Exception 207 | */ 208 | protected function process($from, $to, $value) 209 | { 210 | return ($value * $this->getConversion($from)) / $this->getConversion($to); 211 | } 212 | 213 | 214 | /** 215 | * @param $unit 216 | * @return mixed 217 | * @throws \Exception 218 | */ 219 | protected function getConversion($unit) 220 | { 221 | if (!isset(static::$conversionChart[strtoupper($unit)])) { 222 | throw new \Exception(sprintf( 223 | 'No conversion between "%s" and "%s" is defined.', 224 | static::$native, 225 | $unit 226 | )); 227 | } 228 | 229 | return static::$conversionChart[strtoupper($unit)]; 230 | } 231 | 232 | 233 | /** 234 | * @param int $decimals 235 | * @param string $decPoint 236 | * @param string $thousandSep 237 | * @return string 238 | */ 239 | public function format($decimals = 2, $decPoint = '.', $thousandSep = ',') 240 | { 241 | return number_format($this->value, $decimals, $decPoint, $thousandSep); 242 | } 243 | 244 | 245 | /** 246 | * @return string 247 | */ 248 | public function __toString() 249 | { 250 | return $this->format(); 251 | } 252 | } 253 | --------------------------------------------------------------------------------