* @license PHP License * @package WB * @subpackage rest */ WBClass::load('WBRest_Component' , 'WBUser' ); /** * Rest Component: Counter * * Counter interface for any generic counter * * @version 0.2.1 * @package WB * @subpackage rest */ class WBRest_Component_Counter extends WBRest_Component { /** * Counter * @var WBDatasource_Counter */ private $cnt; /** * 2nd constructor * */ public function init() { $this->cnt = WBClass::create('WBDatasource_Counter'); } /** * Handle Default Request * * Get, set or increment counters for namspaces. Path contains: * - namespace (string) * - command, on of: "get", "set" or "inc" * - optional value to set or increment * * @param array $path */ public function defaultRequest($path) { // default operation: get if (2 > count($path)) { $patn[] = 'get'; } // first in path must be namespace $ns = trim(strtolower(array_shift($path))); $this->out['namespace'] = $ns; switch (strtolower($path[0])) { case 'set': $path[1] = intval($path[1]); if (0 > $path[1]) { $this->status = WBRest::STATUS_ERROR; $this->addError('rest:counter:2', 'Cannot set value, value required in request path'); return; } $this->cnt->set($ns, $path[1]); break; case 'inc': if (empty($path[1])) { $path[1] = 1; } $this->cnt->inc($ns, $path[1]); break; default: case 'get': break; } $cnt = $this->cnt->get($ns); $this->out['counter'] = $cnt['counter']; $this->out['changed'] = $cnt['changed']; } }