Failed Conditions
Push — master ( 5f60a5...9b80eb )
by Rafael
21:42
created

PathToHierarchy::buildSolrHierarchyFromPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\FieldProcessor;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Daniel Poetzinger <[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
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy\HierarchyTool;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Processes a value that may appear as field value in documents
32
 *
33
 * @author Daniel Poetzinger <[email protected]>
34
 */
35
class PathToHierarchy implements FieldProcessor
36
{
37
38
    /**
39
     * Expects a value like "some/hierarchy/value"
40
     *
41
     * @param array $values Array of values, an array because of multivalued fields
42
     * @return array Modified array of values
43
     */
44 2
    public function process(array $values)
45
    {
46 2
        $results = [];
47
48 2
        foreach ($values as $value) {
49 2
            $valueResults = $this->buildSolrHierarchyFromPath($value);
50 2
            $results = array_merge($results, $valueResults);
51
        }
52
53 2
        return array_unique($results);
54
    }
55
56
    /**
57
     * Builds a Solr hierarchy from path string.
58
     *
59
     * @param string $path path string
60
     * @return array Solr hierarchy
61
     * @see http://wiki.apache.org/solr/HierarchicalFaceting
62
     */
63 2
    protected function buildSolrHierarchyFromPath($path)
64
    {
65 2
        $hierarchy = [];
66 2
        $path = HierarchyTool::substituteSlashes($path);
67
68 2
        $treeParts = GeneralUtility::trimExplode('/', $path, true);
69 2
        $currentTreeParts = [];
70
71 2
        foreach ($treeParts as $i => $part) {
72 2
            $currentTreeParts[] = $part;
73 2
            $hierarchyString = $i . '-' . implode('/', $currentTreeParts) . '/';
74 2
            $hierarchyString = HierarchyTool::unSubstituteSlashes($hierarchyString);
75 2
            $hierarchy[] = $hierarchyString;
76
        }
77
78 2
        return $hierarchy;
79
    }
80
}
81