Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] A simple, server-side based implementation of a protocol to be used in Ajax requests.
<?php
$p = new Protocol();
$p->set_response( 'complete', 10 );
$p->get_response();
<?php
class Protocol {
/**
* Maintains the response that will be returned to the browser.
* @var array
*/
private $response;
/**
* A list of valid messages that the protocol can send to the caller.
* @var array
*/
private $valid_messages;
/**
* Maintains a boolean value of if the response is or isn't valid.
* @var bool
*/
private $valid_responses;
/**
* Instantiates the list of valid messages and the actual response (before
* it has been formatted as JSON) for this protocol.
*
* This will be used throughout the class to use a message and file number
* to return to the browser.
*/
public function __construct() {
$this->valid_messages = array(
'INCOMPLETE',
'COMPLETE',
'NOT_FOUND',
);
$this->response = array(
'MESSAGE' => '',
'FILE_NUMBER' => -1,
);
}
/**
* Allows the user to define the message the protocol will use in its
* response. If the message is not one of the predefined options, then
* a default value, 'INVALID_MESSAGE_TYPE' will be used.
*
* @param string $message The message to place in the response.
*/
public function set_message( $message ) {
if ( ! $this->is_valid_message( $message ) ) {
$this->response['MESSAGE'] = 'INVALID_MESSAGE_TYPE';
return;
}
$message = strtoupper( $message );
$this->response['MESSAGE'] = $message;
}
/**
* Allows the user to explicitly set the file number they are working
* with when preparing the response for the caller. If the number is
* invalid, then a default value of "INVALID_FILE_NUMBER" will be used
* as the response.
*
* @param int $number The file number being processed in this response.
*/
public function set_file_number( $number ) {
if ( ! $this->is_valid_number( $number ) ) {
$this->response['FILE_NUMBER'] = 'INVALID_FILE_NUMBER';
return;
}
$this->response['FILE_NUMBER'] = intval( $number );
}
/**
* Allows the user to explicitly set the message and the file number with
* which they are working to build a response for the protocol.
*
* @param string optional $message The message to place in the response.
* @param int optiona $number The file number being processed in this response.
*/
public function set_response( $message = '', $number = -1 ) {
$this->set_message( $message );
$this->set_file_number( $number );
}
/**
* Retrieves the response from the protocol based on the defined message
* and file number and returns the response in the form of JSON.
*
* @return string A JSON response of this protocol's response.
*/
public function get_response() {
$this->validate_response();
return json_encode( $this->response );
}
/**
* Determines if the specified message is valid. The message is considered
* valid if it's part of the predefined set of messages and it's not
* an empty string.
*
* @access private
* @param string $message The message to evaluate.
* @return bool Whether or not the specified message is valid.
*/
private function is_valid_message( $message ) {
$is_valid =
in_array( strtoupper( $message ), $this->valid_messages, true ) &&
! empty( $message );
return $is_valid;
}
/**
* Determines if the specified file number is valid. The number is
* considered valid if greater than or equal to zero.
*
* @access private
* @param int $int The message to evaluate.
* @return bool Whether or not the specified number is >= 0.
*/
private function is_valid_number( $number ) {
return ( $number >= 0 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.