-
-
Save vanchelo/1c6b4ff6e79b05edf40d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ActionsHandler { | |
/** | |
* @var DocumentParser | |
*/ | |
protected $modx; | |
protected $input; | |
protected $action; | |
function __construct(DocumentParser $modx) | |
{ | |
$this->modx = $modx; | |
$this->input = $this->getInput(); | |
$this->action = $this->prepareActionName($this->input['q']); | |
} | |
public function handle() | |
{ | |
if ( ! $this->action || ! method_exists($this, $this->action)) return null; | |
$data = $this->{$this->action}(); | |
return $this->response($data); | |
} | |
protected function getInput() | |
{ | |
$input = array_merge($_GET, $_POST); | |
return $input; | |
} | |
protected function prepareActionName($action) | |
{ | |
$action = preg_replace('/[^a-z]/', '', $action); | |
if ( ! $action) return null; | |
return $action . 'Action'; | |
} | |
protected function success($message = '', $data = []) | |
{ | |
return ['error' => false, 'success' => true, 'data' => $data, 'message' => $message]; | |
} | |
protected function failure($message = '') | |
{ | |
return ['error' => true, 'success' => false, 'message' => $message]; | |
} | |
protected function response($data = []) | |
{ | |
return json_encode($data, JSON_UNESCAPED_UNICODE); | |
} | |
protected function helloAction() | |
{ | |
return $this->success('Hello message', ['hello' => 'Hello, World!']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment