-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNonNegativeDecimal.php
More file actions
43 lines (35 loc) · 1.27 KB
/
NonNegativeDecimal.php
File metadata and controls
43 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace Epignosis\Types\Number;
use InvalidArgumentException;
class NonNegativeDecimal extends Decimal
{
/**
* @param float $value
* @param int $precision < max : 16>
* @param int $rounding (1 : PHP_ROUND_HALF_UP (Default), 2 : PHP_ROUND_HALF_DOWN,
* 3 : PHP_ROUND_HALF_EVEN, 4 : PHP_ROUND_HALF_ODD)
*/
public function __construct($value, int $precision = 2, int $rounding = PHP_ROUND_HALF_UP)
{
if ($value < 0) {
throw new InvalidArgumentException('Value must be a non-negative decimal number.');
}
parent::__construct($value, $precision, $rounding);
}
/**
* @param int|float|string $value
* @param int $precision
* @param int $rounding (1 : PHP_ROUND_HALF_UP, 2 : PHP_ROUND_HALF_DOWN,
* 3 : PHP_ROUND_HALF_EVEN, 4 : PHP_ROUND_HALF_ODD)
* @return self
*/
public static function fromNumeric($value, int $precision = 2, int $rounding = PHP_ROUND_HALF_UP): self
{
if (!is_numeric($value)) {
throw new InvalidArgumentException('Value is not numeric.');
}
$value = (float)$value;
return new self($value, $precision, $rounding);
}
}