Completed
Push — master ( 328402...1cd18c )
by Julien
587:13 queued 584:54
created

ValidatorHelper::getAbsolutePathValidator()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Prompt\Helper;
4
5
use GuzzleHttp\Exception\RequestException;
6
use Symfony\Component\Console\Exception\InvalidArgumentException;
7
use TheAentMachine\Registry\RegistryClient;
8
use function Safe\sprintf;
9
use function Safe\preg_match;
10
11
final class ValidatorHelper
12
{
13
    private const DEFAULT_ERROR_MESSAGE = 'Value "%s" is invalid';
14
15
    /**
16
     * @param callable|null $v1
17
     * @param callable|null $v2
18
     * @return callable
19
     */
20
    public static function merge(?callable $v1, ?callable $v2): callable
21
    {
22
        return function (?string $response) use ($v1, $v2) {
23
            if (!empty($v1)) {
24
                $response = $v1($response);
25
            }
26
            if (!empty($v2)) {
27
                $response = $v2($response);
28
            }
29
            return $response;
30
        };
31
    }
32
33
    /**
34
     * @param callable $func
35
     * @param null|string $errorMessage
36
     * @return callable
37
     */
38
    public static function getFuncShouldReturnTrueValidator(callable $func, ?string $errorMessage = null): callable
39
    {
40
        return function (string $response) use ($func, $errorMessage) {
41
            $response = \trim($response);
42
            $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE), $response);
43
            if (!$func($response)) {
44
                throw new InvalidArgumentException($message);
45
            }
46
            return $response;
47
        };
48
    }
49
50
    /**
51
     * @param callable $func
52
     * @param null|string $errorMessage
53
     * @return callable
54
     */
55
    public static function getFuncShouldNotReturnTrueValidator(callable $func, ?string $errorMessage = null): callable
56
    {
57
        return function (string $response) use ($func, $errorMessage) {
58
            $response = \trim($response);
59
            $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE), $response);
60
            if ($func($response)) {
61
                throw new InvalidArgumentException($message);
62
            }
63
            return $response;
64
        };
65
    }
66
67
    /**
68
     * @param null|string $errorMessage
69
     * @return callable
70
     */
71
    public static function getAlphaValidator(?string $errorMessage = null): callable
72
    {
73
        return function (string $response) use ($errorMessage) {
74
            $response = \trim($response);
75
            $pattern = '/^[a-zA-Z0-9]+$/';
76
            if (!preg_match($pattern, $response)) {
77
                $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE . '. Hint: only alphanumerical characters are allowed'), $response);
78
                throw new InvalidArgumentException($message);
79
            }
80
            return $response;
81
        };
82
    }
83
84
    /**
85
     * @param string[] $additionalCharacters
86
     * @param null|string $errorMessage
87
     * @return callable
88
     */
89
    public static function getAlphaWithAdditionalCharactersValidator(array $additionalCharacters, ?string $errorMessage = null): callable
90
    {
91
        return function (string $response) use ($additionalCharacters, $errorMessage) {
92
            $response = \trim($response);
93
            $pattern = '/^[a-zA-Z0-9';
94
            foreach ($additionalCharacters as $character) {
95
                $pattern .= $character;
96
            }
97
            $pattern .= ']+$/';
98
            if (!preg_match($pattern, $response)) {
99
                $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE . '. Hint: only alphanumerical characters and "%s" characters are allowed'), $response, \implode(', ', $additionalCharacters));
100
                throw new InvalidArgumentException($message);
101
            }
102
            return $response;
103
        };
104
    }
105
106
    /**
107
     * @param null|string $errorMessage
108
     * @return callable
109
     */
110
    public static function getDomainNameValidator(?string $errorMessage = null): callable
111
    {
112
        return function (string $response) use ($errorMessage) {
113
            $response = trim($response);
114
            if (!preg_match('/^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11}?$/im', $response)) {
115
                $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE . '. Hint: the domain name must not start with "http(s)://".'), $response);
116
                throw new InvalidArgumentException($message);
117
            }
118
            return $response;
119
        };
120
    }
121
122
    /**
123
     * @param null|string $errorMessage
124
     * @return callable
125
     */
126
    public static function getDomainNameWithPortValidator(?string $errorMessage = null): callable
127
    {
128
        return function (string $response) use ($errorMessage) {
129
            $response = trim($response);
130
            if (!preg_match('/^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11}?(:\d*)?$/im', $response)) {
131
                $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE . '. Hint: the domain name must not start with "http(s)://".'), $response);
132
                throw new InvalidArgumentException($message);
133
            }
134
            return $response;
135
        };
136
    }
137
138
    /**
139
     * @return callable
140
     */
141
    public static function getDockerImageWithoutTagValidator(): callable
142
    {
143
        return function (string $response) {
144
            $response = \trim($response);
145
            if (!preg_match('/^[a-z0-9]+\/([a-z0-9]+(?:[._-][a-z0-9]+)*)$/', $response)) {
146
                $message = sprintf(self::DEFAULT_ERROR_MESSAGE . '. Hint: the docker image should be of type "username/repository"', $response);
147
                throw new InvalidArgumentException($message);
148
            }
149
            try {
150
                $registryClient = new RegistryClient();
151
                $registryClient->getImageTagsOnDockerHub($response);
152
            } catch (RequestException $e) {
153
                throw new InvalidArgumentException("The image \"$response\" does not seem to exist on Docker Hub. Try again!", $e->getCode(), $e);
154
            }
155
            return $response;
156
        };
157
    }
158
159
    /**
160
     * @param null|string $errorMessage
161
     * @return callable
162
     */
163
    public static function getIPv4Validator(?string $errorMessage = null): callable
164
    {
165
        return function (string $response) use ($errorMessage) {
166
            $response = \trim($response);
167
            if (!preg_match('/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/', $response)) {
168
                $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE), $response);
169
                throw new InvalidArgumentException($message);
170
            }
171
            return $response;
172
        };
173
    }
174
175
    /**
176
     * @param null|string $errorMessage
177
     * @return callable
178
     */
179
    public static function getAbsolutePathValidator(?string $errorMessage = null): callable
180
    {
181
        return function (string $response) use ($errorMessage) {
182
            $response = \trim($response);
183
            if (!preg_match('/^[\'"]?(?:\/[^\/\n]+)*[\'"]?$/', $response)) {
184
                $message = sprintf((!empty($errorMessage) ? $errorMessage : self::DEFAULT_ERROR_MESSAGE . '". Hint: path has to be absolute without trailing "/".'), $response);
185
                throw new InvalidArgumentException($message);
186
            }
187
            return $response;
188
        };
189
    }
190
}
191