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
|
|
|
*/ |
26
|
|
|
class OptionCollection extends AbstractFacetItemCollection |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns an array of prefixes from the option labels. |
31
|
|
|
* |
32
|
|
|
* Red, Blue, Green => r, g, b |
33
|
|
|
* |
34
|
|
|
* Can be used in combination with getByPrefix() to group facet options by prefix (e.g. alphabetical). |
35
|
|
|
* |
36
|
|
|
* @param int $length |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getLowercaseLabelPrefixes($length = 1) |
40
|
|
|
{ |
41
|
|
|
$prefixes = $this->getLabelPrefixes($length); |
42
|
|
|
return array_map('mb_strtolower', $prefixes); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $filteredPrefix |
47
|
|
|
* @return AbstractFacetItemCollection |
48
|
|
|
*/ |
49
|
|
|
public function getByLowercaseLabelPrefix($filteredPrefix) |
50
|
|
|
{ |
51
|
|
|
return $this->getFilteredCopy(function(Option $option) use ($filteredPrefix) |
52
|
|
|
{ |
53
|
|
|
$filteredPrefixLength = mb_strlen($filteredPrefix); |
54
|
|
|
$currentPrefix = mb_substr(mb_strtolower($option->getLabel()), 0, $filteredPrefixLength); |
55
|
|
|
|
56
|
|
|
return $currentPrefix === $filteredPrefix; |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $length |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
protected function getLabelPrefixes($length = 1) : array |
65
|
|
|
{ |
66
|
|
|
$prefixes = []; |
67
|
|
|
foreach ($this->data as $option) { |
68
|
|
|
/** @var $option Option */ |
69
|
|
|
$prefix = mb_substr($option->getLabel(), 0, $length); |
70
|
|
|
$prefixes[$prefix] = $prefix; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return array_values($prefixes); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|