Skip to content

Instantly share code, notes, and snippets.

@antyblin
Last active August 29, 2015 14:06
Show Gist options
  • Save antyblin/94324252a0733c6d5399 to your computer and use it in GitHub Desktop.
Save antyblin/94324252a0733c6d5399 to your computer and use it in GitHub Desktop.
<?php
/**
* BaseControl Action
* @author Pavel Kotlyarov
*/
abstract class BaseControl {
private $response = array(
'errors' => array(),
'result' => null
);
/**
* Entry Point
*/
final public function Execute() {
$this->executeControl();
$this->flushResponse();
}
/**
* Main method to implement
*
* @return mixed
*/
abstract protected function executeControl();
final protected function flushResponse() {
// Flush Response
header( 'Content-Type: application/json; charset=utf-8' );
echo ObjectHelper::ToJSON( $this->response );
}
final protected function setError( $code, $message = null, $data = null ) {
$error = array(
'code' => $code,
'message' => $message,
'data' => $data
);
array_push( $this->response['errors'], $error );
$this->response['result'] = false;
}
final protected function setData( $data, $key = null ) {
if ( $key ) {
$this->response[$key] = $data;
} else if ( is_array( $data ) ) {
$this->response = array_merge( $this->response, $data );
} else {
array_push( $this->response, $data );
}
}
final protected function setResult( $result ) {
$this->response['result'] = $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment