1 | <?php |
||
23 | abstract class TranslatedView implements ViewInterface |
||
24 | { |
||
25 | private $view; |
||
26 | private $translator; |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param ViewInterface $view A view. |
||
32 | * @param TranslatorInterface $translator A translator interface. |
||
33 | */ |
||
34 | public function __construct(ViewInterface $view, TranslatorInterface $translator) |
||
35 | { |
||
36 | $this->view = $view; |
||
37 | $this->translator = $translator; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()) |
||
44 | { |
||
45 | $optionsWithTranslations = $this->addTranslationOptions($options); |
||
46 | |||
47 | return $this->view->render($pagerfanta, $routeGenerator, $optionsWithTranslations); |
||
48 | } |
||
49 | |||
50 | private function addTranslationOptions($options) |
||
51 | { |
||
52 | return $this->addNextTranslationOption( |
||
53 | $this->addPreviousTranslationOption($options) |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | private function addPreviousTranslationOption($options) |
||
58 | { |
||
59 | $option = $this->previousMessageOption(); |
||
60 | $messageMethod = 'previousMessage'; |
||
61 | |||
62 | return $this->addTranslationOption($options, $option, $messageMethod); |
||
63 | } |
||
64 | |||
65 | private function addNextTranslationOption($options) |
||
66 | { |
||
67 | $option = $this->nextMessageOption(); |
||
68 | $messageMethod = 'nextMessage'; |
||
69 | |||
70 | return $this->addTranslationOption($options, $option, $messageMethod); |
||
71 | } |
||
72 | |||
73 | private function addTranslationOption($options, $option, $messageMethod) |
||
74 | { |
||
75 | if (isset($options[$option])) { |
||
76 | return $options; |
||
77 | } |
||
78 | |||
79 | $message = $this->$messageMethod(); |
||
80 | |||
81 | return array_merge($options, array($option => $message)); |
||
82 | } |
||
83 | |||
84 | abstract protected function previousMessageOption(); |
||
85 | |||
86 | abstract protected function nextMessageOption(); |
||
87 | |||
88 | private function previousMessage() |
||
89 | { |
||
90 | $previousText = $this->previousText(); |
||
91 | |||
92 | return $this->buildPreviousMessage($previousText); |
||
93 | } |
||
94 | |||
95 | private function nextMessage() |
||
96 | { |
||
97 | $nextText = $this->nextText(); |
||
98 | |||
99 | return $this->buildNextMessage($nextText); |
||
100 | } |
||
101 | |||
102 | private function previousText() |
||
103 | { |
||
104 | return $this->translator->trans('previous', array(), 'pagerfanta'); |
||
105 | } |
||
106 | |||
107 | private function nextText() |
||
108 | { |
||
109 | return $this->translator->trans('next', array(), 'pagerfanta'); |
||
110 | } |
||
111 | |||
112 | abstract protected function buildPreviousMessage($text); |
||
113 | |||
114 | abstract protected function buildNextMessage($text); |
||
115 | } |
||
116 |