1 | <?php |
||
2 | /** |
||
3 | * src/EncryptionDefaults.php. |
||
4 | * |
||
5 | * @author Austin Heap <[email protected]> |
||
6 | * @version v0.2.1 |
||
7 | */ |
||
8 | declare(strict_types=1); |
||
9 | |||
10 | namespace AustinHeap\Database\Encryption; |
||
11 | |||
12 | use RuntimeException; |
||
13 | |||
14 | /** |
||
15 | * EncryptionDefaults. |
||
16 | * |
||
17 | * @link https://github.com/austinheap/laravel-database-encryption |
||
18 | * @link https://packagist.org/packages/austinheap/laravel-database-encryption |
||
19 | * @link https://austinheap.github.io/laravel-database-encryption/classes/AustinHeap.Database.Encryption.EncryptionDefaults.html |
||
20 | */ |
||
21 | abstract class EncryptionDefaults |
||
22 | { |
||
23 | /** |
||
24 | * Shared default enabled flag. |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | public const DEFAULT_ENABLED = false; |
||
29 | |||
30 | /** |
||
31 | * Shared default versioning flag. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | public const DEFAULT_VERSIONING = true; |
||
36 | |||
37 | /** |
||
38 | * Shared default prefix. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public const DEFAULT_PREFIX = '__LARAVEL-DATABASE-ENCRYPTED-%VERSION%__'; |
||
43 | |||
44 | /** |
||
45 | * Shared default control characters. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | public const DEFAULT_CONTROL_CHARACTERS = [ |
||
50 | 'header' => [ |
||
51 | 'start' => 1, |
||
52 | 'stop' => 4, |
||
53 | ], |
||
54 | 'prefix' => [ |
||
55 | 'start' => 2, |
||
56 | 'stop' => 3, |
||
57 | ], |
||
58 | 'field' => [ |
||
59 | 'start' => 30, |
||
60 | 'delimiter' => 25, |
||
61 | 'stop' => 23, |
||
62 | ], |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Shared default helpers. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | public const DEFAULT_HELPERS = [ |
||
71 | 'database_encryption', |
||
72 | 'db_encryption', |
||
73 | 'dbencryption', |
||
74 | 'database_encrypt', |
||
75 | 'db_encrypt', |
||
76 | 'dbencrypt', |
||
77 | 'database_decrypt', |
||
78 | 'db_decrypt', |
||
79 | 'dbdecrypt', |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Private default control characters cache. |
||
84 | * |
||
85 | * @var null|array |
||
86 | */ |
||
87 | private static $defaultControlCharactersCache = null; |
||
88 | |||
89 | /** |
||
90 | * Private default prefix cache. |
||
91 | * |
||
92 | * @var null|string |
||
93 | */ |
||
94 | private static $defaultPrefixCache = null; |
||
95 | |||
96 | /** |
||
97 | * @return bool |
||
98 | */ |
||
99 | 1 | public static function isEnabledDefault(): bool |
|
100 | { |
||
101 | 1 | return static::DEFAULT_ENABLED; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return bool |
||
106 | */ |
||
107 | 1 | public static function isDisabledDefault(): bool |
|
108 | { |
||
109 | 1 | return ! static::isEnabledDefault(); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return bool |
||
114 | */ |
||
115 | 1 | public static function isVersioningDefault(): bool |
|
116 | { |
||
117 | 1 | return static::DEFAULT_VERSIONING; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | 1 | public static function isVersionlessDefault(): bool |
|
124 | { |
||
125 | 1 | return ! static::isVersioningDefault(); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | 1 | public static function getPrefixDefault(): string |
|
132 | { |
||
133 | 1 | return static::DEFAULT_PREFIX; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return array |
||
138 | */ |
||
139 | 23 | public static function getControlCharactersDefault(?string $type = null, bool $raw = false): array |
|
0 ignored issues
–
show
|
|||
140 | { |
||
141 | 23 | if (is_null(self::$defaultControlCharactersCache)) { |
|
142 | 1 | $characters = []; |
|
143 | |||
144 | 1 | foreach (self::DEFAULT_CONTROL_CHARACTERS as $control => $config) { |
|
145 | 1 | $characters[$control] = []; |
|
146 | |||
147 | 1 | foreach (['start', 'delimiter', 'stop'] as $mode) { |
|
148 | 1 | if (isset($config[$mode])) { |
|
149 | 1 | $characters[$control][$mode] = self::buildCharacter($config[$mode], true); |
|
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | 1 | self::$defaultControlCharactersCache = $characters; |
|
155 | } |
||
156 | |||
157 | 23 | if (! is_null($type)) { |
|
158 | 1 | throw_if(! array_key_exists($type, self::$defaultControlCharactersCache), RuntimeException::class, |
|
159 | 1 | 'Default control characters do not exist for $type: "'.(empty($type) ? '(empty)' : $type).'".'); |
|
160 | |||
161 | 1 | return self::$defaultControlCharactersCache[$type]; |
|
162 | } |
||
163 | |||
164 | 22 | return self::$defaultControlCharactersCache; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | 86 | public static function getHelpersDefault(): array |
|
171 | { |
||
172 | 86 | return static::DEFAULT_HELPERS; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Builds array of character information from character. |
||
177 | * |
||
178 | * @return array |
||
179 | */ |
||
180 | 1 | public static function buildCharacter($character, bool $default = false): array |
|
181 | { |
||
182 | 1 | throw_if(! is_int($character) && ! is_string($character), RuntimeException::class, |
|
183 | 1 | 'Cannot build character array from $character type: "'.gettype($character).'".'); |
|
184 | |||
185 | return [ |
||
186 | 1 | 'int' => is_int($character) ? $character : ord($character), |
|
187 | 1 | 'string' => is_string($character) ? $character : chr($character), |
|
188 | 1 | 'default' => $default, |
|
189 | ]; |
||
190 | } |
||
191 | } |
||
192 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.