Skip to content

Commit

Permalink
implementing serialzer class
Browse files Browse the repository at this point in the history
  • Loading branch information
dittertp committed Sep 29, 2014
1 parent 0f41272 commit 46eb878
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/Puzzle/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
namespace Puzzle;

use Puzzle\Interfaces\ClientInterface;
use Puzzle\Interfaces\SerializerInterface;
use Puzzle\Exceptions\ConfigurationException;
use Puzzle\Exceptions\ClientErrorResponseException;
use Puzzle\Exceptions\ClientException;
use Puzzle\Exceptions\ServerErrorResponseException;
use Puzzle\Exceptions\ServerErrorException;
use Puzzle\Exceptions\InvalidRequestMethodException;
use Puzzle\Exceptions\InvalidRequestException;
use Puzzle\Serializer\DefaultSerializer;

/**
* class Client
Expand All @@ -53,6 +55,11 @@ class Client
*/
const SCHEME_PLAIN = "http://";

/**
* @var SerializerInterface
*/
protected $serializer;

/**
* @var resource
*/
Expand Down Expand Up @@ -118,12 +125,35 @@ protected function init()
$this->port = null;
$this->host = null;

$this->setSerializer(new DefaultSerializer());
$this->scheme = self::SCHEME_PLAIN;
$this->httpOptions = array();
$this->httpOptions[CURLOPT_RETURNTRANSFER] = true;
$this->httpOptions[CURLOPT_FOLLOWLOCATION] = false;
}

/**
* Sets a serializer instance
*
* @param SerializerInterface $serializer the serializer instance
*
* @return void
*/
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* Returns serializer instance
*
* @return SerializerInterface
*/
public function getSerializer()
{
return $this->serializer;
}

/**
* Returns curl resource
*
Expand Down Expand Up @@ -205,7 +235,7 @@ public function performRequest($method, $uri, $params = null, $body = null)
try {

// serialize(json) body if it's not already a string
$body = $this->serialize($body);
$body = $this->getSerializer()->serialize($body);

return $this->processRequest(
$method,
Expand Down Expand Up @@ -369,9 +399,9 @@ protected function execute($method, $uri, $params, $body)
throw new ClientException("Error setting cURL Header options.");
}
}


$response["data"] = curl_exec($this->getHandle());
$result = curl_exec($this->getHandle());
$response["data"] = $this->getSerializer()->deserialize($result);
$response["status"] = $this->getStatusCode();

return $response;
Expand Down
56 changes: 56 additions & 0 deletions src/Puzzle/Interfaces/SerializerInterfac.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Puzzle\Interfaces\SerializerInterface
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* PHP version 5
*
* @category Puzzle
* @package TechDivision_Puzzle
* @author Philipp Dittert <[email protected]>
* @copyright 2014 TechDivision GmbH <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.appserver.io
*/

namespace Puzzle\Interfaces;

/**
* class SerializerInterface
*
* @category Puzzle
* @package TechDivision_Puzzle
* @author Philipp Dittert <[email protected]>
* @copyright 2014 TechDivision GmbH <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.appserver.io
*/

Interface SerializerInterface
{
/**
* Serialize assoc array into JSON string
*
* @param string|array $data Assoc array to encode into JSON
*
* @return string
*/
public function serialize($data);

/**
* Deserialize JSON into an assoc array
*
* @param string $data JSON encoded string
* @param array $headers Response Headers
*
* @return array
*/
public function deserialize($data);

}
74 changes: 74 additions & 0 deletions src/Puzzle/Serializer/DefaultSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Puzzle\Serializer\DefaultSerializer
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* PHP version 5
*
* @category Puzzle
* @package TechDivision_Puzzle
* @author Philipp Dittert <[email protected]>
* @copyright 2014 TechDivision GmbH <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.appserver.io
*/

namespace Puzzle\Serializer;

use Puzzle\Interfaces\SerializerInterface;

/**
* class DefaultSerializer
*
* @category Puzzle
* @package TechDivision_Puzzle
* @author Philipp Dittert <[email protected]>
* @copyright 2014 TechDivision GmbH <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.appserver.io
*/

class DefaultSerializer implements SerializerInterface
{
/**
* Serialize assoc array into JSON string
*
* @param string|array $data Assoc array to encode into JSON
*
* @return string
*/
public function serialize($data)
{
if (is_string($data) === true) {
return $data;
} else {
$data = json_encode($data);
if ($data === '[]') {
return '{}';
} else {
return $data;
}
}


}

/**
* Deserialize JSON into an assoc array
*
* @param string $data JSON encoded string
*
* @return array
*/
public function deserialize($data)
{
return json_decode($data, true);

}
}

0 comments on commit 46eb878

Please sign in to comment.