1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon Simple Cache package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\SimpleCache; |
14
|
|
|
|
15
|
|
|
use Shieldon\SimpleCache\Exception\CacheArgumentException; |
16
|
|
|
use Shieldon\SimpleCache\Exception\CacheException; |
17
|
|
|
use DateInterval; |
18
|
|
|
use function gettype; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This assert trait provides methods to check conditions. |
22
|
|
|
*/ |
23
|
|
|
Trait AssertTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Check if the variable is string or not. |
27
|
|
|
* |
28
|
|
|
* @param string $var The variable will be checked. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
* |
32
|
|
|
* @throws CacheArgumentException |
33
|
|
|
*/ |
34
|
62 |
|
protected function assertArgumentString($value): void |
35
|
|
|
{ |
36
|
62 |
|
if (!is_string($value)) { |
37
|
8 |
|
throw new CacheArgumentException( |
38
|
8 |
|
sprintf( |
39
|
8 |
|
'The type of value must be string, but "%s" provided.', |
40
|
8 |
|
gettype($value) |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
} |
44
|
54 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check if the variable is iterable or not. |
48
|
|
|
* |
49
|
|
|
* @param iterable $value The variable will be checked. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
* |
53
|
|
|
* @throws CacheArgumentException |
54
|
|
|
*/ |
55
|
24 |
|
protected function assertArgumentIterable($value): void |
56
|
|
|
{ |
57
|
24 |
|
if (!is_iterable($value)) { |
58
|
2 |
|
throw new CacheArgumentException( |
59
|
2 |
|
sprintf( |
60
|
2 |
|
'The type of value must be iterable, but "%s" provided.', |
61
|
2 |
|
gettype($value) |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
22 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if the TTL is valid type or not. |
69
|
|
|
* |
70
|
|
|
* @param int|null|DateInterval $ttl The time to live of a cached data. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
* |
74
|
|
|
* @throws CacheArgumentException |
75
|
|
|
*/ |
76
|
54 |
|
protected function assertValidTypeOfTtl($ttl): void |
77
|
|
|
{ |
78
|
|
|
if ( |
79
|
54 |
|
!is_null($ttl) && |
80
|
54 |
|
!is_integer($ttl) && |
81
|
54 |
|
!($ttl instanceof DateInterval) |
|
|
|
|
82
|
|
|
) { |
83
|
4 |
|
throw new CacheArgumentException( |
84
|
4 |
|
sprintf( |
85
|
4 |
|
'The TTL only accetps int, null and DateInterval instance, but "%s" provided.', |
86
|
4 |
|
gettype($ttl) |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
50 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check if a directory exists and is writable. |
94
|
|
|
* |
95
|
|
|
* @param string $directory The path of a directory. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
* |
99
|
|
|
* @throws CacheException |
100
|
|
|
*/ |
101
|
36 |
|
protected function assertDirectoryWritable(string $directory): void |
102
|
|
|
{ |
103
|
36 |
|
if (!is_dir($directory)) { |
104
|
2 |
|
throw new CacheException( |
105
|
2 |
|
'The directory of the storage does not exist.' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// @codeCoverageIgnoreStart |
110
|
|
|
|
111
|
|
|
if (!is_writable($directory)) { |
112
|
|
|
throw new CacheException( |
113
|
|
|
'The directory of the storage must be wriable' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// @codeCoverageIgnoreEnd |
118
|
34 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check if a setting field is empty or not. |
122
|
|
|
* |
123
|
|
|
* @param array $settings The array of the settings. |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
* |
127
|
|
|
* @throws CacheArgumentException |
128
|
|
|
*/ |
129
|
10 |
|
protected function assertSettingFields($settings): void |
130
|
|
|
{ |
131
|
10 |
|
foreach ($settings as $k => $v) { |
132
|
10 |
|
if (empty($v)) { |
133
|
2 |
|
throw new CacheArgumentException( |
134
|
2 |
|
sprintf( |
135
|
2 |
|
'The setting field "%s" cannot be empty or null', |
136
|
10 |
|
$k |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |