• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webboy / measurement-units / 12765994171

14 Jan 2025 10:43AM UTC coverage: 99.814% (-0.09%) from 99.907%
12765994171

push

github

webboy
Fancy extensions

1 of 2 new or added lines in 1 file covered. (50.0%)

1072 of 1074 relevant lines covered (99.81%)

9.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/MeasurementValueDto.php
1
<?php
2

3
namespace Webboy\MeasurementUnits;
4

5
use Webboy\MeasurementUnits\Exceptions\MeasurementExceptions\InvalidUnitIdMeasurementException;
6
use Webboy\MeasurementUnits\Exceptions\MeasurementValueExceptions\IllegalInstantiationMeasurementValueException;
7
use Webboy\MeasurementUnits\Exceptions\UnitConverterExceptions\InvalidConversionParameterConverterException;
8
use Webboy\MeasurementUnits\Exceptions\UnitConverterExceptions\InvalidTargetUnitIdUnitConverterException;
9
use Webboy\MeasurementUnits\Exceptions\UnitExceptions\InvalidUnitIdUnitException;
10

11
/**
12
 * The base class for all measurement value DTOs.
13
 */
14
class MeasurementValueDto
15
{
16
    /**
17
     * @var int | float The value of the measurement.
18
     */
19
    public int | float $value;
20

21
    /**
22
     * @var UnitDto The unit of the measurement.
23
     */
24
    public UnitDto $unit;
25

26
    /**
27
     * @var MeasurementDto The measurement of the value.
28
     */
29
    public MeasurementDto $measurement;
30

31
    /**
32
     * Create a new measurement value DTO.
33
     *
34
     * @param int | float $value The value of the measurement.
35
     * @param UnitDto $unit The unit of the measurement.
36
     * @param MeasurementDto $measurement The measurement of the value.
37
     * @throws InvalidUnitIdUnitException
38
     */
39
    protected function __construct(int | float $value, UnitDto $unit, MeasurementDto $measurement)
67✔
40
    {
41
        $this->value = $value;
67✔
42
        $this->unit = $unit;
67✔
43
        $this->measurement = $measurement;
67✔
44

45
        //Validate unit
46
        if (!key_exists($unit->id, $measurement->units)) {
67✔
NEW
47
            throw new InvalidUnitIdUnitException($unit->id);
×
48
        }
49
    }
50

51
    /**
52
     * Create a new measurement value DTO from a factory.
53
     *
54
     * @param int | float $value The value of the measurement.
55
     * @param UnitDto $unit The unit of the measurement.
56
     * @param MeasurementDto $measurement The measurement of the value.
57
     * @param string $caller The class that called the factory.
58
     * @return static The new measurement value DTO.
59
     * @throws IllegalInstantiationMeasurementValueException
60
     * @throws InvalidUnitIdUnitException
61
     */
62
    public static function createFromFactory(int | float $value, UnitDto $unit, MeasurementDto $measurement, string $caller): self
68✔
63
    {
64
        if ($caller !== get_class($measurement)) {
68✔
65
            throw new IllegalInstantiationMeasurementValueException();
1✔
66
        }
67

68
        return new self($value, $unit, $measurement);
67✔
69
    }
70

71
    /**
72
     * Convert the measurement value to a new unit.
73
     *
74
     * @param int|string $unit_id The ID of the target unit.
75
     * @param mixed ...$args The arguments to pass to the conversion functions.
76
     * @return MeasurementValueDto The new measurement value DTO.
77
     * @throws InvalidTargetUnitIdUnitConverterException
78
     * @throws IllegalInstantiationMeasurementValueException
79
     * @throws InvalidUnitIdMeasurementException
80
     * @throws InvalidConversionParameterConverterException
81
     */
82
    public function to(int | string $unit_id, ...$args): MeasurementValueDto
11✔
83
    {
84
        return UnitConverter::convert($this, $unit_id, ...$args);
11✔
85
    }
86

87
    /**
88
     * Convert the measurement value to a new unit.
89
     * @return string The string representation of the measurement value.
90
     */
91
    public function __toString(): string
11✔
92
    {
93
        return $this->value . ' ' . $this->unit->symbol;
11✔
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc