File tree 5 files changed +61
-10
lines changed
5 files changed +61
-10
lines changed Original file line number Diff line number Diff line change @@ -113,34 +113,34 @@ protected function isEmpty($value) : bool
113
113
/**
114
114
* Appends the given value to an array of data on the path to serializing to a JSON string
115
115
*/
116
- public function appendValue (object $ data , $ value )
116
+ public function appendValue (array & $ data , $ value )
117
117
{
118
118
if ($ this ->omit_empty && $ this ->isEmpty ($ value )) {
119
119
return ;
120
120
}
121
121
$ max = count ($ this ->path )-1 ;
122
- $ d = $ data ;
122
+ $ d = & $ data ;
123
123
foreach ($ this ->path as $ i => $ pathBit ) {
124
- if (property_exists ( $ d , $ pathBit ) && $ i === $ max ) {
124
+ if (array_key_exists ( $ pathBit , $ d ) && $ i === $ max ) {
125
125
throw new \Exception ('invalid path: ' .json_encode ($ this ->path ));
126
126
}
127
127
128
- if (!property_exists ( $ d , $ pathBit ) && $ i < $ max ) {
129
- $ d-> $ pathBit = new stdClass ;
128
+ if (!array_key_exists ( $ pathBit , $ d ) && $ i < $ max ) {
129
+ $ d[ $ pathBit] = [] ;
130
130
}
131
131
132
132
if ($ i < $ max ) {
133
- $ d = $ data -> $ pathBit ;
133
+ $ d = & $ d [ $ pathBit] ;
134
134
}
135
135
136
136
if ($ i === $ max ) {
137
137
if (is_array ($ value )) {
138
- $ d-> $ pathBit = [];
138
+ $ d[ $ pathBit] = [];
139
139
foreach ($ value as $ k => $ val ) {
140
- $ d-> $ pathBit [$ k ] = $ this ->jsonValue ($ val );
140
+ $ d[ $ pathBit] [$ k ] = $ this ->jsonValue ($ val );
141
141
}
142
142
} else {
143
- $ d-> $ pathBit = $ this ->jsonValue ($ value );
143
+ $ d[ $ pathBit] = $ this ->jsonValue ($ value );
144
144
}
145
145
}
146
146
}
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public function toJsonData()
19
19
{
20
20
$ r = RClass::make ($ this );
21
21
$ props = $ r ->getProperties ();
22
- $ d = new stdClass ;
22
+ $ d = [] ;
23
23
foreach ($ props as $ prop ) {
24
24
$ attrs = $ prop ->getAttributes (Json::class, ReflectionAttribute::IS_INSTANCEOF );
25
25
if (empty ($ attrs )) {
Original file line number Diff line number Diff line change 3
3
4
4
use PHPUnit \Framework \TestCase ;
5
5
use ReflectionClass ;
6
+ use Square \Pjson \Json ;
7
+ use Square \Pjson \JsonSerialize ;
6
8
use Square \Pjson \Tests \Definitions \BigCat ;
7
9
use Square \Pjson \Tests \Definitions \BigInt ;
8
10
use Square \Pjson \Tests \Definitions \CatalogCategory ;
9
11
use Square \Pjson \Tests \Definitions \CatalogItem ;
10
12
use Square \Pjson \Tests \Definitions \CatalogObject ;
11
13
use Square \Pjson \Tests \Definitions \Category ;
14
+ use Square \Pjson \Tests \Definitions \MenuList ;
12
15
use Square \Pjson \Tests \Definitions \Schedule ;
13
16
use Square \Pjson \Tests \Definitions \Privateer ;
14
17
use Square \Pjson \Tests \Definitions \Stats ;
@@ -454,4 +457,21 @@ public function testClassToScalar()
454
457
]
455
458
], $ this ->export ($ stats ));
456
459
}
460
+
461
+ public function testIntegerPath ()
462
+ {
463
+ $ json = '{
464
+ "menus": [
465
+ {"main": true, "name": "main-menu"},
466
+ {"main": false, "name": "secondary-menu"}
467
+ ]
468
+ } ' ;
469
+
470
+ $ dl = MenuList::fromJsonString ($ json );
471
+
472
+ $ this ->assertEquals ([
473
+ "@class " => MenuList::class,
474
+ "mainMenuName " => "main-menu "
475
+ ], $ this ->export ($ dl ));
476
+ }
457
477
}
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types=1 );
2
+
3
+ namespace Square \Pjson \Tests \Definitions ;
4
+
5
+ use Square \Pjson \Json ;
6
+ use Square \Pjson \JsonSerialize ;
7
+
8
+ class MenuList
9
+ {
10
+ use JsonSerialize;
11
+
12
+ #[Json(['menus ' , 0 , 'name ' ])]
13
+ public string $ mainMenuName ;
14
+ }
Original file line number Diff line number Diff line change 10
10
use Square \Pjson \Tests \Definitions \CatalogItem ;
11
11
use Square \Pjson \Tests \Definitions \CatalogObject ;
12
12
use Square \Pjson \Tests \Definitions \Category ;
13
+ use Square \Pjson \Tests \Definitions \MenuList ;
13
14
use Square \Pjson \Tests \Definitions \Privateer ;
14
15
use Square \Pjson \Tests \Definitions \Schedule ;
15
16
use Square \Pjson \Tests \Definitions \Stats ;
@@ -226,4 +227,20 @@ public function testClassToScalar()
226
227
$ stats ->toJson ()
227
228
);
228
229
}
230
+
231
+ public function testIntegerPath ()
232
+ {
233
+ $ json = '{
234
+ "menus": [
235
+ {"main": true, "name": "main-menu"},
236
+ {"main": false, "name": "secondary-menu"}
237
+ ]
238
+ } ' ;
239
+
240
+ $ dl = MenuList::fromJsonString ($ json );
241
+
242
+ // MenuList doesn't store the entire structure. Only the name of the first menu
243
+ // it can however still output all the data it has back into its original shape
244
+ $ this ->assertEquals ('{"menus":[{"name":"main-menu"}]} ' , $ dl ->toJson ());
245
+ }
229
246
}
You can’t perform that action at this time.
0 commit comments