| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Everlution\NavigationBundle\Twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Everlution\Navigation\Builder\MatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Everlution\Navigation\Builder\NavigationBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Everlution\Navigation\Builder\NoCurrentItemFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Everlution\Navigation\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Everlution\Navigation\FilteredContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Everlution\Navigation\FilteredContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Everlution\Navigation\Item\ItemInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Everlution\Navigation\Registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Everlution\NavigationBundle\Bridge\NavigationAliasContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Class NavigationHelper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Ivan Barlog <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class NavigationHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var Registry */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var NavigationAliasContainer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $aliasContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var MatcherInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var NavigationBuilder[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $container = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         Registry $registry, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         NavigationAliasContainer $aliasContainer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         MatcherInterface $matcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->registry = $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->aliasContainer = $aliasContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->matcher = $matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function isCurrent(string $identifier, ItemInterface $item): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             return $this->getNavigation($identifier)->getCurrent() === $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } catch (NoCurrentItemFoundException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function isAncestor(string $identifier, ItemInterface $item): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |         return $this->getNavigation($identifier)->isAncestor($item); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     public function getNavigation(string $navigation): NavigationBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         if (false === array_key_exists($navigation, $this->container)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $container = $this->getContainer($navigation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->container[$navigation] = new NavigationBuilder($container, $this->matcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $this->container[$navigation]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private function getContainer(string $navigation): ContainerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $container = $this->registry->getContainer($this->aliasContainer->get($navigation)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($container instanceof FilteredContainerInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return new FilteredContainer($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |