99use Contentstack \Utils \Enum \NodeType ;
1010use Contentstack \Utils \Enum \MarkType ;
1111
12- class Utils
12+ class Utils extends BaseParser
1313{
1414 /**
1515 *
@@ -70,94 +70,21 @@ public static function jsonArrayToHtml(array $contents, Option $option): array {
7070 public static function jsonToHtml (object $ content , Option $ option ): string {
7171 $ resultHtml = '' ;
7272 if (isset ($ content ->children )) {
73- $ resultHtml = Utils::nodeChildrenToHtml ($ content ->children , $ option );
74- }
75- return $ resultHtml ;
76- }
77-
78- private static function nodeChildrenToHtml (array $ nodes , Option $ option ): string {
79- return \implode ('' , \array_map (function (object $ node ) use ($ option ): string {
80- return Utils::nodeToHtml ($ node , $ option );
81- }, $ nodes ));
82- }
83-
84- private static function nodeToHtml (object $ node , Option $ option ): string {
85- $ resultHtml = '' ;
86- if (isset ($ node ->type )) {
87- switch ($ node ->type ) {
88- case NodeType::get (NodeType::REFERENCE )->getValue ():
89- $ resultHtml = Utils::referenceToHtml ($ node , $ option );
90- break ;
91- default :
92- $ innerHtml = "" ;
93- if (isset ($ node ->children ))
94- {
95- $ innerHtml = Utils::nodeChildrenToHtml ($ node ->children , $ option );
73+ $ resultHtml = Utils::nodeChildrenToHtml ($ content ->children , $ option , function (Metadata $ metadata ) use ($ option ): string {
74+ $ resultHtml = '' ;
75+ if ($ option ->entry ) {
76+ $ object = Utils::findObject ($ metadata , $ option ->entry );
77+ if (count ($ object ) > 0 ) {
78+ $ resultHtml = $ option ->renderOptions ($ object , $ metadata );
9679 }
97- $ resultHtml = $ option ->renderNode (
98- $ node ->type ,
99- $ node ,
100- $ innerHtml
101- );
102- break ;
103- }
104- } else {
105- $ resultHtml = Utils::textToHtml ($ node , $ option );
106- }
107- return $ resultHtml ;
108- }
109-
110- private static function textToHtml (object $ node , Option $ option )
111- {
112- $ text = $ node ->text ;
113- if (isset ($ node ->superscript ) && $ node ->superscript ) {
114- $ text = $ option ->renderMark (MarkType::get (MarkType::SUPERSCRIPT ), $ text );
115- }
116- if (isset ($ node ->subscript ) && $ node ->subscript ) {
117- $ text = $ option ->renderMark (MarkType::get (MarkType::SUBSCRIPT ), $ text );
118- }
119- if (isset ($ node ->inlineCode ) && $ node ->inlineCode ) {
120- $ text = $ option ->renderMark (MarkType::get (MarkType::INLINE_CODE ), $ text );
121- }
122- if (isset ($ node ->strikethrough ) && $ node ->strikethrough ) {
123- $ text = $ option ->renderMark (MarkType::get (MarkType::STRIKE_THROUGH ), $ text );
124- }
125- if (isset ($ node ->underline ) && $ node ->underline ) {
126- $ text = $ option ->renderMark (MarkType::get (MarkType::UNDERLINE ), $ text );
127- }
128- if (isset ($ node ->italic ) && $ node ->italic ) {
129- $ text = $ option ->renderMark (MarkType::get (MarkType::ITALIC ), $ text );
130- }
131- if (isset ($ node ->bold ) && $ node ->bold ) {
132- $ text = $ option ->renderMark (MarkType::get (MarkType::BOLD ), $ text );
133- }
134- return $ text ;
135- }
136-
137- private static function referenceToHtml (object $ node , Option $ option )
138- {
139- $ resultHtml = '' ;
140- if ($ option ->entry ) {
141- $ metadata = new Metadata ($ node );
142- $ object = Utils::findObject ($ metadata , $ option ->entry );
143- if (count ($ object ) > 0 ) {
144- $ resultHtml = $ option ->renderOptions ($ object , $ metadata );
145- }
80+ }
81+ return $ resultHtml ;
82+ });
14683 }
14784 return $ resultHtml ;
14885 }
14986
150- private static function findEmbeddedObject (\DOMDocument $ doc ): array {
151- $ xpath = new \DOMXPath ($ doc );
152- $ elements = $ xpath ->query ('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")] ' );
153- $ metadataArray = array ();
154- foreach ($ elements as $ node ) {
155- $ metadataArray [] = new Metadata ($ node );
156- }
157- return $ metadataArray ;
158- }
159-
160- private static function findObject (Metadata $ metadata , array $ entry ): array
87+ protected static function findObject (Metadata $ metadata , array $ entry ): array
16188 {
16289 if (array_key_exists ('_embedded_items ' , $ entry ))
16390 {
@@ -174,14 +101,4 @@ private static function findObject(Metadata $metadata, array $entry): array
174101 }
175102 return [];
176103 }
177-
178- static function innerHTML (\DOMElement $ element )
179- {
180- $ doc = $ element ->ownerDocument ;
181- $ html = '' ;
182- foreach ($ element ->childNodes as $ node ) {
183- $ html .= $ doc ->saveHTML ($ node );
184- }
185- return $ html ;
186- }
187104}
0 commit comments