11<?php
2+
23/**
34 * @link https://www.yiiframework.com/
45 * @copyright Copyright (c) 2008 Yii Software LLC
1314use yii \apidoc \models \TraitDoc ;
1415use yii \apidoc \renderers \ApiRenderer as BaseApiRenderer ;
1516use yii \base \ViewContextInterface ;
16- use Yii ;
1717
1818/**
1919 * The class for outputting documentation data structures as a JSON text.
@@ -32,8 +32,8 @@ class ApiRenderer extends BaseApiRenderer implements ViewContextInterface
3232 public function render ($ context , $ targetDir )
3333 {
3434 $ types = array_merge ($ context ->classes , $ context ->interfaces , $ context ->traits );
35- foreach ($ types as $ name => $ type ) {
36- $ types [$ name ] = (array ) $ type ;
35+ foreach ($ types as $ name => $ type ) {
36+ $ types [$ name ] = (array ) $ this -> removeParentFieldsRecursive ( $ type) ;
3737 if ($ type instanceof ClassDoc) {
3838 $ types [$ name ]['type ' ] = 'class ' ;
3939 } elseif ($ type instanceof InterfaceDoc) {
@@ -42,7 +42,41 @@ public function render($context, $targetDir)
4242 $ types [$ name ]['type ' ] = 'trait ' ;
4343 }
4444 }
45- file_put_contents ($ targetDir . '/types.json ' , json_encode ($ types , JSON_PRETTY_PRINT ));
45+ file_put_contents ($ targetDir . '/types.json ' , json_encode ($ types , JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR ));
46+ }
47+
48+ /**
49+ * Removes the `parent` fields recursively so that the data could be converted to JSON format..
50+ * @param array|object $data
51+ * @return array|object
52+ */
53+ private function removeParentFieldsRecursive ($ data )
54+ {
55+ if (!is_array ($ data ) && !is_object ($ data )) {
56+ return $ data ;
57+ }
58+
59+ if (is_array ($ data )) {
60+ foreach ($ data as $ key => $ value ) {
61+ if ($ key === 'parent ' ) {
62+ unset($ data [$ key ]);
63+ } elseif (is_object ($ value ) || is_array ($ value )) {
64+ $ data [$ key ] = $ this ->removeParentFieldsRecursive ($ value );
65+ }
66+ }
67+
68+ return $ data ;
69+ }
70+
71+ foreach (get_object_vars ($ data ) as $ key => $ value ) {
72+ if ($ key === 'parent ' ) {
73+ unset($ data ->$ key );
74+ } elseif (is_object ($ value ) || is_array ($ value )) {
75+ $ data ->$ key = $ this ->removeParentFieldsRecursive ($ value );
76+ }
77+ }
78+
79+ return $ data ;
4680 }
4781
4882 /**
0 commit comments