| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Http; |
| 4 |
|
| 5 |
use WP_REST_Response; |
| 6 |
use ReflectionException; |
| 7 |
use FluentForm\Framework\Foundation\App; |
| 8 |
use FluentForm\Framework\Validator\ValidationException; |
| 9 |
|
| 10 |
/** |
| 11 |
* Base controller — exposes the Application, Request, and Response |
| 12 |
* via protected properties. The `@property` annotations below are |
| 13 |
* intentional in addition to the protected declarations: they help |
| 14 |
* PHPStan disambiguate when a subclass declares a method with the |
| 15 |
* same name as one of these properties (e.g., `function app()`). |
| 16 |
* |
| 17 |
* @property \FluentForm\Framework\Foundation\Application $app |
| 18 |
* @property \FluentForm\Framework\Http\Request\Request $request |
| 19 |
* @property \FluentForm\Framework\Http\Response\Response $response |
| 20 |
*/ |
| 21 |
abstract class Controller |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Application Instance |
| 25 |
* @var \FluentForm\Framework\Foundation\Application |
| 26 |
*/ |
| 27 |
protected $app = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Request Instane |
| 31 |
* @var \FluentForm\Framework\Http\Request\Request |
| 32 |
*/ |
| 33 |
protected $request = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Response Instane |
| 37 |
* @var \FluentForm\Framework\Http\Response\Response |
| 38 |
*/ |
| 39 |
protected $response = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Validated data after validation has been passed |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
private $__validated = []; |
| 46 |
|
| 47 |
/** |
| 48 |
* Construct the controller instance |
| 49 |
*/ |
| 50 |
public function __construct($app = null) |
| 51 |
{ |
| 52 |
$this->app = $app ?: App::getInstance(); |
| 53 |
$this->request = $this->app['request']; |
| 54 |
$this->response = $this->app['response']; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Validate the request data |
| 59 |
* @param array $data |
| 60 |
* @param array $rules |
| 61 |
* @param array $messages |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function validate($data, $rules, $messages = []) |
| 65 |
{ |
| 66 |
try { |
| 67 |
// @phpstan-ignore-next-line |
| 68 |
$validator = $this->app->validator->make($data, $rules, $messages); |
| 69 |
|
| 70 |
if ($validator->validate()->fails()) { |
| 71 |
throw new ValidationException( |
| 72 |
'Unprocessable Entity!', 422, null, $validator->errors() |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
$this->__validated = $this->request->validated($validator->validated()); |
| 77 |
|
| 78 |
return $data; |
| 79 |
|
| 80 |
} catch (ValidationException $e) { |
| 81 |
|
| 82 |
if (defined('REST_REQUEST') && REST_REQUEST) { |
| 83 |
throw $e; |
| 84 |
}; |
| 85 |
|
| 86 |
$this->app->doCustomAction('handle_exception', $e); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the valid data after validation has been passed. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function validated() |
| 96 |
{ |
| 97 |
return (array) $this->__validated; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Send json response |
| 102 |
* @param array $data |
| 103 |
* @param integer $code |
| 104 |
* @return string|false The JSON encoded string, or false if it cannot be encoded. |
| 105 |
*/ |
| 106 |
public function json($data = [], $code = 200) |
| 107 |
{ |
| 108 |
return $this->response->json($data, $code); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Send json response |
| 113 |
* @param array $data |
| 114 |
* @param integer $code |
| 115 |
* @return \WP_REST_Response |
| 116 |
*/ |
| 117 |
public function response($data = [], $code = 200) |
| 118 |
{ |
| 119 |
return $this->send($data, $code); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Send json response |
| 124 |
* @param array $data |
| 125 |
* @param integer $code |
| 126 |
* @return \WP_REST_Response |
| 127 |
*/ |
| 128 |
public function send($data = [], $code = 200) |
| 129 |
{ |
| 130 |
return $this->response->send($data, $code); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Send a success json response. |
| 135 |
* |
| 136 |
* @param array $data |
| 137 |
* @return \WP_REST_Response |
| 138 |
*/ |
| 139 |
public function sendSuccess($data = []) |
| 140 |
{ |
| 141 |
return $this->response->sendSuccess($data, 200); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Send an error json response |
| 146 |
* @param array $data |
| 147 |
* @param integer $code |
| 148 |
* @return \WP_REST_Response |
| 149 |
*/ |
| 150 |
public function sendError($data = [], $code = 422) |
| 151 |
{ |
| 152 |
return $this->response->sendError($data, $code); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Dynamically Access components from container |
| 157 |
* @param string $key |
| 158 |
* @return mixed |
| 159 |
* @throws ReflectionException |
| 160 |
*/ |
| 161 |
public function __get($key) |
| 162 |
{ |
| 163 |
try { |
| 164 |
return App::getInstance($key); |
| 165 |
} catch(ReflectionException $e) { |
| 166 |
$class = get_class($this); |
| 167 |
wp_die("Undefined property {$key} in $class"); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|