-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |