SchemaFloatAttribute::max()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Ipag\Sdk\Model\Schema;
4
5
use Ipag\Sdk\Model\Schema\Exception\SchemaAttributeParseException;
6
7
/**
8
 * @codeCoverageIgnore
9
 */
10
class SchemaFloatAttribute extends SchemaAttribute
11
{
12
    protected ?float $min = null;
13
    protected ?float $max = null;
14
15
    public function min(?float $min): self
16
    {
17
        $this->min = $min;
18
        return $this;
19
    }
20
21
    public function max(?float $max): self
22
    {
23
        $this->max = $max;
24
        return $this;
25
    }
26
27
    public function parseContextual($value)
28
    {
29
        if (is_int($value)) {
30
            $value = (float) $value;
31
        }
32
33
        if (!is_null($this->min) && $value < $this->min) {
34
            throw new SchemaAttributeParseException($this, "Provided value '{$value}' is less than the minimum value of {$this->min}");
35
        }
36
37
        if (!is_null($this->max) && $value > $this->max) {
38
            throw new SchemaAttributeParseException($this, "Provided value '{$value}' is greater than the maximum value of {$this->max}");
39
        }
40
41
        if (is_float($value)) {
42
            return $value;
43
        }
44
45
        throw new SchemaAttributeParseException($this, "Provided value '$value' is not a floating-point number");
46
    }
47
}