summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Configuration/Parser/BooleanParser.php
blob: 4141c61efed983e84dba7da17f2d7a452947c5c2 (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
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration\Parser;

use InvalidArgumentException;

class BooleanParser
{
    private const TRUE_VALUE = 'true';
    private const FALSE_VALUE = 'false';

    /**
     * @param string|bool $value
     */
    public static function parse($value): bool
    {
        if (is_bool($value)) {
            return $value;
        }
        if (strtolower($value) === self::TRUE_VALUE) {
            return true;
        }

        if (strtolower($value) === self::FALSE_VALUE) {
            return false;
        }

        throw new InvalidArgumentException(
            sprintf('Value "%s" is a non-boolean value', $value)
        );
    }
}