Skip to content

Instantly share code, notes, and snippets.

@vanchelo
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanchelo/1c6b4ff6e79b05edf40d to your computer and use it in GitHub Desktop.
Save vanchelo/1c6b4ff6e79b05edf40d to your computer and use it in GitHub Desktop.
<?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