Passed
Pull Request — release-11.2.x (#3154)
by Markus
16:09
created

ClassificationService::getMatchingClassNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php declare(strict_types = 1);
2
namespace ApacheSolrForTypo3\Solr\Domain\Index\Classification;
3
4
    /***************************************************************
5
     *  Copyright notice
6
     *
7
     *  (c) 2010-2017 dkd Internet Service GmbH <[email protected]>
8
     *  All rights reserved
9
     *
10
     *  This script is part of the TYPO3 project. The TYPO3 project is
11
     *  free software; you can redistribute it and/or modify
12
     *  it under the terms of the GNU General Public License as published by
13
     *  the Free Software Foundation; either version 3 of the License, or
14
     *  (at your option) any later version.
15
     *
16
     *  The GNU General Public License can be found at
17
     *  http://www.gnu.org/copyleft/gpl.html.
18
     *
19
     *  This script is distributed in the hope that it will be useful,
20
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
     *  GNU General Public License for more details.
23
     *
24
     *  This copyright notice MUST APPEAR in all copies of the script!
25
     ***************************************************************/
26
27
/**
28
 * Class ClassificationService
29
 */
30
class ClassificationService {
31
32
    /**
33
     * @param string $stringToMatch
34
     * @param Classification[] $classifications
35
     * @return array
36
     */
37 5
    public function getMatchingClassNames(string $stringToMatch, $classifications) : array
38
    {
39 5
        $matchingClassification = [];
40 5
        foreach ($classifications as $classification) {
41 5
            $matchingClassification = $this->applyMatchPatterns($stringToMatch, $classification, $matchingClassification);
42 5
            $matchingClassification = $this->applyUnMatchPatterns($stringToMatch, $classification, $matchingClassification);
43
        }
44
45 5
        return array_values($matchingClassification);
46
    }
47
48
    /**
49
     * @param string $stringToMatch
50
     * @param Classification $classification
51
     * @param $matchingClassification
52
     * @return array
53
     */
54 5
    protected function applyMatchPatterns(string $stringToMatch, $classification, $matchingClassification): array
55
    {
56
        /** @var $classification Classification */
57 5
        foreach ($classification->getMatchPatterns() as $matchPattern) {
58 5
            if (preg_match_all('~' . $matchPattern . '~ims', $stringToMatch) > 0) {
59 5
                $matchingClassification[] = $classification->getMappedClass();
60
                // if we found one match, we do not need to check the other patterns
61 5
                break;
62
            }
63
        }
64 5
        return array_unique($matchingClassification);
65
    }
66
67
    /**
68
     * @param string $stringToMatch
69
     * @param Classification $classification
70
     * @param $matchingClassification
71
     * @param $messages
72
     * @return array
73
     */
74 5
    protected function applyUnMatchPatterns(string $stringToMatch, $classification, $matchingClassification): array
75
    {
76 5
        foreach ($classification->getUnMatchPatterns() as $unMatchPattern) {
77 3
            if (preg_match_all('~' . $unMatchPattern . '~ims', $stringToMatch) > 0) {
78
                // if we found one match, we do not need to check the other patterns
79 2
                $position = array_search($classification->getMappedClass(), $matchingClassification);
80 2
                if ($position !== false) {
81 1
                    unset($matchingClassification[$position]);
82
                }
83
            }
84
        }
85
86 5
        return $matchingClassification;
87
    }
88
}
89