Skip to content

Commit 6e4b3c0

Browse files
committed
🚧 Json to Html
- Node to Metadata mapping - Reference Asset and Entry Embedded item completed
1 parent b252c93 commit 6e4b3c0

4 files changed

Lines changed: 125 additions & 21 deletions

File tree

src/Model/Metadata.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@
99

1010
class Metadata {
1111

12-
public function __construct(\DOMElement $element)
12+
public function __construct(object $element)
1313
{
14-
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
15-
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
16-
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
17-
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
18-
$this->text = $element->textContent;
19-
$this->attributes = $element->attributes;
20-
$this->element = $element;
14+
if ($element instanceof \DOMElement) {
15+
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
16+
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
17+
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
18+
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
19+
$this->text = $element->textContent;
20+
$this->attributes = $element->attributes;
21+
$this->element = $element;
22+
} else {
23+
$this->attributes = get_object_vars($element->attrs);
24+
$this->itemType = !empty($this->attributes->type)? EmbedItemType::byValue($this->attributes->type) : EmbedItemType::get(EmbedItemType::ENTRY);
25+
$this->styleType = !empty($this->attributes['display-type']) ? StyleType::byValue($this->attributes['display-type']) : StyleType::get(StyleType::BLOCK);
26+
$this->itemUid = !empty($this->attributes['entry-uid']) ? $this->attributes['entry-uid'] : $this->attributes['asset-uid'];
27+
$this->contentTypeUid = $this->attributes['content-type-uid'];
28+
$this->text = ($element->children && count($element->children) > 0) ? $element->children[0]->text : '';
29+
$this->element = $element;
30+
}
2131
}
2232

2333
/* Properties */
@@ -77,13 +87,18 @@ public function getContentTypeUid(): string
7787
return $this->contentTypeUid;
7888
}
7989

80-
public function getAttributes(): \DOMNamedNodeMap
90+
public function getAttributes(): object
8191
{
8292
return $this->attributes;
8393
}
8494

