Config.php
2.36 KB
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
<?php
/**
* @license GPL-3.0-or-later
*
* Modified by learndash on 20-September-2023 using Strauss.
* @see https://github.com/BrianHenryIE/strauss
*/
namespace StellarWP\Learndash\StellarWP\Models;
use InvalidArgumentException;
class Config {
/**
* @var ?string
*/
protected static $hookPrefix;
/**
* @var string
*/
protected static $invalidArgumentException = InvalidArgumentException::class;
/**
* Gets the hook prefix.
*
* @since 1.0.0
*
* @return string
*/
public static function getHookPrefix(): string {
if ( ! static::$hookPrefix ) {
throw new \RuntimeException(
sprintf(
'You must provide a hook prefix via %1$s before using the stellarwp/models library.',
__CLASS__ . '::setHookPrefix()'
)
);
}
return static::$hookPrefix;
}
/**
* Gets the InvalidArgumentException class.
*
* @since 1.0.0
*
* @return string
*/
public static function getInvalidArgumentException(): string {
return static::$invalidArgumentException;
}
/**
* Resets the class back to default.
*
* @since 1.0.0
*
* @return void
*/
public static function reset() {
static::$hookPrefix = null;
static::$invalidArgumentException = InvalidArgumentException::class;
}
/**
* Sets the hook prefix.
*
* @since 1.0.0
*
* @param string $hook_prefix
*
* @return void
*/
public static function setHookPrefix( string $hook_prefix ) {
if ( ! empty( static::$hookPrefix ) ) {
throw new \RuntimeException(
sprintf(
'The %1$s has already been called and set to %2$s.',
__CLASS__ . '::setHookPrefix()',
static::$hookPrefix
)
);
}
$sanitized_prefix = preg_replace( '/[^a-z0-9_-]/', '', $hook_prefix );
if ( $sanitized_prefix !== $hook_prefix ) {
throw new \InvalidArgumentException( 'Hook prefix must only contain lowercase letters, numbers, "_", or "-".' );
}
static::$hookPrefix = $hook_prefix;
}
/**
* Allow for overriding the InvalidArgumentException class.
*
* @since 1.0.0
*
* @param string $class
*
* @return void
*/
public static function setInvalidArgumentException( string $class ) {
if ( ! is_a( $class, InvalidArgumentException::class, true ) ) {
throw new \InvalidArgumentException( 'The provided InvalidArgumentException class must be or must extend ' . InvalidArgumentException::class . '.' );
}
static::$invalidArgumentException = $class;
}
}