File tree Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Original file line number Diff line number Diff line change @@ -147,8 +147,8 @@ public static function isIndexedArray(array $array): bool
147
147
148
148
/**
149
149
* Converts assoc-arrays to objects (recursive)
150
- * @param $schema
151
- * @return mixed
150
+ * @param scalar|\stdClass|array $schema
151
+ * @return scalar|\stdClass|array
152
152
*/
153
153
public static function convertAssocArrayToObject ($ schema )
154
154
{
@@ -161,7 +161,7 @@ public static function convertAssocArrayToObject($schema)
161
161
$ data = [];
162
162
163
163
foreach ($ schema as $ key => $ value ) {
164
- $ data [$ key ] = is_array ($ value ) || is_object ($ value ) ? self ::convertAssocArrayToObject ($ schema ) : $ value ;
164
+ $ data [$ key ] = is_array ($ value ) || is_object ($ value ) ? self ::convertAssocArrayToObject ($ value ) : $ value ;
165
165
}
166
166
167
167
return $ keepArray ? $ data : (object ) $ data ;
@@ -348,4 +348,4 @@ public static function toJSON($data)
348
348
349
349
return (object ) $ map ;
350
350
}
351
- }
351
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Opis \JsonSchema \Test ;
4
+
5
+ use Opis \JsonSchema \Helper ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ class HelperTest extends TestCase
9
+ {
10
+ public function testConvertAssocArrayToObject ()
11
+ {
12
+ $ object = Helper::convertAssocArrayToObject (['a ' => ['b ' => ['c ' => 'd ' ]]]);
13
+ $ this ->assertSame ('d ' , $ object ->a ->b ->c );
14
+ }
15
+
16
+ public function testConvertAssocArrayWithObjectToObject ()
17
+ {
18
+ $ object = Helper::convertAssocArrayToObject (['a ' => (object ) ['b ' => ['c ' => 'd ' ]]]);
19
+ $ this ->assertSame ('d ' , $ object ->a ->b ->c );
20
+ }
21
+
22
+ public function testConvertAssocArrayWithIndexedArrayToObject ()
23
+ {
24
+ $ object = Helper::convertAssocArrayToObject (['a ' => ['b ' , ['c ' => 'd ' ]]]);
25
+ $ this ->assertSame ('b ' , $ object ->a [0 ]);
26
+ $ this ->assertSame ('d ' , $ object ->a [1 ]->c );
27
+ }
28
+
29
+ public function testConvertAssocArrayToObjectWithScalar ()
30
+ {
31
+ $ this ->assertNull (Helper::convertAssocArrayToObject (null ));
32
+ $ this ->assertSame (2 , Helper::convertAssocArrayToObject (2 ));
33
+ $ this ->assertSame ('foo ' , Helper::convertAssocArrayToObject ('foo ' ));
34
+ $ this ->assertSame (true , Helper::convertAssocArrayToObject (true ));
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments