Skip to content

Commit b252c93

Browse files
committed
🚧 Json To Html
- Mark Type Defined - Node Type Defined - Render Mark Type implemented - JsonToHtml String render function added - JsonArrayToHtml Json Array render Function added - Mark Type test case completed
1 parent 68a89bd commit b252c93

8 files changed

Lines changed: 284 additions & 2 deletions

File tree

src/Enum/MarkType.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contentstack\Utils\Enum;
6+
7+
use MabeEnum\Enum;
8+
9+
class MarkType extends Enum
10+
{
11+
const BOLD = 'bold';
12+
const ITALIC = 'italic';
13+
const UNDERLINE = 'underline';
14+
15+
const STRIKE_THROUGH = 'strikethrough';
16+
const INLINE_CODE = 'inlineCode';
17+
18+
const SUBSCRIPT = 'subscript';
19+
const SUPERSCRIPT = 'superscript';
20+
}

src/Enum/NodeType.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contentstack\Utils\Enum;
6+
7+
use MabeEnum\Enum;
8+
9+
class NodeType extends Enum
10+
{
11+
const DOCUMENT = 'doc';
12+
const PARAGRAPH = 'p';
13+
14+
const LINK = 'a';
15+
const IMAGE = 'img';
16+
const EMBED = 'embed';
17+
18+
const HEADING_1 = 'h1';
19+
const HEADING_2 = 'h2';
20+
const HEADING_3 = 'h3';
21+
const HEADING_4 = 'h4';
22+
const HEADING_5 = 'h5';
23+
const HEADING_6 = 'h6';
24+
25+
const ORDER_LIST = 'ol';
26+
const UNORDER_LIST = 'ul';
27+
const LIST_ITEM = 'li';
28+
29+
const HR = 'hr';
30+
31+
const TABLE = 'table';
32+
const TABLE_HEADER = 'thead';
33+
const TABLE_BODY = 'tbody';
34+
const TABLE_FOOTER = 'tfoot';
35+
const TABLE_ROW = 'tr';
36+
const TABLE_HEAD = 'th';
37+
const TABLE_DATA = 'td';
38+
39+
const BLOCK_QUOTE = 'blockquote';
40+
const CODE = 'code';
41+
42+
const TEXT = 'text';
43+
const REFERENCE = 'reference';
44+
}

src/Model/Option.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Contentstack\Utils\Resource\RenderableInterface;
99
use Contentstack\Utils\Resource\EmbeddedObject;
1010
use Contentstack\Utils\Enum\StyleType;
11+
use Contentstack\Utils\Enum\MarkType;
1112

1213
class Option implements RenderableInterface {
1314

@@ -17,10 +18,40 @@ class Option implements RenderableInterface {
1718

1819
public $entry;
1920

20-
public function __construct(array $entry)
21+
public function __construct(array $entry = null)
2122
{
2223
$this->entry = $entry;
2324
}
25+
26+
function renderMark(MarkType $markType, string $text): string
27+
{
28+
$resultString = "";
29+
30+
switch ($markType) {
31+
case MarkType::get(MarkType::BOLD):
32+
$resultString = "<strong>".$text."</strong>";
33+
break;
34+
case MarkType::get(MarkType::ITALIC):
35+
$resultString = "<em>".$text."</em>";
36+
break;
37+
case MarkType::get(MarkType::UNDERLINE):
38+
$resultString = "<u>".$text."</u>";
39+
break;
40+
case MarkType::get(MarkType::STRIKE_THROUGH):
41+
$resultString = "<strike>".$text."</strike>";
42+
break;
43+
case MarkType::get(MarkType::INLINE_CODE):
44+
$resultString = "<span>".$text."</span>";
45+
break;
46+
case MarkType::get(MarkType::SUBSCRIPT):
47+
$resultString = "<sub>".$text."</sub>";
48+
break;
49+
case MarkType::get(MarkType::SUPERSCRIPT):
50+
$resultString = "<sup>".$text."</sup>";
51+
break;
52+
}
53+
return $resultString;
54+
}
2455

2556
function renderOptions(array $embeddedObject, Metadata $metadata): string
2657
{

src/Resource/RenderableInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Contentstack\Utils\Enum\EmbedItemType;
77
use Contentstack\Utils\Model\Metadata;
8+
use Contentstack\Utils\Enum\MarkType;
89

910
interface RenderableInterface
1011
{
@@ -13,4 +14,10 @@ interface RenderableInterface
1314
* @param $metadata - Tag details and attributes
1415
*/
1516
function renderOptions(array $embeddedObject, Metadata $metadata): string;
17+
18+
/**
19+
* @param $markType - MarkType for the text content
20+
* @param $text - Text content for rendering
21+
*/
22+
function renderMark(MarkType $markType, string $text): string;
1623
}

src/Utils.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Contentstack\Utils\Model\Option;
88
use Contentstack\Utils\Model\Metadata;
9+
use Contentstack\Utils\Enum\NodeType;
10+
use Contentstack\Utils\Enum\MarkType;
911

1012
class 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")]');

tests/DefaultOptionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Contentstack\Utils\Utils;
1111
use Contentstack\Utils\Enum\EmbedItemType;
1212
use Contentstack\Utils\Enum\StyleType;
13+
use Contentstack\Utils\Enum\MarkType;
1314
use Contentstack\Utils\Model\Metadata;
1415
use Contentstack\Utils\Model\Option;
1516
use PHPUnit\Framework\TestCase;
@@ -102,4 +103,22 @@ public function testEmbeddedAssetWithText(): void
102103
$resultString = DefaultOptionTest::$defaultRender->renderOptions(DefaultOptionTest::$embeddedAsset, $this->getMetadata(EmbedItemType::ASSET, StyleType::DOWNLOAD, DefaultOptionTest::$text));
103104
$this->assertEquals('<a href="URL">Text To set Link</a>', $resultString);
104105
}
106+
107+
public function testShouldReturnMarkTypeHtmlContent(): void
108+
{
109+
$boldString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::BOLD), DefaultOptionTest::$text);
110+
$this->assertEquals("<strong>".DefaultOptionTest::$text."</strong>", $boldString);
111+
$italicString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::ITALIC), DefaultOptionTest::$text);
112+
$this->assertEquals("<em>".DefaultOptionTest::$text."</em>", $italicString);
113+
$underlineString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::UNDERLINE), DefaultOptionTest::$text);
114+
$this->assertEquals("<u>".DefaultOptionTest::$text."</u>", $underlineString);
115+
$strickthroughString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::STRIKE_THROUGH), DefaultOptionTest::$text);
116+
$this->assertEquals("<strike>".DefaultOptionTest::$text."</strike>", $strickthroughString);
117+
$inlineCodeString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::INLINE_CODE), DefaultOptionTest::$text);
118+
$this->assertEquals("<span>".DefaultOptionTest::$text."</span>", $inlineCodeString);
119+
$subscriptString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::SUBSCRIPT), DefaultOptionTest::$text);
120+
$this->assertEquals("<sub>".DefaultOptionTest::$text."</sub>", $subscriptString);
121+
$superscriptString = DefaultOptionTest::$defaultRender->renderMark(MarkType::get(MarkType::SUPERSCRIPT), DefaultOptionTest::$text);
122+
$this->assertEquals("<sup>".DefaultOptionTest::$text."</sup>", $superscriptString);
123+
}
105124
}

0 commit comments

Comments
 (0)