Passed
Push — master ( 214069...30aafa )
by Timo
40:36
created

ClassificationService::applyMatchPatterns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12
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
 * @package ApacheSolrForTypo3\Solr\Domain\Index\Classification
30
 */
31
class ClassificationService {
32
33
    /**
34
     * @param string $stringToMatch
35
     * @param Classification[] $classifications
36
     * @return array
37
     */
38 2
    public function getMatchingClassNames(string $stringToMatch, $classifications) : array
39
    {
40 2
        $matchingClassification = [];
41 2
        foreach ($classifications as $classification) {
42
            $matchingClassification = $this->applyMatchPatterns($stringToMatch, $classification, $matchingClassification);
43 2
            $matchingClassification = $this->applyUnMatchPatterns($stringToMatch, $classification, $matchingClassification);
44
        }
45 2
46 2
        return array_values($matchingClassification);
47
    }
48 2
49
    /**
50
     * @param string $stringToMatch
51
     * @param Classification $classification
52
     * @param $matchingClassification
53 2
     * @return array
54
     */
55
    protected function applyMatchPatterns(string $stringToMatch, $classification, $matchingClassification): array
56
    {
57
        /** @var $classification Classification */
58
        foreach ($classification->getMatchPatterns() as $matchPattern) {
59
            if (preg_match_all('~' . $matchPattern . '~ims', $stringToMatch) > 0) {
60
                $matchingClassification[] = $classification->getMappedClass();
61
                // if we found one match, we do not need to check the other patterns
62
                break;
63
            }
64
        }
65
        return array_unique($matchingClassification);
66
    }
67
68
    /**
69
     * @param string $stringToMatch
70
     * @param Classification $classification
71
     * @param $matchingClassification
72
     * @param $messages
73
     * @return array
74
     */
75
    protected function applyUnMatchPatterns(string $stringToMatch, $classification, $matchingClassification): array
76
    {
77
        foreach ($classification->getUnMatchPatterns() as $unMatchPattern) {
78
            if (preg_match_all('~' . $unMatchPattern . '~ims', $stringToMatch) > 0) {
79
                // if we found one match, we do not need to check the other patterns
80
                $position = array_search($classification->getMappedClass(), $matchingClassification);
81
                if ($position !== false) {
82
                    unset($matchingClassification[$position]);
83
                }
84
            }
85
        }
86
87
        return $matchingClassification;
88
    }
89
}