Passed
Pull Request — master (#232)
by Romain
03:09
created

TcaRegexEval::evaluateFieldValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 24
rs 9.7998
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Service\TCA;
19
20
use CuyZ\Notiz\Service\LocalizationService;
21
use TYPO3\CMS\Core\Messaging\FlashMessage;
22
use TYPO3\CMS\Core\Messaging\FlashMessageService;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
use function preg_last_error;
27
use function preg_match;
28
29
final class TcaRegexEval implements SingletonInterface
30
{
31
    /**
32
     * Will check if the given value has a correct regex syntax. Sends a flash
33
     * message and resets the value if not.
34
     *
35
     * @param string $value
36
     * @return string
37
     */
38
    public function evaluateFieldValue($value)
39
    {
40
        if (empty($value)) {
41
            return $value;
42
        }
43
44
        preg_match($value, '');
45
46
        if (preg_last_error() === PREG_NO_ERROR) {
47
            return $value;
48
        }
49
50
        $message = GeneralUtility::makeInstance(
51
            FlashMessage::class,
52
            LocalizationService::localize('Backend/TCA:regex_error.description', [$value]),
53
            LocalizationService::localize('Backend/TCA:regex_error.title'),
54
            FlashMessage::ERROR,
55
        );
56
57
        $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
58
        $messageQueue = $flashMessageService->getMessageQueueByIdentifier();
59
        $messageQueue->addMessage($message);
60
61
        return '';
62
    }
63
}
64