Selaa lähdekoodia

lu dbo auto (de)serialization

develop
Robin Thoni 8 vuotta sitten
vanhempi
commit
00eb2f9215

+ 105
- 0
src/Utils/Business/LuPropertyDocParser.php Näytä tiedosto

@@ -0,0 +1,105 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 7:42 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Business;
10
+
11
+use Luticate\Utils\Dbo\LuParameterConstraintDbo;
12
+use Luticate\Utils\Dbo\LuParameterDbo;
13
+
14
+class LuPropertyDocParser
15
+{
16
+    /**
17
+     * LuDocParser constructor.
18
+     * @param $doc string
19
+     */
20
+    public function __construct($doc)
21
+    {
22
+        $this->_doc = $doc;
23
+    }
24
+
25
+    /**
26
+     * @return LuParameterDbo
27
+     */
28
+    public function parse()
29
+    {
30
+        if (!is_string($this->_doc)) {
31
+            return null;
32
+        }
33
+        $lines = preg_split("/(\r?\n)/", $this->_doc);
34
+        $count = count($lines);
35
+        if ($count > 2) {
36
+            array_splice($lines, 0, 1);
37
+            array_splice($lines, $count - 2, 1);
38
+        }
39
+
40
+        $currentParam = new LuParameterDbo();
41
+        
42
+        foreach ($lines as $line) {
43
+            $lineMatches = [];
44
+            if (preg_match("/ *\\** *(.*) */", $line, $lineMatches) === 1) {
45
+                $line = $lineMatches[1];
46
+                $commandMatches = [];
47
+                if (preg_match("/@([^ ]+) *(.*)/", $line, $commandMatches)) {
48
+                    $command = strtolower($commandMatches[1]);
49
+                    $line = $commandMatches[2];
50
+                    if ($command == "var") {
51
+                        $paramMatches = [];
52
+                        if (preg_match("/([^ ]+) *([^ ]+) *(.*)/", $line, $paramMatches) === 1) {
53
+                            if ($paramMatches[1][0] == "$") {
54
+                                $currentParam->setName(substr($paramMatches[1], 1, strlen($paramMatches[1]) - 1));
55
+                                $currentParam->setType($paramMatches[2]);
56
+                            }
57
+                            else {
58
+                                $currentParam->setName(substr($paramMatches[2], 1, strlen($paramMatches[2]) - 1));
59
+                                $currentParam->setType($paramMatches[1]);
60
+                            }
61
+                            $currentParam->setSummary($paramMatches[3] . "\n");
62
+                        }
63
+                    }
64
+                    else {
65
+                        $methodName = $command;
66
+                        $constraint = new LuParameterConstraintDbo();
67
+                        $constraint->setMethod($methodName);
68
+                        $args = [];
69
+                        $argMatches = [];
70
+                        if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
71
+                            $args = $argMatches[1];
72
+                            foreach ($args as $key => $arg) {
73
+                                $argLower = strtolower($arg);
74
+                                if ($arg[0] == '"' || $arg[0] == "'") {
75
+                                    $args[$key] = substr($arg, 1, count($arg) - 2);
76
+                                }
77
+                                else if ($argLower == "true") {
78
+                                    $args[$key] = true;
79
+                                }
80
+                                else if ($argLower == "false") {
81
+                                    $args[$key] = false;
82
+                                }
83
+                                else if ($argLower == "null") {
84
+                                    $args[$key] = null;
85
+                                }
86
+                                else if (strpos($arg, ".") !== false) {
87
+                                    $args[$key] = floatval($arg);
88
+                                }
89
+                                else {
90
+                                    $args[$key] = intval($arg);
91
+                                }
92
+                            }
93
+                        }
94
+                        $constraint->setArguments($args);
95
+                        $currentParam->addConstraint($constraint);
96
+                    }
97
+                }
98
+                else {
99
+                    $currentParam->setSummary($currentParam->getSummary() . $line . "\n");
100
+                }
101
+            }
102
+        }
103
+        return $currentParam;
104
+    }
105
+}

+ 105
- 1
src/Utils/Dbo/LuDbo.php Näytä tiedosto

@@ -2,6 +2,9 @@
2 2
 
3 3
 namespace Luticate\Utils\Dbo;
4 4
 
