Parcourir la source

switched json serialization to lower camel case

develop
Robin Thoni il y a 8 ans
Parent
révision
61328ce006

+ 8
- 7
src/Utils/Business/LuArrayUtils.php Voir le fichier

@@ -11,7 +11,7 @@ namespace Luticate\Utils\Business;
11 11
 
12 12
 class LuArrayUtils
13 13
 {
14
-    public static function snakeToCamelCase($array)
14
+    public static function snakeToCamelCase($array, $capitalizeFirstCharacter = false)
15 15
     {
16 16
         if (!is_array($array))
17 17
         {
@@ -20,7 +20,8 @@ class LuArrayUtils
20 20
         $camelCase = [];
21 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 26
         return $camelCase;
26 27
     }
@@ -38,12 +39,12 @@ class LuArrayUtils
38 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 49
         if (is_object($d)) {
49 50
             $d = get_object_vars($d);
@@ -52,9 +53,9 @@ class LuArrayUtils
52 53
         if (is_array($d)) {
53 54
             $data = [];
54 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 60
         else {
60 61
             return $d;

+ 6
- 3
src/Utils/Business/LuStringUtils.php Voir le fichier

@@ -12,7 +12,7 @@ use Carbon\Carbon;
12 12
 
13 13
 class LuStringUtils
14 14
 {
15
-    public static function snakeToCamelCase($string, $capitalizeFirstCharacter)
15
+    public static function snakeToCamelCase($string, $capitalizeFirstCharacter = false)
16 16
     {
17 17
         $str = preg_replace_callback("/_[a-zA-Z0-9]/", function($matches)
18 18
         {
@@ -21,6 +21,9 @@ class LuStringUtils
21 21
         if ($capitalizeFirstCharacter && !empty($str)) {
22 22
             $str[0] = strtoupper($str[0]);
23 23
         }
24
+        else {
25
+            $str[0] = strtolower($str[0]);
26
+        }
24 27
         return $str;
25 28
     }
26 29
     
@@ -36,10 +39,10 @@ class LuStringUtils
36 39
         return $str;
37 40
     }
38 41
 
39
-    public static function convertJsonString($json)
42
+    public static function convertJsonString($json, $capitalizeFirstCharacter = false)
40 43
     {
41 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 Voir le fichier

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

+ 5
- 5
src/Utils/Dbo/LuPaginatedDbo.php Voir le fichier

@@ -33,8 +33,8 @@ class LuPaginatedDbo extends LuDbo {
33 33
     function jsonSerialize()
34 34
     {
35 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,14 +55,14 @@ class LuPaginatedDbo extends LuDbo {
55 55
     
56 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 61
     public static function generateSample()
62 62
     {
63 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 Voir le fichier

@@ -21,14 +21,6 @@ class LuParameterConstraintDbo extends LuDbo
21 21
      */
22 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 25
      * @return string
34 26
      */

+ 7
- 7
tests/DatabaseTest.php Voir le fichier

@@ -205,17 +205,17 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
205 205
 
206 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 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 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 219
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
220 220
             {
221 221
                 return $q->where("id", "=", 1);
@@ -224,7 +224,7 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
224 224
 
225 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 228
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
229 229
             {
230 230
                 return $q->take(1)->offset(2);
@@ -233,8 +233,8 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
233 233
 
234 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 238
             LuDbo::serializeValue(TestTableDataAccess::getMultiplePaginated(function(Builder $q)
239 239
             {
240 240
                 return $q->orderBy("id", "ASC");
@@ -243,7 +243,7 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
243 243
 
244 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 247
             LuDbo::serializeValue(TestTableDataAccess::getSingleById(1)));
248 248
     }
249 249
     

+ 12
- 12
tests/LuDboDeserializeTest.php Voir le fichier

@@ -92,18 +92,18 @@ class LuDboDeserializeTest extends \PHPUnit_Framework_TestCase{
92 92
     
93 93
     public function test()
94 94
     {
95
-        $json = ["Test" => "Test."];
95
+        $json = ["test" => "Test."];
96 96
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo')->jsonSerialize());
97 97
     }
98 98
     
99 99
     public function test2()
100 100
     {
101 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 108
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
109 109
     }
@@ -111,11 +111,11 @@ class LuDboDeserializeTest extends \PHPUnit_Framework_TestCase{
111 111
     public function test3()
112 112
     {
113 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 120
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
121 121
     }
@@ -123,11 +123,11 @@ class LuDboDeserializeTest extends \PHPUnit_Framework_TestCase{
123 123
     public function test4()
124 124
     {
125 125
         $json = [
126
-            "Test2" => [
127
-                ["Test" => "Test."],
126
+            "test2" => [
127
+                ["test" => "Test."],
128 128
                 null
129 129
             ],
130
-            "Test3" => null
130
+            "test3" => null
131 131
         ];
132 132
         $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
133 133
     }

+ 17
- 17
tests/LuMethodDocParserTest.php Voir le fichier

@@ -44,10 +44,10 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
44 44
         }
45 45
         
46 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 53
         $this->assertSame($expectedParams, $params);
@@ -70,10 +70,10 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
70 70
         }
71 71
 
72 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 79
         $this->assertSame($expectedParams, $params);
@@ -106,15 +106,15 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
106 106
         }
107 107
 
108 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 Voir le fichier

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

Chargement…
Annuler
Enregistrer