commit 2b561f29cbfb068bb42694f5c39704ff5fa5f5c2
parent 0ab6af1e03396a22a95f34f4a90e3ba065630773
Author: finwo <finwo@pm.me>
Date: Wed, 21 Dec 2016 08:39:28 +0100
Should be usable now
Diffstat:
9 files changed, 267 insertions(+), 75 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,2 +1,2 @@
-/vendor/
-/.idea/
+vendor
+.idea
diff --git a/README.md b/README.md
@@ -2,17 +2,16 @@
[](https://travis-ci.org/finwo/php-mapper)
------
+---
-Since I was tired of data mappers not doing what I was expecting them to do, I wrote my own
+Basically a serialization wrapper for [netresearch/json-mapper](https://github.com/netresearch/json-mapper.git).
------
+---
### Contributing
After checking the [Github issues](https://github.com/finwo/php-mapper/issues) and confirming that your request isn't already being worked on, feel free to spawn a new fork of the develop branch & send in a pull request.
-
The develop branch is merged periodically into the master after confirming it's stable, to make sure the master always contains a production-ready version.
-----
diff --git a/composer.json b/composer.json
@@ -11,8 +11,9 @@
"psr-4": { "Finwo\\Mapper\\": "src" }
},
"require": {
- "php": ">=5.3",
- "finwo/property-accessor": "^0.1.3"
+ "php": ">=5.4",
+ "finwo/property-accessor": "^0.1.3",
+ "netresearch/jsonmapper": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "*"
diff --git a/composer.lock b/composer.lock
@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "215edf3bc206f334e9f99d14e9b00bd0",
- "content-hash": "f8535af8ae04e55a5f79c0436591659a",
+ "hash": "7cdd42b8bcfe67bff9a0aa419ef94bfe",
+ "content-hash": "6de96ee06f67ad0788aa8f75fd0dc20a",
"packages": [
{
"name": "finwo/property-accessor",
@@ -38,6 +38,45 @@
}
],
"time": "2016-05-06 13:52:04"
+ },
+ {
+ "name": "netresearch/jsonmapper",
+ "version": "v1.1.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/cweiske/jsonmapper.git",
+ "reference": "2130cc7152b31fb9f60a2d6e1f631981279d3957"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/2130cc7152b31fb9f60a2d6e1f631981279d3957",
+ "reference": "2130cc7152b31fb9f60a2d6e1f631981279d3957",
+ "shasum": ""
+ },
+ "require-dev": {
+ "phpunit/phpunit": "4.2.*",
+ "squizlabs/php_codesniffer": "~1.5"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "JsonMapper": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "OSL-3.0"
+ ],
+ "authors": [
+ {
+ "name": "Christian Weiske",
+ "email": "cweiske@cweiske.de",
+ "homepage": "http://github.com/cweiske/jsonmapper/",
+ "role": "Developer"
+ }
+ ],
+ "description": "Map nested JSON structures onto PHP classes",
+ "time": "2016-11-17 06:34:01"
}
],
"packages-dev": [
@@ -1150,7 +1189,7 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
- "php": ">=5.3"
+ "php": ">=5.4"
},
"platform-dev": []
}
diff --git a/src/Driver/JSONDriver.php b/src/Driver/JSONDriver.php
@@ -1,50 +0,0 @@
-<?php
-
-namespace Finwo\Mapper\Driver;
-
-class JSONDriver extends AbstractDriver
-{
- /**
- * {@inheritdoc}
- */
- public function encodeSupport($testData)
- {
- // TODO: implement proper testing
- try {
- $this->encode($testData);
- return true;
- } catch (\Exception $e) {
- return false;
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function decodeSupport($testData)
- {
- // TODO: implement proper testing
- try {
- $this->decode($testData);
- return true;
- } catch (\Exception $e) {
- return false;
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function encode($raw)
- {
- return json_encode($raw);
- }
-
- /**
- * {@inheritdoc}
- */
- public function decode($testData)
- {
- return json_decode($testData, true);
- }
-}
diff --git a/src/Driver/JsonDriver.php b/src/Driver/JsonDriver.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Finwo\Mapper\Driver;
+
+class JsonDriver extends AbstractDriver implements DriverInterface
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function encodeSupport($testData)
+ {
+ // TODO: implement proper testing
+ try {
+ $this->encode($testData);
+ return true;
+ } catch (\Exception $e) {
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function decodeSupport($testData)
+ {
+ // TODO: implement proper testing
+ try {
+ $this->decode($testData);
+ return true;
+ } catch (\Exception $e) {
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function encode($raw)
+ {
+ return json_encode($raw);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function decode($testData)
+ {
+ return json_decode($testData);
+ }
+}
diff --git a/src/Driver/UrlEncodedDriver.php b/src/Driver/UrlEncodedDriver.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Finwo\Mapper\Driver;
+
+class UrlEncoded extends AbstractDriver implements DriverInterface
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function encodeSupport($testData)
+ {
+ // TODO: implement proper testing
+ try {
+ $this->encode($testData);
+ return true;
+ } catch (\Exception $e) {
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function decodeSupport($testData)
+ {
+ // TODO: implement proper testing
+ try {
+ $this->decode($testData);
+ return true;
+ } catch (\Exception $e) {
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function encode($raw)
+ {
+ return http_build_query($raw);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function decode($testData)
+ {
+ return parse_str($testData);
+ }
+}
diff --git a/src/DriverHandler.php b/src/DriverHandler.php
@@ -2,17 +2,97 @@
namespace Finwo\Mapper;
+use Finwo\Mapper\Driver\DriverInterface;
+
class DriverHandler
{
/**
+ * @var array<DriverInterface>
+ */
+ protected $drivers = array();
+
+ /**
+ * @param string $name
+ * @param DriverInterface $driver
+ *
+ * @return $this
+ * @throws \Error
+ */
+ public function registerDriver($name = '', DriverInterface $driver)
+ {
+ if (!is_string($name)) {
+ throw new \Error("Name is not a string");
+ }
+
+ if (isset($this->drivers[$name])) {
+ throw new \Error(sprintf("Driver by the name of '%s' already registered.",$name));
+ }
+
+ $this->drivers[$name] = $driver;
+
+ return $this;
+ }
+
+ /**
* @param string $input
+ * @param string $encoding
*
- * @return array
+ * @return string
+ * @throws \Error
*/
- public function deserialize( $input = '' )
+ public function deserialize($input = '', $encoding = null)
{
- // Let's go through drivers
-
+ // We may not need to deserialize
+ if (is_array($input)|is_object($input)) {
+ return $input;
+ }
+
+ // We might need to detect which encoding to use
+ if (is_null($encoding)) {
+ foreach ($this->drivers as $name => $driver) {
+ if($driver->decodeSupport($input)) {
+ $encoding = $name;
+ break;
+ }
+ }
+ }
+
+ // Decode the data
+ if (isset($drivers[$encoding])) {
+ return $drivers[$encoding]->decode($input);
+ }
+
+ // Or notify we've failed
+ throw new \Error("No driver capable of handling data is registered");
}
-
+
+ /**
+ * @param mixed $data
+ * @param null $encoding
+ *
+ * @return string
+ * @throws \Error
+ */
+ public function serialize($data, $encoding = null)
+ {
+
+ // We might need to detect which encoding to use
+ if (is_null($encoding)) {
+ foreach ($this->drivers as $name => $driver) {
+ if($driver->encodeSupport($data)) {
+ $encoding = $name;
+ break;
+ }
+ }
+ }
+
+ // Decode the data
+ if (isset($drivers[$encoding])) {
+ return $drivers[$encoding]->encode($data);
+ }
+
+ // Or notify we've failed
+ throw new \Error("No driver capable of handling data is registered");
+ }
+
}
diff --git a/src/Mapper.php b/src/Mapper.php
@@ -2,23 +2,46 @@
namespace Finwo\Mapper;
+use Finwo\Mapper\Driver\JsonDriver;
+use Finwo\Mapper\Driver\UrlEncoded;
+
class Mapper
{
/**
- * @param $originalData
- * @param $newType
+ * @return DriverHandler
*/
- public function map( $originalData, $newType )
+ protected static function getDriverHandler()
{
- // Firstly, make sure we're not dealing with strings
- if (is_string($originalData)) {
- $originalData = DriverHandler::deserialize($originalData);
+ static $dh = null;
+ if (is_null($dh)) {
+ // Build driver handler & register included drivers
+ $dh = new DriverHandler();
+ $dh->registerDriver('json', new JsonDriver())
+ ->registerDriver('urlencoded', new UrlEncoded());
}
- // Make sure we now have an array or object
- if (!(is_array($originalData)||is_object($originalData))) {
- // Hmm, what to do now
+ return $dh;
+ }
+
+ /**
+ * @param object|string $objectOrString
+ * @param object $targetObject
+ *
+ * @return $this
+ */
+ public function map($objectOrString, $targetObject)
+ {
+ // Initialize json-mapper
+ static $mapper = null;
+ if (is_null($mapper)) {
+ $mapper = new \JsonMapper();
}
+ // Make the data usable
+ $data = $this->getDriverHandler()->deserialize($objectOrString);
+
+ // Map & bail
+ $mapper->map($data, $targetObject);
+ return $this;
}
}