Example #1
0
 /**
  * __data() is the generic __data proxy and is called on each access via getData()
  *
  * @param Doozr_Base_State_Interface       $state         The request state object
  * @param Doozr_Base_Presenter_Rest_Config $configuration The configuration
  *
  * @author Benjamin Carl <*****@*****.**>
  * @return void
  * @access protected
  * @throws Doozr_Base_Model_Rest_Exception
  * @throws Doozr_Exception
  */
 protected function __data(Doozr_Base_State_Interface $state, Doozr_Base_Presenter_Rest_Config $configuration)
 {
     // Store state of this model
     /* @var $state Doozr_Request_State */
     $this->setStateObject($state);
     // Extract additional arguments from route
     $model = $this;
     /* @var $model Doozr_Base_Model */
     $resource = $this->getStateObject()->get($configuration->getRootNode() . $configuration->getRoute(), function () use($model) {
         $result = [];
         foreach (func_get_args() as $argument) {
             $result[] = $model->escape($argument);
         }
         return $result;
     });
     // Build a key => value store from already extracted identifiers and the recently extracted resource array
     $routeArguments = array_combine($configuration->getIds(), $resource);
     // Get arguments passed with this request
     $requestArguments = $this->getStateObject()->getQueryParams();
     // Get the identifier of the field/argument containing the real Id (uid) of the resource
     $id = $configuration->getId();
     if ($id !== null) {
         // Check for passed Id for all follow up operations on this resource
         if (array_key_exists('{{' . $id . '}}', $routeArguments)) {
             $id = $routeArguments['{{' . $id . '}}'];
         } elseif (isset($requestArguments->{$id})) {
             $id = $requestArguments->{$id};
         }
     }
     // Combine arguments from "route" with arguments from "request"
     $arguments = $this->mergeArguments($routeArguments, $requestArguments);
     // Check for submitted DOOZR_REQUEST_BODY and extract the structure so we can access the arguments
     if (isset($arguments['DOOZR_REQUEST_BODY']) === true && is_json($arguments['DOOZR_REQUEST_BODY'])) {
         $arguments = object_to_array(json_decode($arguments['DOOZR_REQUEST_BODY']));
     }
     $data = null;
     // Dispatch to resource manager if one is defined...
     $method = $this->getMethodByVerbAndRoute($this->getStateObject()->getMethod(), $configuration->getRealRoute());
     if (is_callable(array($this, $method))) {
         $data = $this->{$method}($configuration, $arguments, getallheaders(), $id);
     } else {
         // Inform developer about a not resolvable route endpoint and show him/her the methods available as hint!
         $methodsDefined = get_class_methods(__CLASS__);
         throw new Doozr_Base_Model_Rest_Exception('Could not call "' . $method . '" operation method in model "' . __CLASS__ . '". Currently defined methods of the model [unordered] are: ' . var_export($methodsDefined, true));
     }
     // So we just need to set our new fresh (or updated) data internally. "protected $data" is returned by
     // getData() to caller :) If the caller follows our strict call api
     $this->setData($data);
 }
Example #2
0
 /**
  * Registers a new route.
  *
  * @param Doozr_Base_Presenter_Rest_Config $config The configuration
  *
  * @author Benjamin Carl <*****@*****.**>
  */
 protected function registerRoute(Doozr_Base_Presenter_Rest_Config $config)
 {
     $this->routes[$config->getRoute()] = $config;
 }