diff --git a/lib/Doctrine/REST/Client/Client.php b/lib/Doctrine/REST/Client/Client.php index 87d2026..d5546b3 100644 --- a/lib/Doctrine/REST/Client/Client.php +++ b/lib/Doctrine/REST/Client/Client.php @@ -67,7 +67,7 @@ public function execute(Request $request) curl_setopt($ch, CURLOPT_URL, $request->getUrl()); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); + curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders()); $username = $request->getUsername(); $password = $request->getPassword(); @@ -76,11 +76,35 @@ public function execute(Request $request) curl_setopt ($ch, CURLOPT_USERPWD, $username . ':' . $password); } + if($request->getMethod()==self::POST || $request->getMethod()==self::PUT) { + + if($request->getRequestType()=='json') { + $requestBody = json_encode($request->getParameters()); + $requestLength = strlen($requestBody); + + $fh = fopen('php://memory', 'rw'); + fwrite($fh, $requestBody); + rewind($fh); + + $headers = array_merge($request->getHeaders(), array( + 'Expect:', + 'Content-length: '.$requestLength, + 'Content-type: application/json;charset="utf-8"' + )); + + curl_setopt($ch, CURLOPT_INFILE, $fh); + curl_setopt($ch, CURLOPT_INFILESIZE, $requestLength); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } else { + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request->getParameters())); + } + } switch ($request->getMethod()) { case self::POST: - case self::PUT: curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request->getParameters())); + break; + case self::PUT: + curl_setopt($ch, CURLOPT_PUT, 1); break; case self::DELETE: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); @@ -91,7 +115,7 @@ public function execute(Request $request) } $result = curl_exec($ch); - + xdebug_var_dump($result); if ( ! $result) { $errorNumber = curl_errno($ch); $error = curl_error($ch); diff --git a/lib/Doctrine/REST/Client/Entity.php b/lib/Doctrine/REST/Client/Entity.php index 3028b20..0a0a983 100644 --- a/lib/Doctrine/REST/Client/Entity.php +++ b/lib/Doctrine/REST/Client/Entity.php @@ -83,7 +83,7 @@ public function save($action = null) { $parameters = $this->toArray(); $exists = $this->exists(); - $method = $exists ? Client::POST : Client::PUT; + $method = $exists ? Client::PUT : Client::POST; $id = $exists ? $this->getIdentifier() : null; $path = $this->generateUrl(get_defined_vars()); return self::$_manager->execute($this, $path, $method, $parameters, $action); diff --git a/lib/Doctrine/REST/Client/EntityConfiguration.php b/lib/Doctrine/REST/Client/EntityConfiguration.php index ac489a3..201314b 100644 --- a/lib/Doctrine/REST/Client/EntityConfiguration.php +++ b/lib/Doctrine/REST/Client/EntityConfiguration.php @@ -192,6 +192,16 @@ public function getResponseTransformerImpl() return $this->_attributes['responseTransformerImpl']; } + public function setHeaders($headers) + { + $this->_attributes['headers'] = $headers; + } + + public function getHeaders() + { + return $this->_attributes['headers']; + } + public function newInstance() { if ($this->_prototype === null) { diff --git a/lib/Doctrine/REST/Client/Manager.php b/lib/Doctrine/REST/Client/Manager.php index 6450c17..261a199 100644 --- a/lib/Doctrine/REST/Client/Manager.php +++ b/lib/Doctrine/REST/Client/Manager.php @@ -91,6 +91,7 @@ public function execute($entity, $url = null, $method = Client::GET, $parameters $request->setParameters($parameters); $request->setUsername($configuration->getUsername()); $request->setPassword($configuration->getPassword()); + $request->setHeaders($configuration->getHeaders()); $request->setResponseType($configuration->getResponseType()); $request->setResponseTransformerImpl($configuration->getResponseTransformerImpl()); @@ -148,12 +149,7 @@ public function execute($entity, $url = null, $method = Client::GET, $parameters private function _hydrate($configuration, $instance, $data) { foreach ($data as $key => $value) { - if (is_array($value)) - { - $configuration->setValue($instance, $key, (string) $value); - } else { - $configuration->setValue($instance, $key, $value); - } + $configuration->setValue($instance, $key, $value); } return $instance; diff --git a/lib/Doctrine/REST/Client/Request.php b/lib/Doctrine/REST/Client/Request.php index 93d0380..0128466 100644 --- a/lib/Doctrine/REST/Client/Request.php +++ b/lib/Doctrine/REST/Client/Request.php @@ -35,8 +35,10 @@ class Request private $_url; private $_method = Client::GET; private $_parameters = array(); + private $_headers = array(); private $_username; private $_password; + private $_requestType; private $_responseType = 'xml'; private $_responseTransformerImpl; @@ -80,6 +82,16 @@ public function getResponseType() return $this->_responseType; } + public function setRequestType($requestType) + { + $this->_requestType = $requestType; + } + + public function getRequestType() + { + return ($this->_requestType?$this->_requestType:$this->getResponseType()); + } + public function setUsername($username) { $this->_username = $username; @@ -100,6 +112,16 @@ public function getPassword() return $this->_password; } + public function setHeaders($headers) + { + $this->_headers = $headers; + } + + public function getHeaders() + { + return $this->_headers; + } + public function setResponseTransformerImpl($responseTransformerImpl) { $this->_responseTransformerImpl = $responseTransformerImpl; diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php index 3c32c9b..b45271f 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php @@ -35,11 +35,41 @@ abstract class AbstractResponseTransformer { protected $_entityConfiguration; + protected $_responseType; - public function __construct(EntityConfiguration $entityConfiguration) + public function __construct($responseType=null) + { + if(is_object($responseType) && get_class($responseType) == 'EntityConfiguration') { //legacy + $this->setEntityConfiguration($responseType); + } else { + $this->_responseType = $responseType; + } + } + + public function setEntityConfiguration(EntityConfiguration $entityConfiguration) { $this->_entityConfiguration = $entityConfiguration; } + public function getEntityConfiguration() + { + return $this->_entityConfiguration; + } + + public function getResponseType() { + if($this->_responseType!=null) { + return $this->_responseType; + } + if($this->_entityConfiguration) { + return $this->_entityConfiguration->getResponseType(); + } + return null; + } + + public function setResponseType($responseType) { + $this->_responseType = $responseType; + } + + abstract public function transform($data); } \ No newline at end of file diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php index 9f4a714..443af8a 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php @@ -35,7 +35,7 @@ class StandardResponseTransformer extends AbstractResponseTransformer { public function transform($data) { - switch ($this->_entityConfiguration->getResponseType()) { + switch ($this->getResponseType()) { case 'xml': return $this->xmlToArray($data); case 'json': @@ -77,6 +77,6 @@ public function xmlToArray($object, &$array = array()) public function jsonToArray($json) { - return (array) json_decode($json); + return (array) json_decode($json, true); } }