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

webboy / measurement-units / 12737284671

12 Jan 2025 09:34PM UTC coverage: 99.227% (-0.8%) from 100.0%
12737284671

push

github

webboy
Preparing v1.2.0

235 of 236 new or added lines in 22 files covered. (99.58%)

6 existing lines in 3 files now uncovered.

899 of 906 relevant lines covered (99.23%)

8.84 hits per line

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

92.31
/src/UnitDto.php
1
<?php
2

3
namespace Webboy\MeasurementUnits;
4

5
use Closure;
6
use Webboy\MeasurementUnits\Exceptions\UnitException;
7
use Webboy\MeasurementUnits\Exceptions\UnitExceptions\InvalidUnitIdUnitException;
8

9
/**
10
 * The base class for all measurement DTOs.
11
 */
12
abstract class UnitDto
13
{
14
    /**
15
     * @var int|string The ID of the unit.
16
     */
17
    public int | string $id;
18

19
    /**
20
     * @var string The name of the unit.
21
     */
22
    public string $name;
23

24
    /**
25
     * @var string The symbol of the unit.
26
     */
27
    public string $symbol;
28

29
    /**
30
     * @var bool Whether the unit is the base unit.
31
     */
32
    public bool $isBase = false;
33

34
    /**
35
     * @var Closure The closure to convert the unit to the base unit.
36
     */
37
    protected Closure $toBase;
38

39
    /**
40
     * @var Closure The closure to convert the unit from the base unit.
41
     */
42
    protected Closure $fromBase;
43

44
    /**
45
     * Create a new unit DTO.
46
     *
47
     * @param int|string $id The ID of the unit.
48
     * @param string $name The name of the unit.
49
     * @param string $symbol The symbol of the unit.
50
     * @param Closure $toBase The closure to convert to the base unit.
51
     * @param Closure $fromBase The closure to convert from the base unit.
52
     * @param bool $isBase Whether the unit is the base unit.
53
     * @param array|false|null $validIds The valid IDs for the unit.
54
     * @throws InvalidUnitIdUnitException
55
     */
56
    public function __construct(
50✔
57
        int | string $id,
58
        string $name,
59
        string $symbol,
60
        Closure $toBase,
61
        Closure $fromBase,
62
        bool $isBase = false,
63
        protected readonly array | null | false $validIds = null
64
    ) {
65
        //Validate ID
66
        if (is_array($this->validIds) && !in_array($id, $this->validIds)) {
50✔
UNCOV
67
            throw new InvalidUnitIdUnitException($id);
×
68
        }
69

70
        $this->id = $id;
50✔
71
        $this->name = $name;
50✔
72
        $this->symbol = $symbol;
50✔
73
        $this->toBase = $toBase;
50✔
74
        $this->fromBase = $fromBase;
50✔
75
        $this->isBase = $isBase;
50✔
76
    }
77

78
    /**
79
     * @return Closure
80
     */
81
    public function getToBase(): Closure
1✔
82
    {
83
        return $this->toBase;
1✔
84
    }
85

86
    /**
87
     * @return Closure
88
     */
89
    public function getFromBase(): Closure
1✔
90
    {
91
        return $this->fromBase;
1✔
92
    }
93
}
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