-
Notifications
You must be signed in to change notification settings - Fork 29
Lot of changes to make it more flexible. #4
base: master
Are you sure you want to change the base?
Changes from all commits
f4fdd83
7b5f29c
9b83039
07b34c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do, sorry Wysłane z iPhone'a / Sent from iPhone Pozdrawiam / Best regards Bartosz Hernas
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for removal |
||
if ( ! $result) { | ||
$errorNumber = curl_errno($ch); | ||
$error = curl_error($ch); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you sure about this change? I believe PUT is to create (specifies the resource id) or replace while POST is to create (lets the server set the resource id on return) or update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://stackoverflow.com/questions/630453/put-vs-post-in-rest POST is to create (no id specified) and PUT is to update (id specified) so I am sure. I tested it with our REST API and its correct behaviour. |
||
$id = $exists ? $this->getIdentifier() : null; | ||
$path = $this->generateUrl(get_defined_vars()); | ||
return self::$_manager->execute($this, $path, $method, $parameters, $action); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$this->setEntityConfiguration($responseType); | ||
} else { | ||
$this->_responseType = $responseType; | ||
} | ||
} | ||
|
||
public function setEntityConfiguration(EntityConfiguration $entityConfiguration) | ||
{ | ||
$this->_entityConfiguration = $entityConfiguration; | ||
} | ||
|
||
public function getEntityConfiguration() | ||
{ | ||
return $this->_entityConfiguration; | ||
} | ||
|
||
public function getResponseType() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. { belongs on the next line. |
||
if($this->_responseType!=null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should be:
|
||
return $this->_responseType; | ||
} | ||
if($this->_entityConfiguration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return $this->_entityConfiguration->getResponseType(); | ||
} | ||
return null; | ||
} | ||
|
||
public function setResponseType($responseType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. { belongs on the next line. |
||
$this->_responseType = $responseType; | ||
} | ||
|
||
|
||
abstract public function transform($data); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($request->getRequestType() === 'json') {