1 | <?php |
||
7 | abstract class Widget implements WidgetInterface |
||
8 | { |
||
9 | /** |
||
10 | * Whether or not user is authorized to view widget content. |
||
11 | * |
||
12 | * Widget child is authorized by default. |
||
13 | * |
||
14 | * @return bool |
||
15 | */ |
||
16 | 3 | public function authorize() |
|
17 | { |
||
18 | 3 | return true; |
|
19 | } |
||
20 | |||
21 | /** |
||
22 | * String or content to display when the widget is not authorized. |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | 1 | public function notAuthorizedContent() |
|
27 | { |
||
28 | 1 | return '<!-- Not authorized. -->'; |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * Method to echo out the widget result. |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 5 | public function render() |
|
37 | { |
||
38 | 5 | if (! $this->authorize()) { |
|
39 | 1 | return $this->notAuthorizedContent(); |
|
40 | } |
||
41 | |||
42 | 4 | $content = $this->content(); |
|
43 | |||
44 | 4 | return $content instanceof View ? $content->render() : $content; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Generate widget content. |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | abstract protected function content(); |
||
53 | } |
||
54 |