5
+use Luticate\Utils\Business\LuPropertyDocParser;
6
+use Luticate\Utils\Business\LuStringUtils;
7
+
5 8
 abstract class LuDbo implements \JsonSerializable {
6 9
     /**
7 10
      * @return string
@@ -10,6 +13,40 @@ abstract class LuDbo implements \JsonSerializable {
10 13
     {
11 14
         return json_encode($this);
12 15
     }
16
+    
17
+    public function jsonSerialize()
18
+    {
19
+        $json = [];
20
+        $reflect = new \ReflectionClass(static::class);
21
+        $properties = $reflect->getProperties();
22
+        foreach ($properties as $property) {
23
+            $name = LuStringUtils::stringSnakeToCamelCase($property->getName(), true);
24
+            $property->setAccessible(true);
25
+            $value = $property->getValue($this);
26
+            if (is_array($value)) {
27
+                $array = [];
28
+                foreach ($value as $key => $v) {
29
+                    if ($v instanceof LuDbo) {
30
+                        $array[$key] = $v->jsonSerialize();
31
+                    }
32
+                    else {
33
+                        $array[$key] = $v;
34
+                    }
35
+                }
36
+                $json[$name] = $array;
37
+            }
38
+            else {
39
+                $value = $property->getValue($this);
40
+                if ($value instanceof LuDbo) {
41
+                    $json[$name] = $value->jsonSerialize();
42
+                }
43
+                else {
44
+                    $json[$name] = $value;
45
+                }
46
+            }
47
+        }
48
+        return $json;
49
+    }
13 50
 
14 51
     /**
15 52
      * @param $newClass LuDbo
@@ -23,6 +60,58 @@ abstract class LuDbo implements \JsonSerializable {
23 60
         return $obj;
24 61
     }
25 62
 
63
+    public static function endsWith($haystack, $needle) {
64
+        return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0
65
+            && strpos($haystack, $needle, $temp) !== false);
66
+    }
67
+
68
+    public static function deserializeValue($value, $type) {
69
+        if (is_null($value)) {
70
+            return null;
71
+        }
72
+        
73
+        if (is_null($type) || $type == "") {
74
+            $type = "mixed";
75
+        }
76
+        else if ($type == "array") {
77
+            $type = "mixed[]";
78
+        }
79
+
80
+        if ($type == "string") {
81
+            $dbo = LuStringDbo::jsonDeserialize($value);
82
+            return $dbo->getString();
83
+        }
84
+        else if ($type == "int") {
85
+            $dbo = LuIntDbo::jsonDeserialize($value);
86
+            return $dbo->getInt();
87
+        }
88
+        else if ($type == "float") {
89
+            $dbo = LuFloatDbo::jsonDeserialize($value);
90
+            return $dbo->getFloat();
91
+        }
92
+        else if ($type == "bool") {
93
+            $dbo = LuBoolDbo::jsonDeserialize($value);
94
+            return $dbo->getBool();
95
+        }
96
+        else if ($type == "mixed") {
97
+            return $value;
98
+        }
99
+        else if (self::endsWith($type, "[]")) {
100
+            if (!is_array($value)) {
101
+                throw new LuDboDeserializeException("Invalid array value");
102
+            }
103
+            $type = substr($type, 0, strlen($type) - 2);
104
+            $data = [];
105
+            foreach ($value as $v) {
106
+                $data[] = self::deserializeValue($v, $type);
107
+            }
108
+            return $data;
109
+        }
110
+        else {
111
+            return call_user_func_array(array($type, "jsonDeserialize"), array($value));
112
+        }
113
+    }
114
+
26 115
     /**
27 116
      * Deserialize from a JSON object
28 117
      * @param $json mixed The JSON data to deserialize
@@ -30,7 +119,22 @@ abstract class LuDbo implements \JsonSerializable {
30 119
      */
31 120
     public static function jsonDeserialize($json)
32 121
     {
33
-        return null;
122
+        $dbo = new static();
123
+        $reflect = new \ReflectionClass(static::class);
124
+        $properties = $reflect->getProperties();
125
+        foreach ($properties as $property) {
126
+            $parser = new LuPropertyDocParser($property->getDocComment());
127
+            $name = LuStringUtils::stringSnakeToCamelCase($property->getName(), true);
128
+            $doc = $parser->parse();
129
+            $type = is_null($doc) ? null : $doc->getType();
130
+
131
+            if (isset($json[$name])) {
132
+                $value = static::deserializeValue($json[$name], $type);
133
+                $property->setAccessible(true);
134
+                $property->setValue($dbo, $value);
135
+            }
136
+        }
137
+        return $dbo;
34 138
     }
35 139
 
36 140
     /**

+ 33
- 1
src/Utils/Dbo/LuMiddlewareDbo.php Näytä tiedosto

@@ -22,7 +22,39 @@ class LuMiddlewareDbo extends LuDbo
22 22
     private $_middleware;
23 23
 
24 24
     /**
25
-     * @var $_parameters mixed[]
25
+     * @return string
26
+     */
27
+    public function getMiddleware()
28
+    {
29
+        return $this->_middleware;
30
+    }
31
+
32
+    /**
33
+     * @param string $middleware
34
+     */
35
+    public function setMiddleware($middleware)
36
+    {
37
+        $this->_middleware = $middleware;
38
+    }
39
+
40
+    /**
41
+     * @return array
42
+     */
43
+    public function getParameters()
44
+    {
45
+        return $this->_parameters;
46
+    }
47
+
48
+    /**
49
+     * @param array $parameters
50
+     */
51
+    public function setParameters($parameters)
52
+    {
53
+        $this->_parameters = $parameters;
54
+    }
55
+
56
+    /**
57
+     * @var $_parameters array
26 58
      */
27 59
     private $_parameters;
28 60
 }

