Completed
Push — master ( 0d60f0...59f6cd )
by Simonas
166:06 queued 101:03
created

Twig/PagerExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\FilterManagerBundle\Twig;
13
14
use ONGR\FilterManagerBundle\Filter\ViewData\PagerAwareViewData;
15
use Symfony\Component\Routing\RouterInterface;
16
17
/**
18
 * PagerExtension extends Twig with pagination capabilities.
19
 */
20
class PagerExtension extends \Twig_Extension
21
{
22
    /**
23
     * Twig extension name.
24
     */
25
    const NAME = 'ongr_pager';
26
27
    /**
28
     * @var RouterInterface
29
     */
30
    protected $router;
31
32
    /**
33
     * @param RouterInterface $router
34
     */
35
    public function __construct(RouterInterface $router)
36
    {
37
        $this->router = $router;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFunctions()
44
    {
45
        return [
46
            new \Twig_SimpleFunction(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
47
                'ongr_paginate',
48
                [$this, 'paginate'],
49
                ['is_safe' => ['html'], 'needs_environment' => true]
50
            ),
51
            new \Twig_SimpleFunction('ongr_paginate_path', [$this, 'path'], ['is_safe' => []]),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
52
        ];
53
    }
54
55
    /**
56
     * Renders pagination element.
57
     *
58
     * @param \Twig_Environment  $env
59
     * @param PagerAwareViewData $pager
60
     * @param string             $route
61
     * @param array              $parameters
62
     * @param string             $template
63
     *
64
     * @return string
65
     */
66
    public function paginate(
67
        \Twig_Environment $env,
68
        $pager,
69
        $route,
70
        array $parameters = [],
71
        $template = 'ONGRFilterManagerBundle:Pager:paginate.html.twig'
72
    ) {
73
74
        return $env->render(
75
            $template,
76
            ['pager' => $pager, 'route' => $route, 'parameters' => $parameters]
77
        );
78
    }
79
80
    /**
81
     * Generates url to certain page.
82
     *
83
     * @param string $route
84
     * @param string $page
85
     * @param array  $parameters
86
     *
87
     * @return string
88
     */
89
    public function path($route, $page, array $parameters = [])
90
    {
91
        $fieldName = 'page';
92
93
        if (isset($parameters['_page'])) {
94
            $fieldName = $parameters['_page'];
95
            unset($parameters['_page']);
96
        }
97
98
        $parameters[$fieldName] = $page;
99
100
        return $this->router->generate($route, $parameters);
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getName()
107
    {
108
        return self::NAME;
109
    }
110
}
111