|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP Units of Measure Library. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/php-units |
|
6
|
|
|
* @package php-units |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\units; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\units\exceptions\InvalidConfigException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Abstract Unit of measure. E.g. |
|
17
|
|
|
* Almost final. |
|
18
|
|
|
* Only findCoverter can be changed. |
|
19
|
|
|
* |
|
20
|
|
|
* @see UnitInterface |
|
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractUnit implements UnitInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $name; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ConverterInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $converter; |
|
34
|
|
|
|
|
35
|
3 |
|
private function __construct($name, ConverterInterface $converter) |
|
36
|
|
|
{ |
|
37
|
3 |
|
$this->name = $name; |
|
38
|
3 |
|
$this->converter = $converter; |
|
39
|
3 |
|
} |
|
40
|
|
|
|
|
41
|
10 |
|
private function getConverter() |
|
42
|
|
|
{ |
|
43
|
10 |
|
return $this->converter; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
10 |
|
final public function getName() |
|
50
|
|
|
{ |
|
51
|
10 |
|
return $this->name; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
3 |
|
final public function getMeasure() |
|
58
|
|
|
{ |
|
59
|
3 |
|
return $this->getConverter()->getMeasure($this); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
4 |
|
final public function getCalculator() |
|
66
|
|
|
{ |
|
67
|
4 |
|
return $this->getConverter()->getCalculator($this); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
2 |
|
final public function equals(UnitInterface $other) |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $this->getConverter()->equals($this, $other); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
2 |
|
final public function isConvertible(UnitInterface $other) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return $this->getConverter()->isConvertible($this, $other); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
6 |
|
final public function convert(UnitInterface $other, $quantity) |
|
90
|
|
|
{ |
|
91
|
6 |
|
return $this->getConverter()->convert($this, $other, $quantity); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var Unit[] |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static $units; |
|
98
|
|
|
|
|
99
|
15 |
|
final public static function __callStatic($name, $args) |
|
100
|
|
|
{ |
|
101
|
15 |
|
return static::create($name); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
15 |
|
public static function create($name) |
|
105
|
|
|
{ |
|
106
|
15 |
|
if (!isset(static::$units[$name])) { |
|
107
|
3 |
|
static::$units[$name] = new static($name, static::findConverter($name)); |
|
108
|
3 |
|
} |
|
109
|
|
|
|
|
110
|
15 |
|
return static::$units[$name]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns converter for given unit name. |
|
115
|
|
|
* The only function to change in child classes. |
|
116
|
|
|
* XXX Should be defined as abstract but not supported in PHP5. |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
* @throws InvalidConfigException |
|
119
|
|
|
* @return ConverterInterface |
|
120
|
|
|
*/ |
|
121
|
|
|
protected static function findConverter($name) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
throw new InvalidConfigException('findConverter method must be redefined'); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.