Browse Source

switched json serialization to lower camel case

develop
Robin Thoni 8 years ago
parent
commit
61328ce006

+ 8
- 7
src/Utils/Business/LuArrayUtils.php View File

11
 
11
 
12
 class LuArrayUtils
12
 class LuArrayUtils
13
 {
13
 {
14
-    public static function snakeToCamelCase($array)
14
+    public static function snakeToCamelCase($array, $capitalizeFirstCharacter = false)
15
     {
15
     {
16
         if (!is_array($array))
16
         if (!is_array($array))
17
         {
17
         {
20
         $camelCase = [];
20
         $camelCase = [];
21
         foreach ($array as $key => $value)
21
         foreach ($array as $key => $value)
22
         {
22
         {
23
-            $camelCase[LuStringUtils::snakeToCamelCase($key, true)] = self::snakeToCamelCase($value);
23
+            $camelCase[LuStringUtils::snakeToCamelCase($key, $capitalizeFirstCharacter)] =
24
+                self::snakeToCamelCase($value, $capitalizeFirstCharacter);
24
         }
25
         }
25
         return $camelCase;
26
         return $camelCase;
26
     }
27
     }
38
         return $camelCase;
39
         return $camelCase;
39
     }
40
     }
40
 
41
 
41
-    public static function convertJsonString($json)
42
+    public static function convertJsonString($json, $capitalizeFirstCharacter = false)
42
     {
43
     {
43
-        return LuStringUtils::convertJsonString($json);
44
+        return LuStringUtils::convertJsonString($json, $capitalizeFirstCharacter);
44
     }
45
     }
45
 
46
 
46
-    public static function objectToArray($d)
47
+    public static function objectToArray($d, $capitalizeFirstCharacter = false)
47
     {
48
     {
48
         if (is_object($d)) {
49
         if (is_object($d)) {
49
             $d = get_object_vars($d);
50
             $d = get_object_vars($d);
52
         if (is_array($d)) {
53
         if (is_array($d)) {
53
             $data = [];
54
             $data = [];
54
             foreach ($d as $key => $value) {
55
             foreach ($d as $key => $value) {
55
-                $data[$key] = self::objectToArray($value);
56
+                $data[$key] = self::objectToArray($value, $capitalizeFirstCharacter);
56
             }
57
             }
57
-            return self::snakeToCamelCase($data);
58
+            return self::snakeToCamelCase($data, $capitalizeFirstCharacter);
58
         }
59
         }
59
         else {
60
         else {
60
             return $d;
61
             return $d;

+ 6
- 3
src/Utils/Business/LuStringUtils.php View File

12
 
12
 
13
 class LuStringUtils
13
 class LuStringUtils
14
 {
14
 {
15
-    public static function snakeToCamelCase($string, $capitalizeFirstCharacter)
15
+    public static function snakeToCamelCase($string, $capitalizeFirstCharacter = false)
16
     {
16
     {
17
         $str = preg_replace_callback("/_[a-zA-Z0-9]/", function($matches)
17
         $str = preg_replace_callback("/_[a-zA-Z0-9]/", function($matches)
18
         {
18
         {
21
         if ($capitalizeFirstCharacter && !empty($str)) {
21
         if ($capitalizeFirstCharacter && !empty($str)) {
22
             $str[0] = strtoupper($str[0]);
22
             $str[0] = strtoupper($str[0]);
23
         }
23
         }
24
+        else {
25
+            $str[0] = strtolower($str[0]);
26
+        }
24
         return $str;
27
         return $str;
25
     }
28
     }
26
     
29
     
36
         return $str;
39
         return $str;
37
     }
40
     }
38
 
41
 
39
-    public static function convertJsonString($json)
42
+    public static function convertJsonString($json, $capitalizeFirstCharacter = false)
40
     {
43
     {
41
         $array = json_decode($json, true);
44
         $array = json_decode($json, true);
42
-        return LuArrayUtils::snakeToCamelCase($array);
45
+        return LuArrayUtils::snakeToCamelCase($array, $capitalizeFirstCharacter);
43
     }
46
     }
44
 
47
 
45
     /**
48
     /**

+ 2
- 2
src/Utils/Dbo/LuDbo.php View File

24
         $reflect = new \ReflectionClass(static::class);
24
         $reflect = new \ReflectionClass(static::class);
25
         $properties = $reflect->getProperties();
25
         $properties = $reflect->getProperties();
26
         foreach ($properties as $property) {
26
         foreach ($properties as $property) {
27
-            $name = LuStringUtils::snakeToCamelCase($property->getName(), true);
27
+            $name = LuStringUtils::snakeToCamelCase($property->getName(), false);
28
             $property->setAccessible(true);
28
             $property->setAccessible(true);
29
             $value = $property->getValue($this);
29
             $value = $property->getValue($this);
30
             $json[$name] = static::serializeValue($value);
30
             $json[$name] = static::serializeValue($value);
144
         $properties = $reflect->getProperties();
144
         $properties = $reflect->getProperties();
145
         foreach ($properties as $property) {
145
         foreach ($properties as $property) {
146
             $parser = new LuPropertyDocParser($property->getDocComment());
146
             $parser = new LuPropertyDocParser($property->getDocComment());
147
-            $name = LuStringUtils::snakeToCamelCase($property->getName(), true);
147
+            $name = LuStringUtils::snakeToCamelCase($property->getName(), false);
148
             $doc = $parser->parse();
148
             $doc = $parser->parse();
149
             $type = is_null($doc) ? null : $doc->getType();
149
             $type = is_null($doc) ? null : $doc->getType();
150
 
150
 

+ 5
- 5
src/Utils/Dbo/LuPaginatedDbo.php View File

33
     function jsonSerialize()
33
     function jsonSerialize()
34
     {
34
     {
35
         return array(
35
         return array(
36
-            "Count" => $this->_count,
37
-            "Data" => LuDbo::serializeValue($this->_data)
36
+            "count" => $this->_count,
37
+            "data" => LuDbo::serializeValue($this->_data)
38
         );
38
         );
39
     }
39
     }
40
 
40
 
55
     
55
     
56
     public static function jsonDeserialize($json)
56
     public static function jsonDeserialize($json)
57
     {
57
     {
58
-        return new static($json["Count"], $json["Data"]);
58
+        return new static($json["count"], $json["data"]);
59
     }
59
     }
60
 
60
 
61
     public static function generateSample()
61
     public static function generateSample()
62
     {
62
     {
63
         return [
63
         return [
64
-            "Count" => 42,
65
-            "Data" => []
64
+            "count" => 42,
65
+            "data" => []
66
         ];
66
         ];
67
     }
67
     }
68
 }
68
 }

+ 0
- 8
src/Utils/Dbo/LuParameterConstraintDbo.php View File

21
      */
21
      */
22
     private $_arguments = [];
22
     private $_arguments = [];
23
 
23
 
24
-    function jsonSerialize()
25
-    {
26
-        return [
27
-            "Method" => $this->_method,
28
-            "Arguments" => $this->_arguments
29
-        ];
30
-    }
31
-
32
     /**
24
     /**
33
      * @return string
25
      * @return string
34
      */
26
      */

+ 7
- 7
tests/DatabaseTest.php View File

205
 
205
 
206
     public function testSpGetMultiplePaginated1()
206
     public function testSpGetMultiplePaginated1()
207
     {
207
     {
208
-        $this->assertSame(["Count" => 16, "Data" => range(0, 1)], SpTest2::executePaginated(42, 0, 2)->jsonSerialize());
208
+        $this->assertSame(["count" => 16, "data" => range(0, 1)], SpTest2::executePaginated(42, 0, 2)->jsonSerialize());
209
     }
209
     }
210
 
210
 
211
     public function testSpGetMultiplePaginated2()
211
     public function testSpGetMultiplePaginated2()
212
     {
212
     {
213
-        $this->assertSame(["Count" => 16, "Data" => range(2, 3)], SpTest2::executePaginated(42, 1, 2)->jsonSerialize());
213
+        $this->assertSame(["count" => 16, "data" => range(2, 3)], SpTest2::executePaginated(42, 1, 2)->jsonSerialize());
214
     }
214
     }
215
 
215
 
216
     public function testDaGetMultiple()
216
     public function testDaGetMultiple()
217
     {
217
     {
218
-        $this->assertSame([["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]],
218
+        $this->assertSame([["id" => 1, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"]],
219
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
219
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
220
             {
220
             {
221
                 return $q->where("id", "=", 1);
221
                 return $q->where("id", "=", 1);
224
 
224
 
225
     public function testDaGetMultipleOffset()
225
     public function testDaGetMultipleOffset()
226
     {
226
     {
227
-        $this->assertSame([["Id" => 11, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]],
227
+        $this->assertSame([["id" => 11, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"]],
228
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
228
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
229
             {
229
             {
230
                 return $q->take(1)->offset(2);
230
                 return $q->take(1)->offset(2);
233
 
233
 
234
     public function testDaGetPaginated()
234
     public function testDaGetPaginated()
235
     {
235
     {
236
-        $this->assertSame(["Count" => 3, "Data" =>
237
-            [["Id" => 11, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]]],
236
+        $this->assertSame(["count" => 3, "data" =>
237
+            [["id" => 11, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"]]],
238
             LuDbo::serializeValue(TestTableDataAccess::getMultiplePaginated(function(Builder $q)
238
             LuDbo::serializeValue(TestTableDataAccess::getMultiplePaginated(function(Builder $q)
239
             {
239
             {
240
                 return $q->orderBy("id", "ASC");
240
                 return $q->orderBy("id", "ASC");
243
 
243
 
244
     public function testDaGetSingleById()
244
     public function testDaGetSingleById()
245
     {
245
     {
246
-        $this->assertSame(["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"],
246
+        $this->assertSame(["id" => 1, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"],
247
             LuDbo::serializeValue(TestTableDataAccess::getSingleById(1)));
247
             LuDbo::serializeValue(TestTableDataAccess::getSingleById(1)));
248
     }
248
     }
249
     
249
     

+ 12
- 12
tests/LuDboDeserializeTest.php View File

92
     
92
     
93
     public function test()
93
     public function test()
94
     {
94
     {
95
-        $json = ["Test" => "Test."];
95
+        $json = ["test" => "Test."];
96
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo')->jsonSerialize());
96
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo')->jsonSerialize());
97
     }
97
     }
98
     
98
     
99
     public function test2()
99
     public function test2()
100
     {
100
     {
101
         $json = [
101
         $json = [
102
-            "Test2" => [
103
-                ["Test" => "Test."],
104
-                ["Test" => "Test.2"]
102
+            "test2" => [
103
+                ["test" => "Test."],
104
+                ["test" => "Test.2"]
105
             ],
105
             ],
106
-            "Test3" => ["Test" => "Test.3"]
106
+            "test3" => ["test" => "Test.3"]
107
         ];
107
         ];
108
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
108
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
109
     }
109
     }
111
     public function test3()
111
     public function test3()
112
     {
112
     {
113
         $json = [
113
         $json = [
114
-            "Test2" => [
115
-                ["Test" => "Test."],
116
-                ["Test" => "Test.2"]
114
+            "test2" => [
115
+                ["test" => "Test."],
116
+                ["test" => "Test.2"]
117
             ],
117
             ],
118
-            "Test3" => null
118
+            "test3" => null
119
         ];
119
         ];
120
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
120
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
121
     }
121
     }
123
     public function test4()
123
     public function test4()
124
     {
124
     {
125
         $json = [
125
         $json = [
126
-            "Test2" => [
127
-                ["Test" => "Test."],
126
+            "test2" => [
127
+                ["test" => "Test."],
128
                 null
128
                 null
129
             ],
129
             ],
130
-            "Test3" => null
130
+            "test3" => null
131
         ];
131
         ];
132
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
132
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
133
     }
133
     }

+ 17
- 17
tests/LuMethodDocParserTest.php View File

44
         }
44
         }
45
         
45
         
46
         $expectedParams = [
46
         $expectedParams = [
47
-            ["NotNull" => true, "Summary" => "some doc\n", "Type" => "MyType", "Name" => 'myvar',
48
-                "Constraints" => []],
49
-            ["NotNull" => true, "Summary" => "some other doc\n", "Type" => "MyType", "Name" => 'myvar2',
50
-                "Constraints" => []]
47
+            ["notNull" => true, "summary" => "some doc\n", "type" => "MyType", "name" => 'myvar',
48
+                "constraints" => []],
49
+            ["notNull" => true, "summary" => "some other doc\n", "type" => "MyType", "name" => 'myvar2',
50
+                "constraints" => []]
51
         ];
51
         ];
52
         
52
         
53
         $this->assertSame($expectedParams, $params);
53
         $this->assertSame($expectedParams, $params);
70
         }
70
         }
71
 
71
 
72
         $expectedParams = [
72
         $expectedParams = [
73
-            'myvar' => ["NotNull" => true, "Summary" => "some doc\n", "Type" => "MyType", "Name" => 'myvar',
74
-                "Constraints" => []],
75
-            'myvar2' => ["NotNull" => true, "Summary" => "some other doc\non another line\n", "Type" => "MyType",
76
-                "Name" => 'myvar2', "Constraints" => []]
73
+            'myvar' => ["notNull" => true, "summary" => "some doc\n", "type" => "MyType", "name" => 'myvar',
74
+                "constraints" => []],
75
+            'myvar2' => ["notNull" => true, "summary" => "some other doc\non another line\n", "type" => "MyType",
76
+                "name" => 'myvar2', "constraints" => []]
77
         ];
77
         ];
78
 
78
 
79
         $this->assertSame($expectedParams, $params);
79
         $this->assertSame($expectedParams, $params);
106
         }
106
         }
107
 
107
 
108
         $expectedParams = [
108
         $expectedParams = [
109
-            'myvar' => ["NotNull" => true, "Summary" => "some doc\nToo much doc\n", "Type" => "MyType",
110
-                "Name" => 'myvar', "Constraints" => []],
111
-            'myvar2' => ["NotNull" => false, "Summary" => "some other doc\non another line\nand another\n",
112
-                "Type" => "MyType", "Name" => 'myvar2',
113
-                "Constraints" => [
114
-                    ["Method" => "min", "Arguments" => [42]],
115
-                    ["Method" => "max", "Arguments" => [42]],
116
-                    ["Method" => "between", "Arguments" => [0, 42]],
117
-                    ["Method" => "another", "Arguments" => [1, 2, 42.42, true, false, "a string", "string", null]]
109
+            'myvar' => ["notNull" => true, "summary" => "some doc\nToo much doc\n", "type" => "MyType",
110
+                "name" => 'myvar', "constraints" => []],
111
+            'myvar2' => ["notNull" => false, "summary" => "some other doc\non another line\nand another\n",
112
+                "type" => "MyType", "name" => 'myvar2',
113
+                "constraints" => [
114
+                    ["method" => "min", "arguments" => [42]],
115
+                    ["method" => "max", "arguments" => [42]],
116
+                    ["method" => "between", "arguments" => [0, 42]],
117
+                    ["method" => "another", "arguments" => [1, 2, 42.42, true, false, "a string", "string", null]]
118
                 ]
118
                 ]
119
             ]
119
             ]
120
         ];
120
         ];

+ 2
- 2
tests/LuRouteTest.php View File

130
         $router = new LuRoute();
130
         $router = new LuRoute();
131
         $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameter");
131
         $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameter");
132
         $router->setup();
132
         $router->setup();
133
-        $res = $router->dispatch("GET", "/test", ["test" => '{"Test": "Some value"}']);
133
+        $res = $router->dispatch("GET", "/test", ["test" => '{"test": "Some value"}']);
134
         $this->assertSame("Some value", $res);
134
         $this->assertSame("Some value", $res);
135
     }
135
     }
136
 
136
 
139
         $router = new LuRoute();
139
         $router = new LuRoute();
140
         $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
140
         $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
141
         $router->setup();
141
         $router->setup();
142
-        $res = $router->dispatch("GET", "/test", ["test" => '{"Test": "42"}']);
142
+        $res = $router->dispatch("GET", "/test", ["test" => '{"test": "42"}']);
143
         $this->assertSame("42", $res);
143
         $this->assertSame("42", $res);
144
     }
144
     }
145
 
145
 

Loading…
Cancel
Save