+ 125
- 0
tests/LuDboDeserializeTest.php Näytä tiedosto

@@ -0,0 +1,125 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuDbo;
3
+
4
+/**
5
+ * Created by PhpStorm.
6
+ * User: robin
7
+ * Date: 6/5/16
8
+ * Time: 7:02 PM
9
+ */
10
+
11
+class TestDbo extends LuDbo {
12
+
13
+    /**
14
+     * @var $_test string
15
+     */
16
+    private $_test;
17
+
18
+    /**
19
+     * @return string
20
+     */
21
+    public function getTest()
22
+    {
23
+        return $this->_test;
24
+    }
25
+
26
+    /**
27
+     * @param string $test
28
+     */
29
+    public function setTest($test)
30
+    {
31
+        $this->_test = $test;
32
+    }
33
+
34
+}
35
+
36
+class TestDbo2 extends LuDbo {
37
+
38
+    /**
39
+     * @var $_test2 TestDbo[]
40
+     */
41
+    private $_test2;
42
+
43
+    /**
44
+     * @var $_test3 TestDbo
45
+     */
46
+    private $_test3;
47
+
48
+    /**
49
+     * @return TestDbo[]
50
+     */
51
+    public function getTest2()
52
+    {
53
+        return $this->_test2;
54
+    }
55
+
56
+    /**
57
+     * @param TestDbo[] $test2
58
+     */
59
+    public function setTest2($test2)
60
+    {
61
+        $this->_test2 = $test2;
62
+    }
63
+
64
+    /**
65
+     * @return TestDbo
66
+     */
67
+    public function getTest3()
68
+    {
69
+        return $this->_test3;
70
+    }
71
+
72
+    /**
73
+     * @param TestDbo $test3
74
+     */
75
+    public function setTest3($test3)
76
+    {
77
+        $this->_test3 = $test3;
78
+    }
79
+
80
+}
81
+
82
+class LuDboDeserializeTest extends \PHPUnit_Framework_TestCase{
83
+    
84
+    public function test()
85
+    {
86
+        $json = ["Test" => "Test."];
87
+        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo')->jsonSerialize());
88
+    }
89
+    
90
+    public function test2()
91
+    {
92
+        $json = [
93
+            "Test2" => [
94
+                ["Test" => "Test."],
95
+                ["Test" => "Test.2"]
96
+            ],
97
+            "Test3" => ["Test" => "Test.3"]
98
+        ];
99
+        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
100
+    }
101
+
102
+    public function test3()
103
+    {
104
+        $json = [
105
+            "Test2" => [
106
+                ["Test" => "Test."],
107
+                ["Test" => "Test.2"]
108
+            ],
109
+            "Test3" => null
110
+        ];
111
+        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
112
+    }
113
+
114
+    public function test4()
115
+    {
116
+        $json = [
117
+            "Test2" => [
118
+                ["Test" => "Test."],
119
+                null
120
+            ],
121
+            "Test3" => null
122
+        ];
123
+        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
124
+    }
125
+}

Loading…
Peruuta
Tallenna