Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Dotenv;
6
 
7
use Dotenv\Exception\ValidationException;
8
use Dotenv\Repository\RepositoryInterface;
9
use Dotenv\Util\Regex;
10
use Dotenv\Util\Str;
11
 
12
class Validator
13
{
14
    /**
15
     * The environment repository instance.
16
     *
17
     * @var \Dotenv\Repository\RepositoryInterface
18
     */
19
    private $repository;
20
 
21
    /**
22
     * The variables to validate.
23
     *
24
     * @var string[]
25
     */
26
    private $variables;
27
 
28
    /**
29
     * Create a new validator instance.
30
     *
31
     * @param \Dotenv\Repository\RepositoryInterface $repository
32
     * @param string[]                               $variables
33
     *
34
     * @throws \Dotenv\Exception\ValidationException
35
     *
36
     * @return void
37
     */
38
    public function __construct(RepositoryInterface $repository, array $variables)
39
    {
40
        $this->repository = $repository;
41
        $this->variables = $variables;
42
    }
43
 
44
    /**
45
     * Assert that each variable is present.
46
     *
47
     * @throws \Dotenv\Exception\ValidationException
48
     *
49
     * @return \Dotenv\Validator
50
     */
51
    public function required()
52
    {
53
        return $this->assert(
54
            static function (?string $value) {
55
                return $value !== null;
56
            },
57
            'is missing'
58
        );
59
    }
60
 
61
    /**
62
     * Assert that each variable is not empty.
63
     *
64
     * @throws \Dotenv\Exception\ValidationException
65
     *
66
     * @return \Dotenv\Validator
67
     */
68
    public function notEmpty()
69
    {
70
        return $this->assertNullable(
71
            static function (string $value) {
72
                return Str::len(\trim($value)) > 0;
73
            },
74
            'is empty'
75
        );
76
    }
77
 
78
    /**
79
     * Assert that each specified variable is an integer.
80
     *
81
     * @throws \Dotenv\Exception\ValidationException
82
     *
83
     * @return \Dotenv\Validator
84
     */
85
    public function isInteger()
86
    {
87
        return $this->assertNullable(
88
            static function (string $value) {
89
                return \ctype_digit($value);
90
            },
91
            'is not an integer'
92
        );
93
    }
94
 
95
    /**
96
     * Assert that each specified variable is a boolean.
97
     *
98
     * @throws \Dotenv\Exception\ValidationException
99
     *
100
     * @return \Dotenv\Validator
101
     */
102
    public function isBoolean()
103
    {
104
        return $this->assertNullable(
105
            static function (string $value) {
106
                if ($value === '') {
107
                    return false;
108
                }
109
 
110
                return \filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE) !== null;
111
            },
112
            'is not a boolean'
113
        );
114
    }
115
 
116
    /**
117
     * Assert that each variable is amongst the given choices.
118
     *
119
     * @param string[] $choices
120
     *
121
     * @throws \Dotenv\Exception\ValidationException
122
     *
123
     * @return \Dotenv\Validator
124
     */
125
    public function allowedValues(array $choices)
126
    {
127
        return $this->assertNullable(
128
            static function (string $value) use ($choices) {
129
                return \in_array($value, $choices, true);
130
            },
131
            \sprintf('is not one of [%s]', \implode(', ', $choices))
132
        );
133
    }
134
 
135
    /**
136
     * Assert that each variable matches the given regular expression.
137
     *
138
     * @param string $regex
139
     *
140
     * @throws \Dotenv\Exception\ValidationException
141
     *
142
     * @return \Dotenv\Validator
143
     */
144
    public function allowedRegexValues(string $regex)
145
    {
146
        return $this->assertNullable(
147
            static function (string $value) use ($regex) {
148
                return Regex::matches($regex, $value)->success()->getOrElse(false);
149
            },
150
            \sprintf('does not match "%s"', $regex)
151
        );
152
    }
153
 
154
    /**
155
     * Assert that the callback returns true for each variable.
156
     *
157
     * @param callable(?string):bool $callback
158
     * @param string                 $message
159
     *
160
     * @throws \Dotenv\Exception\ValidationException
161
     *
162
     * @return \Dotenv\Validator
163
     */
164
    public function assert(callable $callback, string $message)
165
    {
166
        $failing = [];
167
 
168
        foreach ($this->variables as $variable) {
169
            if ($callback($this->repository->get($variable)) === false) {
170
                $failing[] = \sprintf('%s %s', $variable, $message);
171
            }
172
        }
173
 
174
        if (\count($failing) > 0) {
175
            throw new ValidationException(\sprintf(
176
                'One or more environment variables failed assertions: %s.',
177
                \implode(', ', $failing)
178
            ));
179
        }
180
 
181
        return $this;
182
    }
183
 
184
    /**
185
     * Assert that the callback returns true for each variable.
186
     *
187
     * Skip checking null variable values.
188
     *
189
     * @param callable(string):bool $callback
190
     * @param string                $message
191
     *
192
     * @throws \Dotenv\Exception\ValidationException
193
     *
194
     * @return \Dotenv\Validator
195
     */
196
    public function assertNullable(callable $callback, string $message)
197
    {
198
        return $this->assert(
199
            static function (?string $value) use ($callback) {
200
                if ($value === null) {
201
                    return true;
202
                }
203
 
204
                return $callback($value);
205
            },
206
            $message
207
        );
208
    }
209
}