1 | <?php namespace Limoncello\Flute\Package; |
||
22 | abstract class FluteSettings implements FluteSettingsInterface |
||
23 | { |
||
24 | use ClassIsTrait; |
||
25 | |||
26 | /** |
||
27 | * Namespace for string resources. |
||
28 | */ |
||
29 | const GENERIC_NAMESPACE = Messages::RESOURCES_NAMESPACE; |
||
30 | |||
31 | /** |
||
32 | * Namespace for string resources. |
||
33 | */ |
||
34 | public const VALIDATION_NAMESPACE = 'Limoncello.Flute.Validation'; |
||
35 | |||
36 | /** |
||
37 | * Default page size. |
||
38 | */ |
||
39 | public const DEFAULT_PAGE_SIZE = 10; |
||
40 | |||
41 | /** |
||
42 | * Default page size. |
||
43 | */ |
||
44 | public const DEFAULT_MAX_PAGE_SIZE = 30; |
||
45 | |||
46 | /** |
||
47 | * Default JSON API version. |
||
48 | */ |
||
49 | public const DEFAULT_JSON_API_VERSION = '1.0'; |
||
50 | |||
51 | /** Serialized validation data index */ |
||
52 | protected const JSON_API_DATA_VALIDATION_RULES_SERIALIZED = 0; |
||
53 | |||
54 | /** Serialized validation data index */ |
||
55 | protected const JSON_API_QUERIES_VALIDATION_RULES_SERIALIZED = self::JSON_API_DATA_VALIDATION_RULES_SERIALIZED + 1; |
||
56 | |||
57 | /** |
||
58 | * @param array $appConfig |
||
59 | * |
||
60 | * @return array |
||
61 | * |
||
62 | * @throws ReflectionException |
||
63 | */ |
||
64 | 30 | final public function get(array $appConfig): array |
|
65 | { |
||
66 | 30 | $defaults = $this->getSettings(); |
|
67 | |||
68 | 30 | $defaults[static::KEY_ROUTES_FOLDER] = $appConfig[A::KEY_ROUTES_FOLDER]; |
|
69 | 30 | $defaults[static::KEY_WEB_CONTROLLERS_FOLDER] = $appConfig[A::KEY_WEB_CONTROLLERS_FOLDER]; |
|
70 | |||
71 | 30 | $apiFolder = $defaults[static::KEY_API_FOLDER] ?? null; |
|
72 | 30 | $valRulesFolder = $defaults[static::KEY_JSON_VALIDATION_RULES_FOLDER] ?? null; |
|
73 | 30 | $jsonCtrlFolder = $defaults[static::KEY_JSON_CONTROLLERS_FOLDER] ?? null; |
|
74 | 30 | $schemasFolder = $defaults[static::KEY_SCHEMAS_FOLDER] ?? null; |
|
75 | 30 | $schemasFileMask = $defaults[static::KEY_SCHEMAS_FILE_MASK] ?? null; |
|
76 | 30 | $jsonDataValFolder = $defaults[static::KEY_JSON_VALIDATORS_FOLDER] ?? null; |
|
77 | 30 | $jsonDataValFileMask = $defaults[static::KEY_JSON_VALIDATORS_FILE_MASK] ?? null; |
|
78 | 30 | $formsValFolder = $defaults[static::KEY_FORM_VALIDATORS_FOLDER] ?? null; |
|
79 | 30 | $formsValFileMask = $defaults[static::KEY_FORM_VALIDATORS_FILE_MASK] ?? null; |
|
80 | 30 | $jsonQueryValFolder = $defaults[static::KEY_QUERY_VALIDATORS_FOLDER] ?? null; |
|
81 | 30 | $jsonQueryValFileMask = $defaults[static::KEY_QUERY_VALIDATORS_FILE_MASK] ?? null; |
|
82 | |||
83 | 30 | assert( |
|
84 | 30 | $apiFolder !== null && empty(glob($apiFolder)) === false, |
|
85 | 30 | "Invalid API folder `$apiFolder`." |
|
86 | ); |
||
87 | 30 | assert( |
|
88 | 30 | $valRulesFolder !== null && empty(glob($valRulesFolder)) === false, |
|
89 | 30 | "Invalid validation rules folder `$valRulesFolder`." |
|
90 | ); |
||
91 | 30 | assert( |
|
92 | 30 | $jsonCtrlFolder !== null && empty(glob($jsonCtrlFolder)) === false, |
|
93 | 30 | "Invalid JSON API controllers' folder `$jsonCtrlFolder`." |
|
94 | ); |
||
95 | 30 | assert( |
|
96 | 30 | $schemasFolder !== null && empty(glob($schemasFolder)) === false, |
|
97 | 30 | "Invalid Schemas folder `$schemasFolder`." |
|
98 | ); |
||
99 | 30 | assert(empty($schemasFileMask) === false, "Invalid Schemas file mask `$schemasFileMask`."); |
|
100 | 30 | assert( |
|
101 | 30 | $jsonDataValFolder !== null && empty(glob($jsonDataValFolder)) === false, |
|
102 | 30 | "Invalid JSON Validators folder `$jsonDataValFolder`." |
|
103 | ); |
||
104 | 30 | assert(empty($jsonDataValFileMask) === false, "Invalid JSON Validators file mask `$jsonDataValFileMask`."); |
|
105 | 30 | assert( |
|
106 | 30 | $formsValFolder !== null && empty(glob($formsValFolder)) === false, |
|
107 | 30 | "Invalid Forms Validators folder `$formsValFolder`." |
|
108 | ); |
||
109 | 30 | assert(empty($formsValFileMask) === false, "Invalid Forms Validators file mask `$formsValFileMask`."); |
|
110 | 30 | assert( |
|
111 | 30 | $jsonQueryValFolder !== null && empty(glob($jsonQueryValFolder)) === false, |
|
112 | 30 | "Invalid Query Validators folder `$jsonQueryValFolder`." |
|
113 | ); |
||
114 | 30 | assert(empty($jsonQueryValFileMask) === false, "Invalid Query Validators file mask `$jsonQueryValFileMask`."); |
|
115 | |||
116 | 30 | $schemasPath = $schemasFolder . DIRECTORY_SEPARATOR . $schemasFileMask; |
|
117 | 30 | $jsonDataValPath = $jsonDataValFolder . DIRECTORY_SEPARATOR . $jsonDataValFileMask; |
|
118 | 30 | $formsValidatorsPath = $formsValFolder . DIRECTORY_SEPARATOR . $formsValFileMask; |
|
119 | 30 | $jsonQueryValPath = $jsonQueryValFolder . DIRECTORY_SEPARATOR . $jsonQueryValFileMask; |
|
120 | |||
121 | 30 | $requireUniqueTypes = $defaults[static::KEY_SCHEMAS_REQUIRE_UNIQUE_TYPES] ?? true; |
|
122 | |||
123 | 30 | $doNotLogExceptions = $defaults[static::KEY_DO_NOT_LOG_EXCEPTIONS_LIST] ?? []; |
|
124 | 30 | unset($defaults[static::KEY_DO_NOT_LOG_EXCEPTIONS_LIST]); |
|
125 | |||
126 | 30 | [$modelToSchemaMap, $typeToSchemaMap] = $this->createSchemaMapping($schemasPath, $requireUniqueTypes); |
|
127 | |||
128 | return $defaults + [ |
||
129 | 30 | static::KEY_DO_NOT_LOG_EXCEPTIONS_LIST__AS_KEYS => array_flip($doNotLogExceptions), |
|
130 | |||
131 | 30 | static::KEY_MODEL_TO_SCHEMA_MAP => $modelToSchemaMap, |
|
132 | 30 | static::KEY_TYPE_TO_SCHEMA_MAP => $typeToSchemaMap, |
|
133 | |||
134 | 30 | static::KEY_JSON_VALIDATION_RULE_SETS_DATA => |
|
135 | 30 | $this->serializeJsonValidationRules($jsonDataValPath, $jsonQueryValPath), |
|
136 | |||
137 | 30 | static::KEY_ATTRIBUTE_VALIDATION_RULE_SETS_DATA => |
|
138 | 30 | $this->serializeFormValidationRules($formsValidatorsPath), |
|
139 | ]; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | 30 | protected function getSettings(): array |
|
170 | |||
171 | /** |
||
172 | * @param string $schemasPath |
||
173 | * @param bool $requireUniqueTypes |
||
174 | * |
||
175 | * @return array |
||
176 | * |
||
177 | * @throws ReflectionException |
||
178 | */ |
||
179 | 30 | private function createSchemaMapping(string $schemasPath, bool $requireUniqueTypes): array |
|
208 | |||
209 | /** |
||
210 | * @param string $rulesPath |
||
211 | * @param string $queriesValPath |
||
212 | * |
||
213 | * @return array |
||
214 | * |
||
215 | * @throws ReflectionException |
||
216 | */ |
||
217 | 30 | private function serializeJsonValidationRules(string $rulesPath, string $queriesValPath): array |
|
239 | |||
240 | /** |
||
241 | * @param string $formsValPath |
||
242 | * |
||
243 | * @return array |
||
244 | * |
||
245 | * @throws ReflectionException |
||
246 | */ |
||
247 | 30 | private function serializeFormValidationRules(string $formsValPath): array |
|
257 | |||
258 | // serialization above makes some assumptions about format of returned data |
||
259 | // the methods below help to deal with the data encapsulation |
||
260 | |||
261 | /** |
||
262 | * @param array $fluteSettings |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | 12 | public static function getJsonDataSerializedRules(array $fluteSettings): array |
|
273 | |||
274 | /** |
||
275 | * @param array $fluteSettings |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | 21 | public static function getJsonQuerySerializedRules(array $fluteSettings): array |
|
286 | |||
287 | /** |
||
288 | * @param array $fluteSettings |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 1 | public static function getFormSerializedRules(array $fluteSettings): array |
|
296 | } |
||
297 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.