Total Complexity | 7 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
18 | final class Storage |
||
19 | { |
||
20 | public const UNITS = [ |
||
21 | 'bouteille', |
||
22 | 'boite', |
||
23 | 'carton', |
||
24 | 'colis', |
||
25 | 'kilogramme', |
||
26 | 'litre', |
||
27 | 'pièce', |
||
28 | 'poche', |
||
29 | 'portion', |
||
30 | ]; |
||
31 | |||
32 | private string $unit; |
||
33 | private float $quantity; |
||
34 | |||
35 | public function __construct(string $unit, float $quantity) |
||
36 | { |
||
37 | $this->unit = $unit; |
||
38 | $this->quantity = $quantity; |
||
39 | } |
||
40 | |||
41 | public static function fromArray(array $storage): self |
||
42 | { |
||
43 | $unit = static::isValidUnit($storage[0]); |
||
44 | $quantity = static::isValidQuantity($storage[1]); |
||
45 | |||
46 | return new self($unit, $quantity); |
||
47 | } |
||
48 | |||
49 | public function toArray(): array |
||
50 | { |
||
51 | return [$this->unit, $this->quantity]; |
||
52 | } |
||
53 | |||
54 | private static function isValidUnit(string $unit): string |
||
55 | { |
||
56 | if (!\in_array(\strtolower($unit), self::UNITS, true)) { |
||
57 | throw new InvalidUnit(); |
||
58 | } |
||
59 | |||
60 | return \strtolower($unit); |
||
61 | } |
||
62 | |||
63 | private static function isValidQuantity(float $quantity): float |
||
70 | } |
||
71 | } |
||
72 |