66
77use Contentstack \Utils \Model \Option ;
88use Contentstack \Utils \Model \Metadata ;
9+ use Contentstack \Utils \Enum \NodeType ;
10+ use Contentstack \Utils \Enum \MarkType ;
911
1012class Utils
1113{
@@ -44,7 +46,7 @@ public static function renderContent(string $content, Option $option): string
4446 *
4547 *
4648 * @param string $content RTE content to render embedded objects
47- * @param Option $option Option containing Entry and RendarableInterface
49+ * @param Option $option Option containing Entry and RenderableInterface
4850 *
4951 * @return string Returns RTE content with render embedded objects
5052 */
@@ -57,6 +59,68 @@ public static function renderContents(array $contents, Option $option): array
5759 return $ result ;
5860 }
5961
62+ public static function jsonArrayToHtml (array $ contents , Option $ option ): array {
63+ $ result = array ();
64+ foreach ($ contents as $ content ) {
65+ $ result [] = Utils::jsonToHtml ($ content , $ option );
66+ }
67+ return $ result ;
68+ }
69+
70+ public static function jsonToHtml (object $ content , Option $ option ): string {
71+ if ($ content ->children != null ) {
72+ return Utils::nodeChildrenToHtml ($ content ->children , $ option );
73+ }
74+ return '' ;
75+ }
76+
77+ private static function nodeChildrenToHtml (array $ nodes , Option $ option ): string {
78+ return \implode ('' , \array_map (function (object $ node ) use ($ option ): string {
79+ return Utils::nodeToHtml ($ node , $ option );
80+ }, $ nodes ));
81+ }
82+
83+ private static function nodeToHtml (object $ node , Option $ option ): string {
84+ $ resultHtml = '' ;
85+ if ($ node ->type != null ) {
86+ switch ($ node ->type ) {
87+ case NodeType::get (NodeType::REFERENCE ):
88+ break ;
89+ default :
90+ break ;
91+ }
92+ } else {
93+ $ resultHtml = Utils::textToHtml ($ node , $ option );
94+ }
95+ return $ resultHtml ;
96+ }
97+
98+ private static function textToHtml (object $ node , Option $ option ) {
99+ $ text = $ node ->text ;
100+ if ($ node ->superscript ) {
101+ $ text = $ option ->renderMark (MarkType::get (MarkType::SUPERSCRIPT ), $ text );
102+ }
103+ if ($ node ->subscript ) {
104+ $ text = $ option ->renderMark (MarkType::get (MarkType::SUBSCRIPT ), $ text );
105+ }
106+ if ($ node ->inlineCode ) {
107+ $ text = $ option ->renderMark (MarkType::get (MarkType::INLINE_CODE ), $ text );
108+ }
109+ if ($ node ->strikethrough ) {
110+ $ text = $ option ->renderMark (MarkType::get (MarkType::STRIKE_THROUGH ), $ text );
111+ }
112+ if ($ node ->underline ) {
113+ $ text = $ option ->renderMark (MarkType::get (MarkType::UNDERLINE ), $ text );
114+ }
115+ if ($ node ->italic ) {
116+ $ text = $ option ->renderMark (MarkType::get (MarkType::ITALIC ), $ text );
117+ }
118+ if ($ node ->bold ) {
119+ $ text = $ option ->renderMark (MarkType::get (MarkType::BOLD ), $ text );
120+ }
121+ return $ text ;
122+ }
123+
60124 private static function findEmbeddedObject (\DOMDocument $ doc ): array {
61125 $ xpath = new \DOMXPath ($ doc );
62126 $ elements = $ xpath ->query ('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")] ' );
0 commit comments