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

webboy / measurement-units / 12734123994

12 Jan 2025 01:52PM UTC coverage: 96.259% (+1.0%) from 95.262%
12734123994

push

github

webboy
Merge remote-tracking branch 'origin/main'

386 of 401 relevant lines covered (96.26%)

6.07 hits per line

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

93.33
/src/MeasurementDto.php
1
<?php
2

3
namespace Webboy\MeasurementUnits;
4

5
use ReflectionClass;
6
use Webboy\MeasurementUnits\Exceptions\MeasurementException;
7
use Webboy\MeasurementUnits\Exceptions\MeasurementExceptions\InvalidBaseUnitIdMeasurementException;
8
use Webboy\MeasurementUnits\Exceptions\MeasurementExceptions\InvalidMeasurementIdMeasurementException;
9
use Webboy\MeasurementUnits\Exceptions\MeasurementExceptions\InvalidUnitDefinitionsMeasurementException;
10
use Webboy\MeasurementUnits\Exceptions\MeasurementExceptions\InvalidUnitIdMeasurementException;
11
use Webboy\MeasurementUnits\Exceptions\MeasurementValueExceptions\IllegalInstantiationMeasurementValueException;
12

13
/**
14
 * The base class for all measurement DTOs.
15
 */
16
abstract class MeasurementDto
17
{
18
    /**
19
     * @var int | string The ID of the measurement.
20
     */
21
    public int | string $id;
22

23
    /**
24
     * @var string The name of the measurement.
25
     */
26
    public string $name;
27

28
    /**
29
     * @var int|string The base unit of the measurement.
30
     */
31
    public int | string $base_unit_id;
32

33
    /**
34
     * @var UnitDto[] The units of the measurement.
35
     */
36
    public array $units;
37

38
    /**
39
     * @var UnitDto The base unit of the measurement.
40
     */
41
    protected UnitDto $base_unit;
42

43
    /**
44
     * Create a new measurement DTO.
45
     *
46
     * @param int|string $id The ID of the measurement.
47
     * @param string $name The name of the measurement.
48
     * @param int|string $base_unit_id The ID of the base unit.
49
     * @param array|null $units The units of the measurement.
50
     * @param array|null $validIds The valid IDs of the measurement.
51
     * @throws MeasurementException
52
     */
53
    public function __construct(
25✔
54
        int | string $id,
55
        string $name,
56
        int | string $base_unit_id,
57
        ?array $units = null,
58
        private readonly ?array $validIds = null,
59
    ){
60
        //Validate ID
61
        if ($this->validIds !== null && !in_array($id, $this->validIds)) {
25✔
62
            throw new InvalidMeasurementIdMeasurementException($id);
×
63
        }
64

65
        $this->id = $id;
25✔
66
        $this->name = $name;
25✔
67
        $this->base_unit_id = $base_unit_id;
25✔
68
        $this->units = $units ?? $this->loadDefinitions();
25✔
69

70
        //Set base unit
71
        $this->base_unit = $this->getBaseUnit();
25✔
72
    }
73

74
    /**
75
     * Create a new measurement DTO from a factory.
76
     *
77
     * @param int|float $value
78
     * @param int|string $unitId
79
     * @return MeasurementValueDto
80
     * @throws IllegalInstantiationMeasurementValueException
81
     * @throws InvalidUnitIdMeasurementException
82
     */
83
    public function createValue(int | float $value, int | string $unitId): MeasurementValueDto
12✔
84
    {
85
        $unit = $this->getUnit($unitId);
12✔
86
        return MeasurementValueDto::createFromFactory($value, $unit, $this, $this::class);
6✔
87
    }
88

89
    /**
90
     * Get the base unit of the measurement.
91
     *
92
     * @return UnitDto
93
     * @throws InvalidBaseUnitIdMeasurementException
94
     */
95
    public function getBaseUnit(): UnitDto
25✔
96
    {
97
        $baseUnit = array_filter(
25✔
98
            $this->units,
25✔
99
            function(UnitDto $unit) {
25✔
100
                return $unit->id === $this->base_unit_id;
25✔
101
            }
25✔
102
        );
25✔
103

104
        $baseUnit = reset($baseUnit);
25✔
105

106
        if (empty($baseUnit)) {
25✔
107
            throw new InvalidBaseUnitIdMeasurementException($this->base_unit_id);
6✔
108
        }
109

110
        return $baseUnit;
25✔
111
    }
112

113
    /**
114
     * Load the definitions for the measurement.
115
     *
116
     * @return UnitDto[]
117
     * @throws InvalidUnitDefinitionsMeasurementException
118
     */
119
    protected function loadDefinitions(): array
24✔
120
    {
121
        $definitions = [];
24✔
122

123
        // Path to the definitions directory (adjust as needed if relative paths differ)
124
        $calledClass = new ReflectionClass(static::class);
24✔
125
        $definitionsPath = dirname($calledClass->getFileName())
24✔
126
            . '/Definitions/'
24✔
127
            . str_replace(
24✔
128
                'MeasurementDto',
24✔
129
                '',
24✔
130
                $calledClass->getShortName()
24✔
131
            );
24✔
132

133
        // Search for all PHP files in the directory
134
        foreach (glob($definitionsPath . '/*.php') as $definitionFile) {
24✔
135
            // Include the file and add the returned UnitDto object to the array
136
            $unitDefinition = include $definitionFile;
24✔
137

138
            if (!$unitDefinition instanceof UnitDto) {
24✔
139
                // Skip or handle improperly formatted files
140
                continue;
×
141
            }
142

143
            $definitions[] = $unitDefinition;
24✔
144
        }
145

146
        if (empty($definitions)) {
24✔
147
            throw new InvalidUnitDefinitionsMeasurementException();
×
148
        }
149

150
        return $definitions;
24✔
151

152
    }
153

154
    /**
155
     * Get a unit by its ID.
156
     *
157
     * @param int|string $unitId
158
     * @return UnitDto
159
     * @throws InvalidUnitIdMeasurementException
160
     */
161
    public function getUnit(int|string $unitId): UnitDto
18✔
162
    {
163
        foreach ($this->units as $unit) {
18✔
164
            if ($unit->id === $unitId) {
18✔
165
                return $unit;
12✔
166
            }
167
        }
168

169
        throw new InvalidUnitIdMeasurementException($unitId);
6✔
170
    }
171
}
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