| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * GpsLab component. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author    Peter Gribanov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright Copyright (c) 2011, Peter Gribanov | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license   http://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace GpsLab\Bundle\PaginationBundle\ParamConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use GpsLab\Bundle\PaginationBundle\Service\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Routing\RouterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | final class PaginationParamConverter implements ParamConverterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var RouterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $max_navigate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $parameter_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param RouterInterface $router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param int             $max_navigate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string          $parameter_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 13 |  |     public function __construct(RouterInterface $router, $max_navigate, $parameter_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 13 |  |         $this->router = $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 13 |  |         $this->max_navigate = $max_navigate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 13 |  |         $this->parameter_name = $parameter_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 13 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param ParamConverter $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |     public function supports(ParamConverter $configuration) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 2 |  |         return 'GpsLab\Bundle\PaginationBundle\Service\Configuration' === $configuration->getClass(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param Request        $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param ParamConverter $converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 11 |  |     public function apply(Request $request, ParamConverter $converter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 11 |  |         $options = $converter->getOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 11 |  |         $max_navigate = $this->max_navigate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 11 |  |         $param_name = $this->parameter_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 11 |  |         $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $reference_types = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 11 |  |             'absolute_url' => UrlGeneratorInterface::ABSOLUTE_PATH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 11 |  |             'absolute_path' => UrlGeneratorInterface::ABSOLUTE_PATH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 11 |  |             'relative_path' => UrlGeneratorInterface::RELATIVE_PATH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 11 |  |             'network_path' => UrlGeneratorInterface::NETWORK_PATH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 11 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 11 |  |         if (isset($options['max_navigate'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             $max_navigate = $options['max_navigate']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 11 |  |         if (isset($options['parameter_name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             $param_name = $options['parameter_name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 11 |  |         if (isset($options['reference_type']) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |             array_key_exists($options['reference_type'], $reference_types) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 11 |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |             $reference_type = $reference_types[$options['reference_type']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 11 |  |         $current_page = (int) $request->get($param_name, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 11 |  |         $current_page = $current_page > 1 ? $current_page : 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // get routing params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 11 |  |         $route = $request->attributes->get('_route'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 11 |  |         $route_params = array_merge($request->query->all(), $request->attributes->get('_route_params', [])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 11 |  |         unset($route_params[$param_name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // impossible resolve total pages here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 11 |  |         $total_pages = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 11 |  |         $configuration = new Configuration($total_pages, $current_page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 11 |  |         $configuration->setMaxNavigate($max_navigate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 11 |  |         $configuration->setFirstPageLink($this->router->generate($route, $route_params, $reference_type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 11 |  |         $configuration->setPageLink(function ($number) use ($route, $route_params, $param_name, $reference_type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 11 |  |             return $this->router->generate($route, [$param_name => $number] + $route_params, $reference_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 11 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 11 |  |         $request->attributes->set($converter->getName(), $configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 11 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  |