11<?php
2-
3- namespace Kitodo \Dlf \Middleware ;
4-
52/**
63 * (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
74 *
129 * LICENSE.txt file that was distributed with this source code.
1310 */
1411
12+ namespace Kitodo \Dlf \Middleware ;
13+
1514use DOMDocument ;
1615use InvalidArgumentException ;
1716use Kitodo \Dlf \Validation \DOMDocumentValidationStack ;
2423use TYPO3 \CMS \Core \Localization \LanguageService ;
2524use TYPO3 \CMS \Core \Localization \LanguageServiceFactory ;
2625use TYPO3 \CMS \Core \Log \LogManager ;
26+ use TYPO3 \CMS \Core \Site \Entity \SiteLanguage ;
2727use TYPO3 \CMS \Core \TypoScript \TypoScriptService ;
2828use TYPO3 \CMS \Core \Utility \GeneralUtility ;
2929use TYPO3 \CMS \Core \Utility \PathUtility ;
@@ -116,7 +116,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
116116 throw new InvalidArgumentException ('Validation configuration type does not exist. ' , 1744373532 );
117117 }
118118
119- $ validationConfiguration = $ settings ['domDocumentValidation ' ][$ typeParam ];
119+ $ validationConfiguration = $ this ->removeDisabledValidators ($ parameters , $ settings ['domDocumentValidation ' ][$ typeParam ]);
120+
120121 $ validation = GeneralUtility::makeInstance (DOMDocumentValidationStack::class, $ validationConfiguration );
121122 // validate and return json response
122123 return $ this ->getJsonResponse ($ validationConfiguration , $ validation ->validate ($ document ));
@@ -159,11 +160,45 @@ protected function getJsonResponse(array $configurations, ?Result $result): Resp
159160 return $ response ;
160161 }
161162
162- private function getTranslation (string $ key , ?array $ arguments = null ): string
163+ /**
164+ * Get the message.
165+ *
166+ * @return \Closure
167+ */
168+ protected function getMessageText (): \Closure
169+ {
170+ return function (Message $ message ): string {
171+ return $ message ->getMessage ();
172+ };
173+ }
174+
175+ /**
176+ * Removes validators marked as disabled from the configuration.
177+ *
178+ * If a validator in the configuration has the disabled flag set to true and its key does not exist in the enabledValidators parameter, it will be removed.
179+ *
180+ * @param array $parameters The parameters of the middleware.
181+ * @param array $validationConfiguration The validation configuration to remove from
182+ * @return array The validator configuration without the disabled validators
183+ */
184+ protected function removeDisabledValidators (array $ parameters , array $ validationConfiguration ): array
185+ {
186+ $ enableValidators = [];
187+ if (array_key_exists ("enableValidators " , $ parameters )) {
188+ $ enableValidators = explode (", " , $ parameters ['enableValidators ' ]);
189+ }
190+ foreach ($ validationConfiguration as $ key => $ value ) {
191+ if (isset ($ value ['disabled ' ]) && $ value ['disabled ' ] === "true " && !in_array ($ key , $ enableValidators )) {
192+ unset($ validationConfiguration [$ key ]);
193+ }
194+ }
195+ return $ validationConfiguration ;
196+ }
197+
198+ protected function getTranslation (string $ key , ?array $ arguments = null ): string
163199 {
164- $ language =
165- $ this ->request ->getAttribute ('language ' )
166- ?? $ this ->request ->getAttribute ('site ' )->getDefaultLanguage ();
200+ /** @var SiteLanguage $siteLanguage */
201+ $ siteLanguage = $ this ->request ->getAttribute ('language ' ) ?? $ this ->request ->getAttribute ('site ' )->getDefaultLanguage ();
167202
168203 /** @var LanguageServiceFactory $languageServiceFactory */
169204 $ languageServiceFactory = GeneralUtility::makeInstance (
@@ -172,7 +207,7 @@ private function getTranslation(string $key, ?array $arguments = null): string
172207
173208 /** @var LanguageService $languageService */
174209 $ languageService = $ languageServiceFactory
175- ->createFromSiteLanguage ($ language );
210+ ->createFromSiteLanguage ($ siteLanguage );
176211
177212 if (isset ($ arguments ) && count ($ arguments ) > 0 ) {
178213 return vsprintf (
@@ -182,16 +217,4 @@ private function getTranslation(string $key, ?array $arguments = null): string
182217 }
183218 return $ languageService ->sL ($ key );
184219 }
185-
186- /**
187- * Get the message.
188- *
189- * @return \Closure
190- */
191- private function getMessageText (): \Closure
192- {
193- return function (Message $ message ): string {
194- return $ message ->getMessage ();
195- };
196- }
197220}
0 commit comments