/** * Makes the REST call for the given operation * * @history * 2013.09.30: * (AT) Initial release * * @version 2013.09.30 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param string $operation One of CREATE, READ, UPDATE or DELETE * @return mixed REST response * @throws \Cougar\Exceptions\Exception * @throws \Cougar\Exceptions\NotImplementedException */ protected function makeRequest($operation) { # See which operation we have switch ($operation) { case "CREATE": $call = $this->__createCall; break; case "READ": $call = $this->__readCall; break; case "UPDATE": $call = $this->__updateCall; break; case "DELETE": $call = $this->__deleteCall; break; case "QUERY": $call = $this->__queryCall; break; default: throw new Exception("Invalid REST operation: " . $operation); break; } # Create the URI and add property values $uri = $this->__baseUri . $call["uri"]; $uri_param_list = array(); preg_match_all("/:[A-Za-z0-9_]+\\w/u", $uri, $uri_param_list); foreach ($uri_param_list[0] as $property_param) { $property = substr($property_param, 1); if (in_array($property, $this->__properties)) { $uri = str_replace($property_param, $this->{$property}, $uri); } } # See if we have query parameters if ($operation == "QUERY" && count($call["parameters"])) { # See if the URI has a ? already if (strpos($uri, "?") === false) { $uri .= "?" . QueryParameter::toHtml($call["parameters"]); } else { $uri .= "&" . QueryParameter::toHtml($call["parameters"]); } } # Gather the GET parameters $get_fields = array(); foreach ($call["get"] as $property) { $get_fields[$property] = $this->{$property}; } # See which kind of body we are sending switch ($call["bodyType"]) { case "json": $content_type = "application/json"; $body = json_encode($this); break; case "php": $content_type = "application/vnd.php.serialized"; $body = serialize((object) $this); break; case "xml": $content_type = "application/xml"; throw new NotImplementedException("XML REST calls not yet supported"); break; default: # Build an array of arguments $content_type = null; $body = null; if ($call["post"]) { $body = array(); foreach ($call["post"] as $property) { $body[$property] = $this->{$property}; } } break; } # Make the call and return the result return $this->__restClient->makeRequest($call["method"], $uri, null, $get_fields, $body, $content_type); }
/** * @covers \Cougar\Util\QueryParameter::fromUri */ public function testToHtmlSingleMultipleOperators() { $query = "this=that&blue!=green|red>=circle"; $parameters = QueryParameter::fromUri("? " . $query); $this->assertEquals($query, QueryParameter::toHtml($parameters)); }