commit 0ab6af1e03396a22a95f34f4a90e3ba065630773
parent 82bbe8eea9542ff0d1eb4bd3c62fc1e1987d5241
Author: finwo <finwo@pm.me>
Date: Thu, 9 Jun 2016 12:27:54 +0200
Some progress on dirvers
Diffstat:
6 files changed, 142 insertions(+), 11 deletions(-)
diff --git a/src/Deserialize.php b/src/Deserialize.php
@@ -1,11 +0,0 @@
-<?php
-
-namespace Finwo\Mapper;
-
-class Deserialize
-{
- public function __invoke()
- {
-
- }
-}
diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Finwo\Mapper\Driver;
+
+abstract class AbstractDriver implements DriverInterface
+{
+
+}
diff --git a/src/Driver/DriverInterface.php b/src/Driver/DriverInterface.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Finwo\Mapper\Driver;
+
+interface DriverInterface
+{
+ /**
+ * encodeSupport()
+ * Checks if the given data is compatible with encoding technique
+ *
+ * @param mixed $testData
+ *
+ * @return boolean
+ */
+ public function encodeSupport( $testData );
+
+ /**
+ * decodeSupport()
+ * Checks if the given encoded data is of our type
+ *
+ * @param string $testData
+ *
+ * @return boolean
+ */
+ public function decodeSupport( $testData );
+
+ /**
+ * encode()
+ * Returns encoded version of the raw data
+ *
+ * @return string
+ */
+ public function encode( $raw );
+
+ /**
+ * decode()
+ * Returns data decoded to assoc array based upon the encoded data
+ *
+ * @return array
+ */
+ public function decode( $encoded );
+}
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
+{
+ /**
+ * {@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/DriverHandler.php b/src/DriverHandler.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace Finwo\Mapper;
+
+class DriverHandler
+{
+ /**
+ * @param string $input
+ *
+ * @return array
+ */
+ public function deserialize( $input = '' )
+ {
+ // Let's go through drivers
+
+ }
+
+}
diff --git a/src/Mapper.php b/src/Mapper.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Finwo\Mapper;
+
+class Mapper
+{
+ /**
+ * @param $originalData
+ * @param $newType
+ */
+ public function map( $originalData, $newType )
+ {
+ // Firstly, make sure we're not dealing with strings
+ if (is_string($originalData)) {
+ $originalData = DriverHandler::deserialize($originalData);
+ }
+
+ // Make sure we now have an array or object
+ if (!(is_array($originalData)||is_object($originalData))) {
+ // Hmm, what to do now
+ }
+
+ }
+}