85-
public function getAttribute(string $name): string {
86-
return $this->element->getAttribute($name);
95+
public function getAttribute(string $name): ?string {
96+
if ($this->element instanceof \DOMElement) {
97+
return $this->element->getAttribute($name);
98+
} else if (isset($this->attributes[$name])) {
99+
return $this->attributes[$name];
100+
}
101+
return null;
87102
}
88103

89104
public function getOuterHTML(): string

src/Utils.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ private static function nodeChildrenToHtml(array $nodes, Option $option): string
8282

8383
private static function nodeToHtml(object $node, Option $option): string {
8484
$resultHtml = '';
85-
if ($node->type != null) {
85+
if (isset($node->type)) {
8686
switch ($node->type) {
87-
case NodeType::get(NodeType::REFERENCE):
87+
case NodeType::get(NodeType::REFERENCE)->getValue():
88+
$resultHtml = Utils::referenceToHtml($node, $option);
8889
break;
8990
default:
9091
break;
@@ -95,32 +96,46 @@ private static function nodeToHtml(object $node, Option $option): string {
9596
return $resultHtml;
9697
}
9798

98-
private static function textToHtml(object $node, Option $option) {
99+
private static function textToHtml(object $node, Option $option)
100+
{
99101
$text = $node->text;
100-
if ($node->superscript) {
102+
if (isset($node->superscript) && $node->superscript) {
101103
$text = $option->renderMark(MarkType::get(MarkType::SUPERSCRIPT), $text);
102104
}
103-
if ($node->subscript) {
105+
if (isset($node->subscript) && $node->subscript) {
104106
$text = $option->renderMark(MarkType::get(MarkType::SUBSCRIPT), $text);
105107
}
106-
if ($node->inlineCode) {
108+
if (isset($node->inlineCode) && $node->inlineCode) {
107109
$text = $option->renderMark(MarkType::get(MarkType::INLINE_CODE), $text);
108110
}
109-
if ($node->strikethrough) {
111+
if (isset($node->strikethrough) && $node->strikethrough) {
110112
$text = $option->renderMark(MarkType::get(MarkType::STRIKE_THROUGH), $text);
111113
}
112-
if ($node->underline) {
114+
if (isset($node->underline) && $node->underline) {
113115
$text = $option->renderMark(MarkType::get(MarkType::UNDERLINE), $text);
114116
}
115-
if ($node->italic) {
117+
if (isset($node->italic) && $node->italic) {
116118
$text = $option->renderMark(MarkType::get(MarkType::ITALIC), $text);
117119
}
118-
if ($node->bold) {
120+
if (isset($node->bold) && $node->bold) {
119121
$text = $option->renderMark(MarkType::get(MarkType::BOLD), $text);
120122
}
121123
return $text;
122124
}
123125

126+
private static function referenceToHtml(object $node, Option $option)
127+
{
128+
$resultHtml = '';
129+
if ($option->entry) {
130+
$metadata = new Metadata($node);
131+
$object = Utils::findObject($metadata, $option->entry);
132+
if (count($object) > 0) {
133+
$resultHtml = $option->renderOptions($object, $metadata);
134+
}
135+
}
136+
return $resultHtml;
137+
}
138+
124139
private static function findEmbeddedObject(\DOMDocument $doc): array {
125140
$xpath = new \DOMXPath($doc);
126141
$elements = $xpath->query('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]');

tests/Mock/JsonMock.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
define('CodeHtml', '<code>Code template.</code>');
1919
define('LinkInPHtml', '<p><strong><em><u><sub></sub></u></em></strong><a href="LINK.com">LINK</a></p>');
2020
define('EmbedHtml', '<iframe src="https://www.youtube.com/watch?v=AOP0yARiW8U"></iframe>');
21+
define('AssetReferenceHtml', '<img src="URL" alt="title" />');
22+
define('EntryReferenceBlockHtml', '<div><p>blttitleuid</p><p>Content type: <span>contentTypeUid</span></p></div>');
23+
define('EntryReferenceLinkHtml', '<a href="bltemmbedEntryuid">/copy-of-entry-final-02</a>');
24+
define('EntryReferenceInlineHtml', '<span>blttitleUpdateuid</span>');
2125

2226
define('BlankDocument', '{ "uid":"06e34a7a4e5d7fc2acd", "_version":13, "attrs":{ }, "children":[],"type":"doc"}');
2327
define('PlainTextJson', '{ "uid":"06e34a7a4e5d7fc2acd", "_version":13, "attrs":{ }, "children":[{"text":"Aliquam sit amet libero dapibus, eleifend ligula at, varius justo","bold":true},{ "text":"Lorem ipsum","bold":true,"italic":true},{ "text":"dolor sit amet","bold":true,"italic":true,"underline":true},{ "text":"consectetur adipiscing elit.","bold":true,"italic":true,"underline":true,"strikethrough":true},{ "text":"Sed condimentum iaculis magna in vehicula. ","bold":true,"italic":true,"underline":true,"inlineCode":true},{ "text":"Vestibulum vitae convallis ","bold":true,"italic":true,"underline":true,"superscript":true},{ "text":" lacus. ","bold":true,"italic":true,"underline":true,"subscript":true}],"type":"doc"}');

tests/UtilsJsonToHtmlTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,74 @@ public function testShouldReturnStringForPlainTextDocumentArray(): void
5151
$sss = Utils::jsonArrayToHtml([$jsonObject], new Option());
5252
$this->assertEquals([PlainTextHtml], $sss);
5353
}
54+
55+
public function testShouldReturnEmptyStringOnAssetReference(): void
56+
{
57+
$jsonObject = json_decode(AssetReferenceJson);
58+
$sss = Utils::jsonToHtml($jsonObject, new Option());
59+
$this->assertEquals('', $sss);
60+
}
61+
62+
public function testShouldReturnEmptyStringOnArrayAssetReference(): void
63+
{
64+
$jsonObject = json_decode(AssetReferenceJson);
65+
$sss = Utils::jsonArrayToHtml([$jsonObject], new Option());
66+
$this->assertEquals([''], $sss);
67+
}
68+
69+
public function testShouldReturnEmbeddedAssetOnAssetReference(): void
70+
{
71+
$jsonObject = json_decode(AssetReferenceJson);
72+
$sss = Utils::jsonToHtml($jsonObject, new Option(EmbedObjectMock::embeddedModel('', '', 'blt44asset')));
73+
$this->assertEquals(AssetReferenceHtml, $sss);
74+
}
75+
76+
public function testShouldReturnEmbeddedAssetOnArrayAssetReference(): void
77+
{
78+
$jsonObject = json_decode(AssetReferenceJson);
79+
$sss = Utils::jsonArrayToHtml([$jsonObject], new Option(EmbedObjectMock::embeddedModel('', '', 'blt44asset')));
80+
$this->assertEquals([AssetReferenceHtml], $sss);
81+
}
82+
83+
public function testShouldReturnEmbeddedEntryOnEntryBlockReference(): void
84+
{
85+
$jsonObject = json_decode(EntryReferenceBlockJson);
86+
$sss = Utils::jsonToHtml($jsonObject, new Option(EmbedObjectMock::embeddedModel('', 'blttitleuid', 'blt44asset')));
87+
$this->assertEquals(EntryReferenceBlockHtml, $sss);
88+
}
89+
90+
public function testShouldReturnEmbeddedEntryOnArrayEntryBlockReference(): void
91+
{
92+
$jsonObject = json_decode(EntryReferenceBlockJson);
93+
$sss = Utils::jsonArrayToHtml([$jsonObject], new Option(EmbedObjectMock::embeddedModel('', 'blttitleuid', 'blt44asset')));
94+
$this->assertEquals([EntryReferenceBlockHtml], $sss);
95+
}
96+
97+
public function testShouldReturnEmbeddedEntryOnEntryLinkReference(): void
98+
{
99+
$jsonObject = json_decode(EntryReferenceLinkJson);
100+
$sss = Utils::jsonToHtml($jsonObject, new Option(EmbedObjectMock::embeddedModel('', 'bltemmbedEntryuid', 'blt44asset')));
101+
$this->assertEquals(EntryReferenceLinkHtml, $sss);
102+
}
103+
104+
public function testShouldReturnEmbeddedEntryOnArrayEntryLinkReference(): void
105+
{
106+
$jsonObject = json_decode(EntryReferenceLinkJson);
107+
$sss = Utils::jsonArrayToHtml([$jsonObject], new Option(EmbedObjectMock::embeddedModel('', 'bltemmbedEntryuid', 'blt44asset')));
108+
$this->assertEquals([EntryReferenceLinkHtml], $sss);
109+
}
110+
111+
public function testShouldReturnEmbeddedEntryOnEntryInlineReference(): void
112+
{
113+
$jsonObject = json_decode(EntryReferenceInlineJson);
114+
$sss = Utils::jsonToHtml($jsonObject, new Option(EmbedObjectMock::embeddedModel('', 'blttitleUpdateuid', 'blt44asset')));
115+
$this->assertEquals(EntryReferenceInlineHtml, $sss);
116+
}
117+
118+
public function testShouldReturnEmbeddedEntryOnArrayEntryInlineReference(): void
119+
{
120+
$jsonObject = json_decode(EntryReferenceInlineJson);
121+
$sss = Utils::jsonArrayToHtml([$jsonObject], new Option(EmbedObjectMock::embeddedModel('', 'blttitleUpdateuid', 'blt44asset')));
122+
$this->assertEquals([EntryReferenceInlineHtml], $sss);
123+
}
54124
}

0 commit comments

Comments
 (0)