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 | use function is_dir; |
||
20 | use function is_iterable; |
||
21 | use function is_null; |
||
22 | use function is_string; |
||
23 | use function is_writable; |
||
24 | use function sprintf; |
||
25 | |||
26 | /** |
||
27 | * This assert trait provides methods to check conditions. |
||
28 | */ |
||
29 | trait AssertTrait |
||
30 | { |
||
31 | /** |
||
32 | * Check if the variable is string or not. |
||
33 | * |
||
34 | * @param string $var The variable will be checked. |
||
35 | * |
||
36 | * @return void |
||
37 | * |
||
38 | * @throws CacheArgumentException |
||
39 | */ |
||
40 | 108 | protected function assertArgumentString($value): void |
|
41 | { |
||
42 | 108 | if (!is_string($value)) { |
|
43 | 8 | throw new CacheArgumentException( |
|
44 | 8 | sprintf( |
|
45 | 8 | 'The type of value must be string, but "%s" provided.', |
|
46 | 8 | gettype($value) |
|
47 | ) |
||
48 | ); |
||
49 | } |
||
50 | 100 | } |
|
51 | |||
52 | /** |
||
53 | * Check if the variable is iterable or not. |
||
54 | * |
||
55 | * @param iterable $value The variable will be checked. |
||
56 | * |
||
57 | * @return void |
||
58 | * |
||
59 | * @throws CacheArgumentException |
||
60 | */ |
||
61 | 40 | protected function assertArgumentIterable($value): void |
|
62 | { |
||
63 | 40 | if (!is_iterable($value)) { |
|
64 | 2 | throw new CacheArgumentException( |
|
65 | 2 | sprintf( |
|
66 | 2 | 'The type of value must be iterable, but "%s" provided.', |
|
67 | 2 | gettype($value) |
|
68 | ) |
||
69 | ); |
||
70 | } |
||
71 | 38 | } |
|
72 | |||
73 | /** |
||
74 | * Check if the TTL is valid type or not. |
||
75 | * |
||
76 | * @param int|null|DateInterval $ttl The time to live of a cached data. |
||
77 | * |
||
78 | * @return void |
||
79 | * |
||
80 | * @throws CacheArgumentException |
||
81 | */ |
||
82 | 100 | protected function assertValidTypeOfTtl($ttl): void |
|
83 | { |
||
84 | 100 | if (!is_null($ttl) && |
|
85 | 100 | !is_integer($ttl) && |
|
86 | 100 | !($ttl instanceof DateInterval) |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
87 | ) { |
||
88 | 4 | throw new CacheArgumentException( |
|
89 | 4 | sprintf( |
|
90 | 4 | 'The TTL only accetps int, null and DateInterval instance, but "%s" provided.', |
|
91 | 4 | gettype($ttl) |
|
92 | ) |
||
93 | ); |
||
94 | } |
||
95 | 96 | } |
|
96 | |||
97 | /** |
||
98 | * Check if a directory exists and is writable. |
||
99 | * |
||
100 | * @param string $directory The path of a directory. |
||
101 | * |
||
102 | * @return void |
||
103 | * |
||
104 | * @throws CacheException |
||
105 | */ |
||
106 | 48 | protected function assertDirectoryWritable(string $directory): void |
|
107 | { |
||
108 | 48 | if (!is_dir($directory)) { |
|
109 | 2 | throw new CacheException( |
|
110 | 2 | sprintf( |
|
111 | 2 | 'The directory of the storage does not exist. ( %s )', |
|
112 | 2 | $directory |
|
113 | ) |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | // @codeCoverageIgnoreStart |
||
118 | |||
119 | if (!is_writable($directory)) { |
||
120 | throw new CacheException( |
||
121 | sprintf( |
||
122 | 'The directory of the storage must be wriable. ( %s )', |
||
123 | $directory |
||
124 | ) |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | // @codeCoverageIgnoreEnd |
||
129 | 46 | } |
|
130 | |||
131 | /** |
||
132 | * Check if a setting field is empty or not. |
||
133 | * |
||
134 | * @param array $settings The array of the settings. |
||
135 | * |
||
136 | * @return void |
||
137 | * |
||
138 | * @throws CacheArgumentException |
||
139 | */ |
||
140 | 16 | protected function assertSettingFields($settings): void |
|
141 | { |
||
142 | 16 | foreach ($settings as $k => $v) { |
|
143 | 16 | if (empty($v)) { |
|
144 | 2 | throw new CacheArgumentException( |
|
145 | 2 | sprintf( |
|
146 | 2 | 'The setting field "%s" cannot be empty or null', |
|
147 | 16 | $k |
|
148 | ) |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | 14 | } |
|
153 | } |
||
154 |