Passed
Push — master ( 6ee2a7...a3f50d )
by Timo
22:29
created

AbstractFacetItemCollection::getManualSortedCopy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItem;
17
use ApacheSolrForTypo3\Solr\System\Data\AbstractCollection;
18
19
/**
20
 * Collection for facet options.
21
 *
22
 * @author Frans Saris <[email protected]>
23
 * @author Timo Hund <[email protected]>
24
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionsFacet
25
 */
26
abstract class AbstractFacetItemCollection extends AbstractCollection
27
{
28
    /**
29
     * @param AbstractFacetItem $item
30
     * @return AbstractFacetItemCollection
31
     */
32 82
    public function add($item)
33
    {
34 82
        if ($item === null) {
35
            return $this;
36
        }
37
38 82
        $this->data[$item->getCollectionKey()] = $item;
39 82
        return $this;
40
    }
41
42
    /**
43
     * @param string $value
44
     * @return AbstractFacetItem
45
     */
46 1
    public function getByValue($value)
47
    {
48 1
        return isset($this->data[$value]) ? $this->data[$value] : null;
49
    }
50
51
    /**
52
     * Retrieves the count (with get prefixed to be usable in fluid).
53
     *
54
     * @return int
55
     */
56 5
    public function getCount()
57
    {
58 5
        return $this->count();
59
    }
60
61
    /**
62
     * @return AbstractFacetItemCollection
63
     */
64
    public function getSelected()
65
    {
66 4
        return $this->getFilteredCopy(function(AbstractFacetItem $item) {
67 4
            return $item->getSelected();
68 4
        });
69
    }
70
71
    /**
72
     * @param array $manualSorting
73
     * @return AbstractFacetItemCollection
74
     */
75 6
    public function getManualSortedCopy(array $manualSorting)
76
    {
77 6
        $result = clone $this;
78 6
        $copiedItems = $result->data;
79 6
        $sortedOptions = [];
80 6
        foreach ($manualSorting as $item) {
81 6
            if (isset($copiedItems[$item])) {
82 6
                $sortedOptions[$item] = $copiedItems[$item];
83 6
                unset($copiedItems[$item]);
84
            }
85
        }
86
        // in the end all items get appended that are not configured in the manual sort order
87 6
        $sortedOptions = $sortedOptions + $copiedItems;
88 6
        $result->data = $sortedOptions;
89
90 6
        return $result;
91
    }
92
93
    /**
94
     * @return AbstractFacetItemCollection
95
     */
96 2
    public function getReversedOrderCopy()
97
    {
98 2
        $result = clone $this;
99 2
        $result->data = array_reverse($result->data, true);
100
101 2
        return $result;
102
    }
103
}
104