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