1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Application; |
3
|
|
|
|
4
|
|
|
use Mezon\Transport\RequestParamsInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Presenter |
8
|
|
|
* |
9
|
|
|
* @package Mezon |
10
|
|
|
* @subpackage Presenter |
11
|
|
|
* @author Dodonov A.A. |
12
|
|
|
* @version v.1.0 (2020/01/12) |
13
|
|
|
* @copyright Copyright (c) 2020, aeon.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class for all views |
18
|
|
|
* |
19
|
|
|
* @deprecated since 2020-06-26 |
20
|
|
|
*/ |
21
|
|
|
class Presenter extends \Mezon\Application\AbstractPresenter |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Router |
26
|
|
|
* |
27
|
|
|
* @var \Mezon\Transport\RequestParams |
28
|
|
|
*/ |
29
|
|
|
private $requestParams = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param string $presenterName |
35
|
|
|
* Presenter name to be executed |
36
|
|
|
* @param ?\Mezon\Transport\RequestParams $requestParams |
37
|
|
|
* request params fetcher |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $presenterName = '', ?\Mezon\Transport\RequestParams $requestParams = null) |
40
|
|
|
{ |
41
|
|
|
$this->setPresenterName($presenterName); |
42
|
|
|
|
43
|
|
|
$this->requestParams = $requestParams; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Method return $requestParams and thrown exception if it was not set |
48
|
|
|
* |
49
|
|
|
* @return RequestParamsInterface request params fetcher |
50
|
|
|
*/ |
51
|
|
|
public function getParamsFetcher(): RequestParamsInterface |
52
|
|
|
{ |
53
|
|
|
if ($this->requestParams === null) { |
54
|
|
|
throw (new \Exception('Param fetcher was not setup')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->requestParams; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Method runs controller |
62
|
|
|
* |
63
|
|
|
* @param |
64
|
|
|
* string PresenterName |
65
|
|
|
* Presenter name to be run |
66
|
|
|
* @return mixed result of the controller |
67
|
|
|
*/ |
68
|
|
|
public function run(string $presenterName = '') |
69
|
|
|
{ |
70
|
|
|
if ($presenterName === '') { |
71
|
|
|
$presenterName = $this->getPresenterName(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($presenterName === '') { |
75
|
|
|
$presenterName = 'Default'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (method_exists($this, 'presenter' . $presenterName)) { |
79
|
|
|
return call_user_func([ |
80
|
|
|
$this, |
81
|
|
|
'presenter' . $presenterName |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw (new \Exception('Presenter ' . $presenterName . ' was not found')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* May be these functions should be excluded to base class common with View |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Method redirects user to another page |
94
|
|
|
* |
95
|
|
|
* @param string $url |
96
|
|
|
* @codeCoverageIgnore |
97
|
|
|
*/ |
98
|
|
|
public function redirectTo(string $url): void |
99
|
|
|
{ |
100
|
|
|
header("Location: $url"); |
101
|
|
|
exit(0); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Method builds route data |
106
|
|
|
* |
107
|
|
|
* @param string $route |
108
|
|
|
* route |
109
|
|
|
* @param string $method |
110
|
|
|
* HTTP method |
111
|
|
|
* @param string $function |
112
|
|
|
* controller's function name |
113
|
|
|
* @return array built route data |
114
|
|
|
*/ |
115
|
|
|
public function buildRoute(string $route, string $method, string $function): array |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'route' => $route, |
119
|
|
|
'method' => $method, |
120
|
|
|
'callback' => [ |
121
|
|
|
$this, |
122
|
|
|
$function |
123
|
|
|
] |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Method returns code of the last error |
129
|
|
|
* |
130
|
|
|
* @return int code of the last error |
131
|
|
|
* @codeCoverageIgnore |
132
|
|
|
*/ |
133
|
|
|
public function getErrorCode(): int |
134
|
|
|
{ |
135
|
|
|
return $this->errorCode; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Method sets code of the last error |
140
|
|
|
* |
141
|
|
|
* @param int $code |
142
|
|
|
* code of the last error |
143
|
|
|
* @codeCoverageIgnore |
144
|
|
|
*/ |
145
|
|
|
public function setErrorCode(int $errorCode): void |
146
|
|
|
{ |
147
|
|
|
$this->errorCode = $errorCode; |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Method return last error description |
152
|
|
|
* |
153
|
|
|
* @return string last error description |
154
|
|
|
* @codeCoverageIgnore |
155
|
|
|
*/ |
156
|
|
|
public function getErrorMessage(): string |
157
|
|
|
{ |
158
|
|
|
return $this->errorMessage; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Method sets last error description |
163
|
|
|
* |
164
|
|
|
* @param |
165
|
|
|
* string last error description |
166
|
|
|
* @codeCoverageIgnore |
167
|
|
|
*/ |
168
|
|
|
public function setErrorMessage(string $errorMessage): void |
169
|
|
|
{ |
170
|
|
|
$this->errorMessage = $errorMessage; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|