aboutsummaryrefslogtreecommitdiff
path: root/src/Parser/Parser.php
blob: 7a5c37bde7d4d55a57087316100e22d4eed598ae (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);

/**
 * @author: Alexander Kiryukhin <alexander@kiryukhin.su>
 * @license: MIT
 */

namespace NeonXP\Dotenv\Parser;

use NeonXP\Dotenv\Types\KeyValue;

/**
 * Class Parser
 * @package NeonXP\Dotenv\Parser
 */
class Parser implements ParserInterface
{
    const REGEX_EXPORT_PREFIX = '/^\s*export\s/i';

    // Quotes and comments
    const SINGLE_QUOTED_LINE_WITH_COMMENT = '/^\'(.*?)\'\s+#.*?$/i';
    const DOUBLE_QUOTED_LINE_WITH_COMMENT = '/^\"(.+?)\"\s+#.*?$/i';
    const SINGLE_QUOTED_LINE = '/^\'(.+?)\'$/i';
    const DOUBLE_QUOTED_LINE = '/^\"(.*?)\"$/i';

    // Types
    const BOOLEAN = '/^(true|false)$/i';
    const NUMBER = '/^(\d+)$/';

    public function parseLine(string $line): KeyValue
    {
        $line = preg_replace(self::REGEX_EXPORT_PREFIX, '', $line);
        list($key, $value) = explode('=', $line, 2) + ['', ''];
        $key = trim($key);
        $value = trim($value);
        $matches = [];
        if (
            preg_match(self::SINGLE_QUOTED_LINE_WITH_COMMENT, $value, $matches) ||
            preg_match(self::DOUBLE_QUOTED_LINE_WITH_COMMENT, $value, $matches) ||
            preg_match(self::SINGLE_QUOTED_LINE, $value, $matches) ||
            preg_match(self::DOUBLE_QUOTED_LINE, $value, $matches)
        ) {
            $value = $matches[1];
        }

        if (preg_match(self::BOOLEAN, $value)) {
            $value = (strtolower($value) === 'true');
        } elseif (preg_match(self::NUMBER, $value)) {
            $value = intval($value);
        }

        return new KeyValue($key, $value);
    }
}