コード例 #1
0
 public function __construct($client, $resource_type, $path, $hash = null)
 {
     Helper::__construct();
     $that =& $this;
     // fix exceptional resource types
     if (array_key_exists($singular = self::get_singular($resource_type), $this->INCONSISTENT_RESOURCE_TYPES)) {
         $resource_type = $this->INCONSISTENT_RESOURCE_TYPES[$singular];
     }
     $this->client = $client;
     $this->resource_type = $resource_type;
     $this->path = $path;
     $this->resources = array();
     $this->methods->index = function ($params = null) use(&$that, &$client, $resource_type, $path) {
         if ($resource_type == "session") {
             list($resource_type, $path, $hash) = $client->do_get($path, $params);
             return new ResourceDetail($client, $resource_type, $path, $hash);
         }
         list($resource_type, $path, $hash) = $client->do_get($path, $params);
         $that->resources = Resource::process($client, $resource_type, $path, $hash);
         return $that;
     };
     $this->methods->create = function ($params = null) use(&$client, $path) {
         return $client->do_post($path, $params);
     };
     if (isset($this->RESOURCE_SPECIAL_ACTIONS[$resource_type])) {
         foreach ($this->RESOURCE_SPECIAL_ACTIONS[$resource_type] as $meth => $action) {
             $action_path = Helper::insert_in_path($path, $meth);
             $this->methods->{$meth} = function ($params = null) use(&$client, $action, $action_path) {
                 return $client->{$action}($action_path, $params);
             };
         }
     }
 }
コード例 #2
0
 public function __construct($client, $resource_type, $href, $hash)
 {
     Helper::__construct();
     $that =& $this;
     $this->client = $client;
     $this->resource_type = $resource_type;
     $this->href = $href;
     if (!is_null($hash) && isset($hash->links)) {
         $links = $hash->links;
         unset($hash->links);
     } else {
         $links = array();
     }
     if (!is_null($hash) && isset($hash->actions)) {
         $raw_actions = $hash->actions;
         unset($hash->actions);
     } else {
         $raw_actions = array();
     }
     if (is_null($hash)) {
         $hash = new \StdClass();
     }
     $hash->href = Helper::get_and_delete_href_from_links($links);
     // Add links to attributes set and create a method that returns the links
     $this->attributes = new Set();
     $this->attributes[] = "links";
     $this->links = $links;
     foreach ($raw_actions as $a) {
         $action_name = $a->rel;
         $this->methods->{$action_name} = function ($params = null) use(&$client, $hash, $action_name) {
             return $client->do_post("{$hash->href}/{$action_name}", $params);
         };
     }
     $associations = new Set();
     $this->get_associated_resources($client, $links, $associations);
     switch ($resource_type) {
         case "instance":
             $this->methods->live_tasks = function ($params = null) use(&$client, $href) {
                 if (Helper::has_id($params)) {
                     $path = "{$href}/live/tasks";
                     $path = Helper::add_id_and_params_to_path($path, $params);
                     return Resource::process($client, "live_task", $path);
                 }
             };
     }
     foreach ($hash as $k => $v) {
         if ($associations[$k]) {
             // has links...?
         } else {
             // add to attributes and create a getter method
             $this->attributes[] = $k;
             $this->{$k} = $v;
         }
     }
     $this->methods->destroy = function ($params = null) use(&$client, $href) {
         return $client->do_delete($href, $params);
     };
     $this->methods->update = function ($params = null) use(&$client, $href, $resource_type) {
         if ($resource_type == "account") {
             // HACK: handle child_account update specially
             $href = strstr("account", "child_account");
         }
         return $client->do_put($href, $params);
     };
     $this->methods->show = function ($params = null) use(&$that) {
         return $that;
     };
 }
コード例 #3
0
 /**
  * creates instance methods out of the associated resources from links
  */
 protected function get_associated_resources($client, $links, Set &$associations = null)
 {
     $rels = array();
     foreach ($links as $l) {
         if (array_key_exists($l->rel, $rels)) {
             $rels[$l->rel][] = $l->href;
         } else {
             $rels[$l->rel] = array($l->href);
         }
     }
     foreach ($rels as $rel => $hrefs) {
         if (!is_null($associations)) {
             $associations[] = $rel;
         }
         $that =& $this;
         $this->methods->{$rel} = function ($params = null) use(&$that, &$client, $rel, $hrefs) {
             if (count($hrefs) == 1) {
                 if (Helper::has_id($params) || Helper::is_singular($rel)) {
                     // user wants a single resource
                     // calling data() you don't want a resource object back
                     if ($rel == "data") {
                         return new ResourceDetail($hrefs[0], $params);
                     }
                     if (Helper::is_singular($rel)) {
                         $resource_type = Helper::get_resource_type($hrefs[0], -2);
                     } else {
                         $resource_type = Helper::get_resource_type($hrefs[0], -1);
                     }
                     $path = Helper::add_id_and_params_to_path($hrefs[0], $params);
                     Helper::get_resource_type($hrefs[0], -1);
                     return Resource::Process($client, $resource_type, $path);
                 } else {
                     $resource_type = Helper::get_resource_type($hrefs[0], -1);
                     $path = Helper::add_id_and_params_to_path($hrefs[0], $params);
                     return new Resources($client, $resource_type, $path);
                 }
             } else {
                 $resources = array();
                 if (Helper::has_id($params) || Helper::is_singular($rel)) {
                     foreach ($hrefs as $href) {
                         // user wants a single resource. Doing show, update, delete, etc
                         if (Helper::is_singular($rel)) {
                             $resource_type = Helper::get_resource_type($href, -2);
                         } else {
                             $resource_type = Helper::get_resource_type($href, -1);
                         }
                         $path = Helper::add_id_and_params_to_path($href, $params);
                         $resources[] = Resource::process($client, $resource_type, $path);
                     }
                 } else {
                     foreach ($hrefs as $href) {
                         $resource_type = Helper::get_resource_type($href, -1);
                         $path = Helper::add_id_and_params_to_path($href, $params);
                         $resources[] = new Resources($client, $resource_type, $path);
                     }
                 }
                 return $resources;
             }
         };
     }
 }