1 | <?php |
||
7 | class PaginationExtension extends \Twig_Extension |
||
8 | { |
||
9 | /** |
||
10 | * @var \Twig_Environment |
||
11 | */ |
||
12 | protected $environment; |
||
13 | |||
14 | /** |
||
15 | * @var Processor |
||
16 | */ |
||
17 | protected $processor; |
||
18 | |||
19 | public function __construct(Processor $processor) |
||
23 | |||
24 | /** |
||
25 | * {@inheritDoc} |
||
26 | */ |
||
27 | public function initRuntime(\Twig_Environment $environment) |
||
31 | |||
32 | /** |
||
33 | * {@inheritDoc} |
||
34 | */ |
||
35 | public function getFunctions() |
||
43 | |||
44 | /** |
||
45 | * Renders the pagination template |
||
46 | * |
||
47 | * @param string $template |
||
48 | * @param array $queryParams |
||
49 | * @param array $viewParams |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function render($pagination, $template = null, array $queryParams = array(), array $viewParams = array()) |
||
60 | |||
61 | /** |
||
62 | * Create a sort url for the field named $title |
||
63 | * and identified by $key which consists of |
||
64 | * alias and field. $options holds all link |
||
65 | * parameters like "alt, class" and so on. |
||
66 | * |
||
67 | * $key example: "article.title" |
||
68 | * |
||
69 | * @param string $title |
||
70 | * @param string $key |
||
71 | * @param array $options |
||
72 | * @param array $params |
||
73 | * @param string $template |
||
74 | * @return string |
||
75 | */ |
||
76 | public function sortable($pagination, $title, $key, $options = array(), $params = array(), $template = null) |
||
83 | |||
84 | /** |
||
85 | * Create a filter url for the field named $title |
||
86 | * and identified by $key which consists of |
||
87 | * alias and field. $options holds all link |
||
88 | * parameters like "alt, class" and so on. |
||
89 | * |
||
90 | * $key example: "article.title" |
||
91 | * |
||
92 | * @param string $title |
||
93 | * @param string $key |
||
94 | * @param array $options |
||
95 | * @param array $params |
||
96 | * @param string $template |
||
97 | * @return string |
||
98 | */ |
||
99 | public function filter($pagination, array $fields, $options = array(), $params = array(), $template = null) |
||
106 | |||
107 | /** |
||
108 | * Get name |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getName() |
||
116 | } |
||
117 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.