| 1 |  |  | <?php namespace Arcanedev\Units\Measures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Arcanedev\Units\Bases\UnitMeasure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Arcanedev\Units\Contracts\Measures\Distance as DistanceContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Arcanedev\Units\Traits\Calculatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class     Distance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package  Arcanedev\Units\Measures | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author   ARCANEDEV <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Distance extends UnitMeasure implements DistanceContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      |  Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use Calculatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      |  Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Distance constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param  float|int  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param  string     $unit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param  array      $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct($value = 0, $unit = self::M, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 176 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->init($value, $unit, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 176 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 176 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 176 |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 176 |  |      |  Getters & Setters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 176 |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 176 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 176 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 132 |  |      * Get the default names. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 176 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function defaultNames() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return array_combine(static::units(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'kilometer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'hectometre', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'decametre', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'metre', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'decimetre', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 8 |  |             'centimetre', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'millimetre', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 8 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  |      |  Main Functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 6 |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 6 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 6 |  |      * Make a distance instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param  float|int  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param  string     $unit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param  array      $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public static function make($value = 0, $unit = self::M, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 8 |  |         return new static($value, $unit, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 8 |  |      * Convert the distance to the given unit. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param  string  $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return \Arcanedev\Units\Contracts\UnitMeasure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function to($to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if ($to === $this->unit()) return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $value = static::convert($this->unit(), $to, $this->value()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return static::make($value, $to, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'symbols' => $this->symbols(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'names'   => $this->names(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'format'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 72 |  |                 'decimals'            => $this->decimals, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 'decimal-separator'   => $this->decimalSeparator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 72 |  |                 'thousands-separator' => $this->thousandsSeparator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Convert the distance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param  string     $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param  string     $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 72 |  |      * @param  float|int  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 72 |  |      * @return float|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 24 |  |     public static function convert($from, $to, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 24 |  |         return $value * static::getRatio($to, $from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      |  Calculation Functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Add the distance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param  float|int  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param  string     $unit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 24 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return \Arcanedev\Units\Contracts\Measures\Distance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 24 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function addDistance($value, $unit = self::M) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this->add( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             self::make($value, $unit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * Sub the distance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param  float|int  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param  string     $unit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return \Arcanedev\Units\Contracts\Measures\Distance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 16 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function subDistance($value, $unit = self::M) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 16 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 16 |  |         return $this->sub( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 12 |  |             static::make($value, $unit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      |  Other Functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * Get the distance convert ratio. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 16 |  |      * @param  string  $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param  string  $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 16 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 16 |  |      * @return float|int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 | 12 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |     protected static function getRatio($to, $from) | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |         static::checkUnit($from); | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |         static::checkUnit($to); | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |         if ($to === $from) return 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |         $ratios = static::getRatios(); | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |         return $ratios[$to] / $ratios[$from]; | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * Get all the distance ratios. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 24 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 24 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 24 |  |     protected static function getRatios() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 24 |  |         $rate   = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $ratios = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 24 |  |             static::KM  => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             static::HM  => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 24 |  |             static::DAM => 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             static::M   => 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             static::DM  => 4, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             static::CM  => 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             static::MM  => 6, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         return array_map(function ($ratio) use ($rate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 24 |  |             return static::calculate($rate, '^', $ratio); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         }, $ratios); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 24 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 195 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 196 |  |  |  |