File tree 1 file changed +20
-6
lines changed
1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -36,18 +36,32 @@ public function convertArrayToDocument(string $namespace, array $raw)
36
36
37
37
public function convertDocumentToArray ($ document ): array
38
38
{
39
- $ class = get_class ($ document );
39
+ $ documentClass = get_class ($ document );
40
+ $ class = $ this ->getConvertableClass ($ documentClass );
40
41
41
- if (! isset ( $ this -> propertyMetadata [ $ class]) ) {
42
- throw new \ Exception ( " Cannot convert object of class ` $ class` to array. " );
42
+ if ($ class !== null ) {
43
+ return $ this -> normalize ( $ document , null , $ class );
43
44
}
44
45
45
- return $ this -> normalize ( $ document );
46
+ throw new \ Exception ( " Cannot convert object of class ` $ documentClass ` to array. " );
46
47
}
47
48
48
- protected function normalize ($ document , $ metadata = null )
49
+ public function getConvertableClass ($ class ) {
50
+ while ($ class ) {
51
+ if (isset ($ this ->propertyMetadata [$ class ])) {
52
+ return $ class ;
53
+ }
54
+
55
+ $ class = get_parent_class ($ class );
56
+ }
57
+
58
+ return null ;
59
+ }
60
+
61
+ protected function normalize ($ document , $ metadata = null , $ class = null )
49
62
{
50
- $ metadata = $ metadata ?? $ this ->propertyMetadata [get_class ($ document )];
63
+ $ class = $ class ?? get_class ($ document );
64
+ $ metadata = $ metadata ?? $ this ->propertyMetadata [$ class ];
51
65
$ result = [];
52
66
53
67
foreach ($ metadata as $ field => $ fieldMeta ) {
You can’t perform that action at this time.
0 commit comments