1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Application; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Interface PresenterInterface |
6
|
|
|
* |
7
|
|
|
* @package Application |
8
|
|
|
* @subpackage PresenterInterface |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
* @version v.1.0 (2020/06/23) |
11
|
|
|
* @copyright Copyright (c) 2020, aeon.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
// TODO move all methods of the Presenter method after the Controller class will be removed |
15
|
|
|
// TODO create methods 'setMessage' 'getMessage' because we have not only error messages but success notifications also |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for all presenters |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractPresenter implements PresenterInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Presenter's name |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $presenterName = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* View object |
32
|
|
|
* |
33
|
|
|
* @var ViewInterface |
34
|
|
|
*/ |
35
|
|
|
private $view = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Error code |
39
|
|
|
* |
40
|
|
|
* @var integer |
41
|
|
|
*/ |
42
|
|
|
private $errorCode = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Error message |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $errorMessage = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor |
53
|
|
|
* |
54
|
|
|
* @param ?ViewInterface $view |
55
|
|
|
* view object |
56
|
|
|
*/ |
57
|
|
|
public function __construct(?ViewInterface $view = null) |
58
|
|
|
{ |
59
|
|
|
$this->view = $view; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Method returns presenter's name |
64
|
|
|
* |
65
|
|
|
* @return string presenter's name |
66
|
|
|
*/ |
67
|
|
|
public function getPresenterName(): string |
68
|
|
|
{ |
69
|
|
|
return $this->presenterName; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Method returns presenter's name |
74
|
|
|
* |
75
|
|
|
* @return string presenter's name |
76
|
|
|
*/ |
77
|
|
|
public function setPresenterName(string $presenterName): void |
78
|
|
|
{ |
79
|
|
|
$this->presenterName = $presenterName; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Method returns code of the last error |
84
|
|
|
* |
85
|
|
|
* @return int code of the last error |
86
|
|
|
*/ |
87
|
|
|
public function getErrorCode(): int |
88
|
|
|
{ |
89
|
|
|
return $this->view === null ? $this->errorCode : $this->view->getErrorCode(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Method sets code of the last error |
94
|
|
|
* |
95
|
|
|
* @param int $code |
96
|
|
|
* code of the last error |
97
|
|
|
*/ |
98
|
|
|
public function setErrorCode(int $errorCode): void |
99
|
|
|
{ |
100
|
|
|
if ($this->view === null) { |
101
|
|
|
$this->errorCode = $errorCode; |
102
|
|
|
} else { |
103
|
|
|
$this->view->setErrorCode($errorCode); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Method return last error description |
109
|
|
|
* |
110
|
|
|
* @return string last error description |
111
|
|
|
*/ |
112
|
|
|
public function getErrorMessage(): string |
113
|
|
|
{ |
114
|
|
|
return $this->view === null ? $this->errorMessage : $this->view->getErrorMessage(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Method sets last error description |
119
|
|
|
* |
120
|
|
|
* @param |
121
|
|
|
* string last error description |
122
|
|
|
*/ |
123
|
|
|
public function setErrorMessage(string $errorMessage): void |
124
|
|
|
{ |
125
|
|
|
if ($this->view === null) { |
126
|
|
|
$this->errorMessage = $errorMessage; |
127
|
|
|
} else { |
128
|
|
|
$this->view->setErrorMessage($errorMessage); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Method sets view's var |
134
|
|
|
* |
135
|
|
|
* @param string $name |
136
|
|
|
* var name |
137
|
|
|
* @param mixed $value |
138
|
|
|
* var value |
139
|
|
|
* @param bool $setTemplateVar |
140
|
|
|
* do we need to set template parameter |
141
|
|
|
*/ |
142
|
|
|
public function setViewParameter(string $name, $value, bool $setTemplateVar): void |
143
|
|
|
{ |
144
|
|
|
if ($this->view !== null) { |
145
|
|
|
$this->view->setViewParameter($name, $value, $setTemplateVar); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Method gets view's var |
151
|
|
|
* |
152
|
|
|
* @param string $name |
153
|
|
|
* var name |
154
|
|
|
* @return mixed view's variable |
155
|
|
|
*/ |
156
|
|
|
public function getViewParameter(string $name) |
157
|
|
|
{ |
158
|
|
|
if ($this->view !== null) { |
159
|
|
|
return $this->view->getViewParameter($name); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|