* @author Timo Hund */ class OptionCollection extends AbstractFacetItemCollection { /** * Returns an array of prefixes from the option labels. * * Red, Blue, Green => r, g, b * * Can be used in combination with getByPrefix() to group facet options by prefix (e.g. alphabetical). * * @param int $length * @return array */ public function getLowercaseLabelPrefixes($length = 1) { $prefixes = $this->getLabelPrefixes($length); return array_map('mb_strtolower', $prefixes); } /** * @param string $filteredPrefix * @return AbstractFacetItemCollection */ public function getByLowercaseLabelPrefix($filteredPrefix) { return $this->getFilteredCopy(function(Option $option) use ($filteredPrefix) { $filteredPrefixLength = mb_strlen($filteredPrefix); $currentPrefix = mb_substr(mb_strtolower($option->getLabel()), 0, $filteredPrefixLength); return $currentPrefix === $filteredPrefix; }); } /** * @param int $length * @return array */ protected function getLabelPrefixes($length = 1) : array { $prefixes = []; foreach ($this->data as $option) { /** @var $option Option */ $prefix = mb_substr($option->getLabel(), 0, $length); $prefixes[$prefix] = $prefix; } return array_values($prefixes); } }