summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Configuration/Parser/RatioParser.php
blob: f0fe321007df72f19e13cad87fce4b680711c913 (plain)
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
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration\Parser;

use InvalidArgumentException;
use RangeException;

class RatioParser
{
    private const MAX_VALUE = 1;
    private const MIN_VALUE = 0;

    public static function parse($value): float
    {
        if (filter_var($value, FILTER_VALIDATE_FLOAT) === false) {
            throw new InvalidArgumentException(
                sprintf('Value "%s" contains non-numeric value', $value)
            );
        }

        $result = (float) $value;

        if ($result > self::MAX_VALUE || $result < self::MIN_VALUE) {
            throw new RangeException(
                sprintf(
                    'Value must not be lower than %s or higher than %s. Given: %s',
                    self::MIN_VALUE,
                    self::MAX_VALUE,
                    $value
                )
            );
        }

        return $result;
    }
}