NumberWithUnit   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 9
dl 0
loc 47
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_as_short() 0 3 1
A per() 0 3 1
A get_as_narrow() 0 3 1
A as() 0 3 1
A __toString() 0 3 1
A __construct() 0 5 1
A get_as_long() 0 3 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Supplemental\Units;
4
5
use ICanBoogie\Accessor\AccessorTrait;
6
7
/**
8
 * @internal
9
 *
10
 * @property-read string $as_long A long format of the number.
11
 * @property-read string $as_short A short format of the number.
12
 * @property-read string $as_narrow A narrow format of the number.
13
 */
14
final class NumberWithUnit
15
{
16
    /**
17
     * @uses get_as_long
18
     * @uses get_as_short
19
     * @uses get_as_narrow
20
     */
21
    use AccessorTrait;
22
23
    /**
24
     * @param float|int|numeric-string $number
0 ignored issues
show
Documentation Bug introduced by
The doc comment float|int|numeric-string at position 4 could not be parsed: Unknown type name 'numeric-string' at position 4 in float|int|numeric-string.
Loading history...
25
     */
26
    public function __construct(
27
        private readonly float|int|string $number,
28
        private readonly string $unit,
29
        private readonly Units $units
30
    ) {
31
    }
32
33
    public function __toString(): string
34
    {
35
        return $this->as(Units::DEFAULT_LENGTH);
36
    }
37
38
    public function per(Unit|string $unit): NumberPerUnit
39
    {
40
        return new NumberPerUnit($this->number, $this->unit, $unit, $this->units);
41
    }
42
43
    private function get_as_long(): string
44
    {
45
        return $this->as(UnitLength::LONG);
46
    }
47
48
    private function get_as_short(): string
49
    {
50
        return $this->as(UnitLength::SHORT);
51
    }
52
53
    private function get_as_narrow(): string
54
    {
55
        return $this->as(UnitLength::NARROW);
56
    }
57
58
    private function as(UnitLength $length): string
59
    {
60
        return $this->units->format($this->number, $this->unit, $length);
61
    }
62
}
63