Passed
Pull Request — master (#1318)
by
unknown
32:46
created

AbstractFacetItemCollection::getByValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
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
    public function add($item)
33
    {
34
        if ($item === null) {
35
            return $this;
36
        }
37
38
        $this->data[$item->getCollectionKey()] = $item;
39
        return $this;
40
    }
41
42
    /**
43
     * @param string $value
44
     * @return AbstractFacetItem
45
     */
46
    public function getByValue($value)
47
    {
48
        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
    public function getCount()
57
    {
58
        return $this->count();
59
    }
60
61
    /**
62
     * @return AbstractFacetItemCollection
63
     */
64
    public function getSelected()
65
    {
66
        return $this->getFilteredCopy(function(AbstractFacetItem $item) {
67
            return $item->getSelected();
68
        });
69
    }
70
71
    /**
72
     * @param array $manualSorting
73
     * @return AbstractFacetItemCollection
74
     */
75
    public function getManualSortedCopy(array $manualSorting)
76
    {
77
        $result = clone $this;
78
        $copiedItems = $result->data;
79
        $sortedOptions = [];
80
        foreach ($manualSorting as $item) {
81
            if (isset($copiedItems[$item])) {
82
                $sortedOptions[] = $copiedItems[$item];
83
                unset($copiedItems[$item]);
84
            }
85
        }
86
        // in the end all items get appended that are not configured in the manual sort order
87
        $sortedOptions = $sortedOptions + $copiedItems;
88
        $result->data = $sortedOptions;
89
90
        return $result;
91
    }
92
93
    /**
94
     * @return AbstractFacetItemCollection
95
     */
96
    public function getReversedOrderCopy()
97
    {
98
        $result = clone $this;
99
        $result->data = array_reverse($result->data, true);
100
101
        return $result;
102
    }
103
}
104