|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased; |
|
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
|
|
|
|
|
17
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItemCollection; |
|
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\Option; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Collection for facet options. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Frans Saris <[email protected]> |
|
24
|
|
|
* @author Timo Hund <[email protected]> |
|
25
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionsFacet |
|
26
|
|
|
*/ |
|
27
|
|
|
class OptionCollection extends AbstractFacetItemCollection |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns an array of prefixes from the option labels. |
|
32
|
|
|
* |
|
33
|
|
|
* Red, Blue, Green => r, g, b |
|
34
|
|
|
* |
|
35
|
|
|
* Can be used in combination with getByPrefix() to group facet options by prefix (e.g. alphabetical). |
|
36
|
|
|
* |
|
37
|
|
|
* @param int $length |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getLowercaseLabelPrefixes($length = 1) |
|
41
|
|
|
{ |
|
42
|
|
|
$prefixes = $this->getLabelPrefixes($length); |
|
43
|
|
|
return array_map('strtolower', $prefixes); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $filteredPrefix |
|
48
|
|
|
* @return AbstractFacetItemCollection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getByLowercaseLabelPrefix($filteredPrefix) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->getFilteredCopy(function(Option $option) use ($filteredPrefix) |
|
53
|
|
|
{ |
|
54
|
|
|
$filteredPrefixLength = strlen($filteredPrefix); |
|
55
|
|
|
$currentPrefix = substr(strtolower($option->getLabel()),0, $filteredPrefixLength); |
|
56
|
|
|
|
|
57
|
|
|
return $currentPrefix === $filteredPrefix; |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $length |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getLabelPrefixes($length = 1) : array |
|
66
|
|
|
{ |
|
67
|
|
|
$prefixes = []; |
|
68
|
|
|
foreach ($this->data as $option) { |
|
69
|
|
|
/** @var $option Option */ |
|
70
|
|
|
$prefix = substr($option->getLabel(), 0, $length); |
|
71
|
|
|
$prefixes[$prefix] = $prefix; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return array_values($prefixes); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|