1
|
|
|
<?php namespace Arcanedev\Units; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Units\Contracts\UnitsManager as UnitsManagerContract; |
4
|
|
|
use Illuminate\Support\Arr; |
5
|
|
|
use Illuminate\Support\Manager; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class UnitsManager |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\Units |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class UnitsManager extends Manager implements UnitsManagerContract |
16
|
|
|
{ |
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
18
|
|
|
| Main Functions |
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
20
|
|
|
*/ |
21
|
|
|
/** |
22
|
|
|
* Create the distance unit instance. |
23
|
|
|
* |
24
|
|
|
* @return Bases\UnitMeasure |
25
|
|
|
*/ |
26
|
8 |
|
public function createDistanceDriver() |
27
|
|
|
{ |
28
|
8 |
|
return $this->buildUnit('distance', Measures\Distance::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create the liquid volume unit instance. |
33
|
|
|
* |
34
|
|
|
* @return Bases\UnitMeasure |
35
|
|
|
*/ |
36
|
8 |
|
public function createLiquidVolumeDriver() |
37
|
|
|
{ |
38
|
8 |
|
return $this->buildUnit('liquid-volume', Measures\LiquidVolume::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create the weight unit instance. |
43
|
|
|
* |
44
|
|
|
* @return Bases\UnitMeasure |
45
|
|
|
*/ |
46
|
8 |
|
public function createWeightDriver() |
47
|
|
|
{ |
48
|
8 |
|
return $this->buildUnit('weight', Measures\Weight::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the default driver name. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
8 |
|
public function getDefaultDriver() |
57
|
|
|
{ |
58
|
8 |
|
throw new InvalidArgumentException('No unit of measurement was specified.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* ------------------------------------------------------------------------------------------------ |
62
|
|
|
| Other Functions |
63
|
|
|
| ------------------------------------------------------------------------------------------------ |
64
|
|
|
*/ |
65
|
|
|
/** |
66
|
|
|
* Create a new driver instance. |
67
|
|
|
* |
68
|
|
|
* @param string $driver |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
24 |
|
protected function createDriver($driver) |
73
|
|
|
{ |
74
|
24 |
|
$method = 'create'.Str::studly($driver).'Driver'; |
75
|
|
|
|
76
|
|
|
// We'll check to see if a creator method exists for the given driver. If not we |
77
|
|
|
// will check for a custom driver creator, which allows developers to create |
78
|
|
|
// drivers using their own customized driver creator Closure to create it. |
79
|
24 |
|
if (isset($this->customCreators[$driver])) |
80
|
18 |
|
return $this->callCustomCreator($driver); |
81
|
24 |
|
elseif (method_exists($this, $method)) |
82
|
24 |
|
return $this->$method(); |
83
|
|
|
|
84
|
|
|
throw new InvalidArgumentException("Driver [$driver] not supported."); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build the unit of measurement. |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* @param string $unitClass |
92
|
|
|
* |
93
|
|
|
* @return \Arcanedev\Units\Bases\UnitMeasure |
94
|
|
|
*/ |
95
|
24 |
|
protected function buildUnit($key, $unitClass) |
96
|
|
|
{ |
97
|
24 |
|
$configs = $this->app['config']->get("units.$key", []); |
98
|
|
|
|
99
|
24 |
|
return new $unitClass( |
100
|
24 |
|
0, Arr::get($configs, 'default'), Arr::except($configs, ['default']) |
101
|
18 |
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|