| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Service class to check for a facet if allRequirements are met for that facet. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Timo Hund <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class RequirementsService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Checks if facet meets all requirements. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Evaluates configuration in "plugin.tx_solr.search.faceting.facets.[facetName].requirements", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param AbstractFacet $facet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return bool true if facet might be rendered | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function getAllRequirementsMet(AbstractFacet $facet) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $requirements = $facet->getRequirements(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (!is_array($requirements) || count($requirements) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         foreach ($requirements as $requirement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $requirementMet = $this->getRequirementMet($facet, $requirement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $requirementMet = $this->getNegationWhenConfigured($requirementMet, $requirement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if ($requirementMet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 // early return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Checks if a single requirement is met. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param AbstractFacet $facet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param array $requirement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     protected function getRequirementMet(AbstractFacet $facet, $requirement = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $activeFacetItemValues = $this->getActiveItemValues($facet, $requirement['facet']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $csvActiveFacetItemValues = implode(', ', $activeFacetItemValues); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $requirementValues = GeneralUtility::trimExplode(',', $requirement['values']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         foreach ($requirementValues as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $noFacetOptionSelectedRequirementMet = ($value === '__none' && empty($activeFacetItemValues)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $anyFacetOptionSelectedRequirementMet = ($value === '__any' && !empty($activeFacetItemValues)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if ($noFacetOptionSelectedRequirementMet || $anyFacetOptionSelectedRequirementMet || in_array($value, $activeFacetItemValues) || fnmatch($value, $csvActiveFacetItemValues)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 // when we find a single matching requirement we can exit and return true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Returns the active item values of a facet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $facetNameToCheckRequirementsOn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return AbstractFacetItem[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected function getActiveItemValues(AbstractFacet $facet, $facetNameToCheckRequirementsOn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $facetToCheckRequirements = $facet->getResultSet()->getFacets()->getByName($facetNameToCheckRequirementsOn)->getByPosition(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (!$facetToCheckRequirements instanceof AbstractFacet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             throw new \InvalidArgumentException('Requirement for unexisting facet configured'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (!$facetToCheckRequirements->getIsUsed()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             // unused facets do not have active values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $itemValues = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $activeFacetItems = $facetToCheckRequirements->getAllFacetItems(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         foreach ($activeFacetItems as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             /** @var AbstractFacetItem $item */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $itemValues[] = $item->getUriValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $itemValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * Negates the result when configured. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param boolean $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param array $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return boolean | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |     protected function getNegationWhenConfigured($value, $configuration) | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         if (!is_array($configuration) || empty($configuration['negate']) || (bool)$configuration['negate'] === false) { | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |             return $value; | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         return !((bool)$value); | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | } |