1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akeneo\Bundle\MeasureBundle\Manager; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Measure manager |
7
|
|
|
* |
8
|
|
|
* @author Gildas Quemener <[email protected]> |
9
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
11
|
|
|
*/ |
12
|
|
|
class MeasureManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array $config |
16
|
|
|
*/ |
17
|
|
|
protected $config = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set measure config |
21
|
|
|
* |
22
|
|
|
* @param array $config |
23
|
|
|
*/ |
24
|
|
|
public function setMeasureConfig(array $config) |
25
|
|
|
{ |
26
|
|
|
$this->config = $config; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get unit symbols for a measure family |
31
|
|
|
* |
32
|
|
|
* @param string $family the measure family |
33
|
|
|
* |
34
|
|
|
* @return array the measure symbols |
35
|
|
|
*/ |
36
|
|
|
public function getUnitSymbolsForFamily($family) |
37
|
|
|
{ |
38
|
|
|
$familyConfig = $this->getFamilyConfig($family); |
39
|
|
|
$unitsConfig = $familyConfig['units']; |
40
|
|
|
|
41
|
|
|
return array_map( |
42
|
|
|
function ($unit) { |
43
|
|
|
return $unit['symbol']; |
44
|
|
|
}, |
45
|
|
|
$unitsConfig |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if unit exists in the given family |
51
|
|
|
* |
52
|
|
|
* @param string $unit the unit to check |
53
|
|
|
* @param string $family the measure family |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function unitExistsInFamily($unit, $family) |
58
|
|
|
{ |
59
|
|
|
return in_array($unit, $this->getUnitSymbolsForFamily($family)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get standard unit for a measure family |
64
|
|
|
* |
65
|
|
|
* @param string $family |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getStandardUnitForFamily($family) |
70
|
|
|
{ |
71
|
|
|
$familyConfig = $this->getFamilyConfig($family); |
72
|
|
|
|
73
|
|
|
return $familyConfig['standard']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the family config |
78
|
|
|
* |
79
|
|
|
* @param string $family |
80
|
|
|
* |
81
|
|
|
* @throws \InvalidArgumentException |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
protected function getFamilyConfig($family) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (!isset($this->config[$family])) { |
87
|
|
|
throw new \InvalidArgumentException( |
88
|
|
|
sprintf('Undefined measure family "%s"', $family) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->config[$family]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.