Passed
Pull Request — main (#9)
by Michael
03:09
created

Number::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7
 *
8
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11
 */
12
13
namespace MichaelRubel\ValueObjects\Collection\Primitive;
14
15
use MichaelRubel\ValueObjects\Concerns\SanitizesNumbers;
16
use MichaelRubel\ValueObjects\ValueObject;
17
use PHP\Math\BigNumber\BigNumber;
18
19
/**
20
 * "Number" object that presents numeric values.
21
 *
22
 * @author Michael Rubél <[email protected]>
23
 *
24
 * @template TKey of array-key
25
 * @template TValue
26
 *
27
 * @method static static make(int|string $number, int $scale = 2)
28
 * @method static static from(int|string $number, int $scale = 2)
29
 * @method static static makeOrNull(int|string|null $number, int $scale = 2)
30
 *
31
 * @extends ValueObject<TKey, TValue>
32
 */
33
class Number extends ValueObject
34
{
35
    use SanitizesNumbers;
36
37
    /**
38
     * @var BigNumber
39
     */
40
    protected BigNumber $bigNumber;
41
42
    /**
43
     * Create a new instance of the value object.
44
     *
45
     * @param  int|string  $number
46
     * @param  int  $scale
47
     */
48 14
    public function __construct(int|string $number, protected int $scale = 2)
49
    {
50 14
        $this->bigNumber = new BigNumber($this->sanitize($number), $this->scale);
51
    }
52
53
    /**
54
     * Get the object value.
55
     *
56
     * @return string
57
     */
58 9
    public function value(): string
59
    {
60 9
        return $this->bigNumber->getValue();
61
    }
62
63
    /**
64
     * Get the number as an integer.
65
     *
66
     * @return int
67
     */
68 1
    public function asInteger(): int
69
    {
70 1
        return (int) $this->bigNumber->getValue();
71
    }
72
}
73