| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Setono\SyliusLagersystemPlugin\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Pagerfanta\Pagerfanta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Setono\SyliusLagersystemPlugin\View\PageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Routing\RouterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class PageViewFactory implements PageViewFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var RouterInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $pageViewClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $pageLinksViewClass; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         RouterInterface $router, | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         string $pageViewClass, | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         string $pageLinksViewClass | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $this->router = $router; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->pageViewClass = $pageViewClass; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->pageLinksViewClass = $pageLinksViewClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function create(Pagerfanta $pagerfanta, string $route, array $parameters): PageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         /** @var PageView $page */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $page = new $this->pageViewClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $page->page = $pagerfanta->getCurrentPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $page->limit = $pagerfanta->getMaxPerPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $page->pages = $pagerfanta->getNbPages(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $page->total = $pagerfanta->getNbResults(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $page->links = new $this->pageLinksViewClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $page->links->self = $this->router->generate($route, array_merge($parameters, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'page' => $pagerfanta->getCurrentPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'limit' => $pagerfanta->getMaxPerPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $page->links->first = $this->router->generate($route, array_merge($parameters, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'page' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'limit' => $pagerfanta->getMaxPerPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $page->links->last = $this->router->generate($route, array_merge($parameters, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'page' => $pagerfanta->getNbPages(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'limit' => $pagerfanta->getMaxPerPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $page->links->next = $this->router->generate($route, array_merge($parameters, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'page' => ($pagerfanta->getCurrentPage() < $pagerfanta->getNbPages()) ? $pagerfanta->getCurrentPage() + 1 : $pagerfanta->getCurrentPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'limit' => $pagerfanta->getMaxPerPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |