* @license PHP License * @package WB * @subpackage rest */ /** * load base class */ WBClass::load( 'WBRest', 'WBFormProcessor' ); /** * Rest Component: Explorer * * Base class for Restful component * * @version 0.2.0 * @package WB * @subpackage rest */ class WBRest_Component extends WBFormProcessor { /** * current request method * @var string */ protected $method = WBRest::METHOD_GET; /** * list of available request methods * @var array */ protected $allowedMethods = array( WBRest::METHOD_GET, WBRest::METHOD_POST, WBRest::METHOD_PUT, WBRest::METHOD_DELETE ); /** * result status */ const STATUS_OK = 'ok'; /** * result status */ const STATUS_ERROR = 'error'; /** * result status */ const STATUS_FAILED = 'failed'; /** * response status * @var string */ protected $status = WBRest::STATUS_OK; /** * request data * @var array */ protected $in = array(); /** * response data * @var array */ protected $out = array(); /** * list of errors * @var array */ protected $error = array(); /** * name of rest service * @var string */ protected $serviceName; /** * constructor * */ final public function __construct() { $this->serviceName = strtolower(substr(get_class($this), strlen(__CLASS__) + 1)); } /** * set request information * * @param string $method * @param array $in */ final public function setRequest($method, $in = array()) { if (!is_array($in)) { $in = array(); } $this->in = $in; $method = strtolower($method); if (!in_array($method, $this->allowedMethods)) { $method = WBRest::METHOD_GET; } $this->method = $method; $this->init(); } /** * 2nd constructor * * gets called after request is set * @see setRequest() */ protected function init() { } /** * get response to forward * * @param int $status * @param array $data * @param array $error */ final public function getResponse(&$status, &$data, &$error) { $this->checkMethod($this->allowedMethods); if (empty($this->error)) { $this->error = null; } $status = $this->status; $data = $this->out; $error = $this->error; } /** * handle default request * * @param array $path */ public function defaultRequest($path) { $this->status = WBRest::STATUS_ERROR; $this->addError('rest:1', sprintf('The selected path "%s" is not available', implode('/', $path))); } /** * handle empty request * */ public function emptyRequest() { $this->status = WBRest::STATUS_ERROR; $this->addError('rest:2', 'Additional path expected'); } /** * Helper method to add error messages * * Exmple: * $this->addError('rest:2', 'Additional path expected'); * $this->addError('rest:3', 'Additional path expected'); * * @param string $code * @param string $message * @param array additionl information (associative array) */ protected function addError($code, $message, $additional = null) { $error = array( 'code' => $code, 'message' => $message ); if (is_array($additional)) { $error = array_merge($error, $additional); } $this->error[] = $error; } /** * check whether current request method is allowed * * convenient function to check current request method and * build matching error code and message * * @param array|string $method list of allowed methods * @return bool */ protected function checkMethod($method) { if (!is_array($method)) { $method = array($method); } if (in_array($this->method, $method)) { return true; } $this->status = WBRest::STATUS_ERROR; $add = array( 'allowed' => $method ); $this->addError('rest:3', sprintf('The selected method "%s" is not available', $this->method), $add); return false; } /** * Process Form Adapter * * Inverse parameter "always" * * @param string $name named form to prrocess * @param array $values form values to pre-set * @param string $saveParam name of POST / GET parameter that triggers "save" * @param bool $always - always process form, ignore save parameter "true" * @return mixed string HTML in case template engine will be used, or bool * @see WBFormProcessor::processForm() */ public function processForm($name, $values = array(), $saveParam = 'save', $always = true) { return parent::processForm($name, $values, $saveParam, $always); } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { $name = substr(get_class($this), strlen(__CLASS__) + 1); return sprintf('rest/%s/form', strtolower($name)); } /** * Add form validation errors * * Get all validation errors and add them to output * Also errors are counted and the sum returned * * @param patForms|array $form * @return int */ protected function addValidationErrors($form) { $this->status = WBRest::STATUS_FAILED; if (is_array($form)) { $errors = $form; } else { $errors = $form->getValidationErrors(); } $count = 0; foreach ($errors as $field => $err) { foreach ($err as $e) { ++$count; $add = array( 'field' => $field ); $this->addError('invalid:' . strtolower($e['element']) . ':' . $e['code'] , $e['message'] , $add ); } } return $count; } } ?>