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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
<?php
declare(strict_types=1);
namespace StellarWP\Validation;
use InvalidArgumentException; use RuntimeException; use StellarWP\ContainerContract\ContainerInterface; use StellarWP\Validation\Exceptions\Contracts\ValidationExceptionInterface; use StellarWP\Validation\Exceptions\ValidationException;
/** * Sets up the validation library for use within the application. It provides a way of overriding various parts of the * library to better integrate into the adopting application. * * @since 1.0.0 */ class Config { /** * @var ContainerInterface The service container to use for resolving dependencies. */ private static $container;
/** * @var string The prefix to add to action and hook filters in the library */ private static $hookPrefix = '';
/** * @var class-string<ValidationExceptionInterface> */ private static $validationExceptionClass = ValidationException::class;
/** * @var class-string<InvalidArgumentException> */ private static $invalidArgumentExceptionClass = InvalidArgumentException::class;
/** * @var bool Whether the library has already been initialized */ private static $initialized = false;
/** * @since 1.0.0 * * @param ContainerInterface $container */ public static function setServiceContainer($container) { self::$container = $container; }
/** * @since 1.0.0 * * @return ContainerInterface */ public static function getServiceContainer() { return self::$container; }
/** * @since 1.0.0 */ public static function getHookPrefix(): string { return self::$hookPrefix; }
/** * @since 1.0.0 */ public static function setHookPrefix(string $prefix) { self::$hookPrefix = $prefix; }
/** * @since 1.0.0 * * @throws ValidationExceptionInterface */ public static function throwValidationException() { throw new self::$validationExceptionClass(...func_get_args()); }
/** * @since 1.0.0 * * @return class-string<ValidationExceptionInterface> */ public static function getValidationExceptionClass(): string { return self::$validationExceptionClass; }
/** * @since 1.0.0 * * @param class-string<ValidationException> $validationExceptionClass */ public static function setValidationExceptionClass(string $validationExceptionClass) { if (!is_a($validationExceptionClass, ValidationExceptionInterface::class, true)) { throw new RuntimeException( 'The validation exception class must implement the ValidationExceptionInterface' ); }
self::$validationExceptionClass = $validationExceptionClass; }
/** * @since 1.0.0 * * @throws InvalidArgumentException */ public static function throwInvalidArgumentException() { throw new self::$invalidArgumentExceptionClass(...func_get_args()); }
/** * @since 1.0.0 * * @return class-string<InvalidArgumentException> */ public static function getInvalidArgumentExceptionClass(): string { return self::$invalidArgumentExceptionClass; }
/** * @since 1.0.0 * * @param class-string<InvalidArgumentException> $invalidArgumentExceptionClass */ public static function setInvalidArgumentExceptionClass(string $invalidArgumentExceptionClass) { if (!is_a($invalidArgumentExceptionClass, InvalidArgumentException::class, true)) { throw new RuntimeException( 'The invalid argument exception class must extend the InvalidArgumentException' ); }
self::$invalidArgumentExceptionClass = $invalidArgumentExceptionClass; }
/** * @since 1.0.0 * * @return void */ public static function initialize() { if (self::$initialized) { return; }
if (empty(self::$container)) { throw new RuntimeException('A service container must be set before initializing the library'); }
$serviceProvider = new ServiceProvider(); $serviceProvider->register(); self::$initialized = true; } }
|