-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOption.php
More file actions
178 lines (167 loc) · 7.74 KB
/
Option.php
File metadata and controls
178 lines (167 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace Contentstack\Utils\Model;
use Contentstack\Utils\Resource\EntryEmbedable;
use Contentstack\Utils\Resource\RenderableInterface;
use Contentstack\Utils\Resource\EmbeddedObject;
use Contentstack\Utils\Enum\StyleType;
use Contentstack\Utils\Enum\MarkType;
use Contentstack\Utils\Enum\NodeType;
class Option implements RenderableInterface {
/**
* @var EntryEmbedable
*/
public $entry;
public function __construct(array $entry = null)
{
$this->entry = $entry;
}
function renderMark(MarkType $markType, string $text): string
{
$resultString = "";
switch ($markType) {
case MarkType::get(MarkType::BOLD):
$resultString = "<strong>".$text."</strong>";
break;
case MarkType::get(MarkType::ITALIC):
$resultString = "<em>".$text."</em>";
break;
case MarkType::get(MarkType::UNDERLINE):
$resultString = "<u>".$text."</u>";
break;
case MarkType::get(MarkType::STRIKE_THROUGH):
$resultString = "<strike>".$text."</strike>";
break;
case MarkType::get(MarkType::INLINE_CODE):
$resultString = "<span>".$text."</span>";
break;
case MarkType::get(MarkType::SUBSCRIPT):
$resultString = "<sub>".$text."</sub>";
break;
case MarkType::get(MarkType::SUPERSCRIPT):
$resultString = "<sup>".$text."</sup>";
break;
case MarkType::get(MarkType::BREAK):
$resultString = "<br />";
break;
}
return $resultString;
}
function renderNode(string $nodeType, object $node, string $innerHtml): string
{
$resultString = "";
if (property_exists($node, 'attrs')) {
$attrs = get_object_vars((object)$node->attrs);
} else {
$attrs = array();
}
switch ($nodeType)
{
case NodeType::get(NodeType::PARAGRAPH)->getValue():
$resultString = "<p>".$innerHtml."</p>";
break;
case NodeType::get(NodeType::LINK)->getValue():
$resultString = "<a href=\"".($attrs["href"] ?? "")."\">".$innerHtml."</a>";
break;
case NodeType::get(NodeType::IMAGE)->getValue():
$resultString = "<img src=\"".($attrs["src"] ?? "")."\" />".$innerHtml;
break;
case NodeType::get(NodeType::EMBED)->getValue():
$resultString = "<iframe src=\"".($attrs["src"] ?? "")."\">".$innerHtml."</iframe>";
break;
case NodeType::get(NodeType::HEADING_1)->getValue():
$resultString = "<h1>".$innerHtml."</h1>";
break;
case NodeType::get(NodeType::HEADING_2)->getValue():
$resultString = "<h2>".$innerHtml."</h2>";
break;
case NodeType::get(NodeType::HEADING_3)->getValue():
$resultString = "<h3>".$innerHtml."</h3>";
break;
case NodeType::get(NodeType::HEADING_4)->getValue():
$resultString = "<h4>".$innerHtml."</h4>";
break;
case NodeType::get(NodeType::HEADING_5)->getValue():
$resultString = "<h5>".$innerHtml."</h5>";
break;
case NodeType::get(NodeType::HEADING_6)->getValue():
$resultString = "<h6>".$innerHtml."</h6>";
break;
case NodeType::get(NodeType::ORDER_LIST)->getValue():
$resultString = "<ol>".$innerHtml."</ol>";
break;
case NodeType::get(NodeType::UNORDER_LIST)->getValue():
$resultString = "<ul>".$innerHtml."</ul>";
break;
case NodeType::get(NodeType::LIST_ITEM)->getValue():
$resultString = "<li>".$innerHtml."</li>";
break;
case NodeType::get(NodeType::HR)->getValue():
$resultString = "<hr>";
break;
case NodeType::get(NodeType::TABLE)->getValue():
$resultString = "<table>".$innerHtml."</table>";
break;
case NodeType::get(NodeType::TABLE_HEADER)->getValue():
$resultString = "<thead>".$innerHtml."</thead>";
break;
case NodeType::get(NodeType::TABLE_BODY)->getValue():
$resultString = "<tbody>".$innerHtml."</tbody>";
break;
case NodeType::get(NodeType::TABLE_FOOTER)->getValue():
$resultString = "<tfoot>".$innerHtml."</tfoot>";
break;
case NodeType::get(NodeType::TABLE_ROW)->getValue():
$resultString = "<tr>".$innerHtml."</tr>";
break;
case NodeType::get(NodeType::TABLE_HEAD)->getValue():
$resultString = "<th>".$innerHtml."</th>";
break;
case NodeType::get(NodeType::TABLE_DATA)->getValue():
$resultString = "<td>".$innerHtml."</td>";
break;
case NodeType::get(NodeType::BLOCK_QUOTE)->getValue():
$resultString = "<blockquote>".$innerHtml."</blockquote>";
break;
case NodeType::get(NodeType::CODE)->getValue():
$resultString = "<code>".$innerHtml."</code>";
break;
case NodeType::get(NodeType::FRAGMENT)->getValue():
$resultString = "<fragment>".$innerHtml."</fragment>";
break;
case NodeType::get(NodeType::REFERENCE)->getValue():
if(isset($attrs["type"]) && $attrs["type"] == "asset"){
$resultString = "<img src=".$attrs['attrs']['asset-link']." alt=".$attrs['attrs']['asset-name']."/>";
}else{
$resultString = $innerHtml;
}
break;
default:
$resultString = $innerHtml;
break;
}
return $resultString;
}
function renderOptions(array $embeddedObject, Metadata $metadata): string
{
$resultString = "";
switch ($metadata->getStyleType()) {
case StyleType::get(StyleType::BLOCK):
$resultString = "<div><p>" . ($embeddedObject["title"] ?? $embeddedObject["uid"]) . "</p><p>Content type: <span>". ($embeddedObject["_content_type_uid"] ?? $embeddedObject["system"]["content_type_uid"]) ."</span></p></div>";
break;
case StyleType::get(StyleType::INLINE):
$resultString = "<span>".($embeddedObject["title"] ?? $embeddedObject["uid"])."</span>";
break;
case StyleType::get(StyleType::LINK):
$resultString = "<a href=\"".($metadata->getAttribute("href")->value ?? $embeddedObject["url"] ?? $embeddedObject["title"] ?? $embeddedObject["uid"] )."\">".($metadata->getText() ?? $embeddedObject["title"] ?? $embeddedObject["uid"])."</a>";
break;
case StyleType::get(StyleType::DISPLAY):
$resultString = "<img src=\"".($metadata->getAttribute("src")->value ?? $embeddedObject["url"] )."\" alt=\"".($metadata->getAttribute("alt")->value ?? $embeddedObject["title"] ?? $embeddedObject["filename"] ?? $embeddedObject["uid"])."\" />";
break;
case StyleType::get(StyleType::DOWNLOAD):
$resultString = "<a href=\"".($metadata->getAttribute("href")->value ?? $embeddedObject["url"])."\">".($metadata->getText() ?? $embeddedObject["filename"]?? $embeddedObject["title"] ?? $embeddedObject["uid"])."</a>";
break;
}
return $resultString;
}
}