Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup PHP with Xdebug 2.x
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.0'
coverage: xdebug2

- name: Validate composer.json and composer.lock
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## [1.1.0](https://github.com/contentstack/contentstack-utils-php/tree/v1.1.0) (2021-07-16)
- JSON RTE to html content functionality for GQL API added
## [1.0.1](https://github.com/contentstack/contentstack-utils-php/tree/v1.0.1) (2021-07-16)
- JSON RTE to html content functionality added

## [1.0.0](https://github.com/contentstack/contentstack-utils-php/tree/v1.0.0) (2021-04-05)
- Initial release for Contentstack Utils SDK
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,15 @@ for($i = 0; $i < count($result[0]); $i++) {
$render_rich_text = Contentstack::jsonToHtml($entry['rich_text_content'], new Option($entry));
}
```
#### GQL

To Convert JSON RTE content to HTML from GQL API. Use `GQL::jsonToHtml` function as shown below:

```php
use Contentstack\Utils\GQL;
use Contentstack\Utils\Model\Option;
// GQL fetch API
...
$render_html_text = GQL::jsonToHtml($entry->rich_text_content,, new Option());
...
```
99 changes: 99 additions & 0 deletions src/BaseParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Contentstack\Utils;

use Contentstack\Utils\Model\Option;
use Contentstack\Utils\Model\Metadata;
use Contentstack\Utils\Enum\NodeType;
use Contentstack\Utils\Enum\MarkType;

class BaseParser
{

protected static function nodeChildrenToHtml(array $nodes, Option $option, $renderEmbed): string {
return \implode('', \array_map(function (object $node) use ($option, $renderEmbed): string {
return BaseParser::nodeToHtml($node, $option, $renderEmbed);
}, $nodes));
}

protected static function nodeToHtml(object $node, Option $option, $renderEmbed): string {
$resultHtml = '';
if (isset($node->type)) {
switch ($node->type) {
case NodeType::get(NodeType::REFERENCE)->getValue():
$resultHtml = BaseParser::referenceToHtml($node, $renderEmbed);
break;
default:
$innerHtml = "";
if (isset($node->children))
{
$innerHtml = BaseParser::nodeChildrenToHtml($node->children, $option, $renderEmbed);
}
$resultHtml = $option->renderNode(
$node->type,
$node,
$innerHtml
);
break;
}
} else {
$resultHtml = BaseParser::textToHtml($node, $option);
}
return $resultHtml;
}

protected static function textToHtml(object $node, Option $option)
{
$text = $node->text;
if (isset($node->superscript) && $node->superscript) {
$text = $option->renderMark(MarkType::get(MarkType::SUPERSCRIPT), $text);
}
if (isset($node->subscript) && $node->subscript) {
$text = $option->renderMark(MarkType::get(MarkType::SUBSCRIPT), $text);
}
if (isset($node->inlineCode) && $node->inlineCode) {
$text = $option->renderMark(MarkType::get(MarkType::INLINE_CODE), $text);
}
if (isset($node->strikethrough) && $node->strikethrough) {
$text = $option->renderMark(MarkType::get(MarkType::STRIKE_THROUGH), $text);
}
if (isset($node->underline) && $node->underline) {
$text = $option->renderMark(MarkType::get(MarkType::UNDERLINE), $text);
}
if (isset($node->italic) && $node->italic) {
$text = $option->renderMark(MarkType::get(MarkType::ITALIC), $text);
}
if (isset($node->bold) && $node->bold) {
$text = $option->renderMark(MarkType::get(MarkType::BOLD), $text);
}
return $text;
}

protected static function referenceToHtml(object $node, $renderEmbed)
{
$metadata = new Metadata($node);
return $renderEmbed($metadata);
}

protected static function findEmbeddedObject(\DOMDocument $doc): array {
$xpath = new \DOMXPath($doc);
$elements = $xpath->query('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]');
$metadataArray = array();
foreach ($elements as $node) {
$metadataArray[] = new Metadata($node);
}
return $metadataArray;
}

static function innerHTML(\DOMElement $element)
{
$doc = $element->ownerDocument;
$html = '';
foreach ($element->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
}
}
57 changes: 57 additions & 0 deletions src/GQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Contentstack\Utils;

use Contentstack\Utils\Model\Option;
use Contentstack\Utils\Model\Metadata;
use Contentstack\Utils\Enum\NodeType;
use Contentstack\Utils\Enum\MarkType;

class GQL extends BaseParser
{
public static function jsonToHtml(object $content, Option $option): array|string {
$result = array();
$embeddedItems = $content->embedded_itemsConnection != null ? $content->embedded_itemsConnection->edges : [];

if (isset($content->json) && isset($content->json->children)) {
return GQL::nodeChildrenToHtml($content->json->children, $option, function (Metadata $metadata) use ($option, $embeddedItems): string {
$resultHtml = '';
$object = GQL::findObject($metadata, $embeddedItems);
if (count($object) > 0) {
$resultHtml = $option->renderOptions($object, $metadata);
}
return $resultHtml;
});
} else if (is_array($content->json)) {
foreach ($content->json as $node) {
$result[] = GQL::nodeChildrenToHtml($node->children, $option, function (Metadata $metadata) use ($option, $embeddedItems): string {
$resultHtml = '';
$object = GQL::findObject($metadata, $embeddedItems);
if (count($object) > 0) {
$resultHtml = $option->renderOptions($object, $metadata);
}
return $resultHtml;
});
}
}
return $result;
}
protected static function findObject(Metadata $metadata, array $embeddedItems): array
{

foreach ($embeddedItems as $entry)
{
if ($entry->node)
{
$item = $entry->node;
if ($item->system && $item->system->uid && $item->system->uid == $metadata->getItemUid())
{
return json_decode(json_encode($item), true);
}
}
}
return [];
}
}
4 changes: 2 additions & 2 deletions src/Model/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ function renderNode(string $nodeType, object $node, string $innerHtml): string
return $resultString;
}

function renderOptions(array $embeddedObject, Metadata $metadata): string
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"] ."</span></p></div>";
$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>";
Expand Down
105 changes: 11 additions & 94 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Contentstack\Utils\Enum\NodeType;
use Contentstack\Utils\Enum\MarkType;

class Utils
class Utils extends BaseParser
{
/**
*
Expand Down Expand Up @@ -70,94 +70,21 @@ public static function jsonArrayToHtml(array $contents, Option $option): array {
public static function jsonToHtml(object $content, Option $option): string {
$resultHtml = '';
if (isset($content->children)) {
$resultHtml = Utils::nodeChildrenToHtml($content->children, $option);
}
return $resultHtml;
}

private static function nodeChildrenToHtml(array $nodes, Option $option): string {
return \implode('', \array_map(function (object $node) use ($option): string {
return Utils::nodeToHtml($node, $option);
}, $nodes));
}

private static function nodeToHtml(object $node, Option $option): string {
$resultHtml = '';
if (isset($node->type)) {
switch ($node->type) {
case NodeType::get(NodeType::REFERENCE)->getValue():
$resultHtml = Utils::referenceToHtml($node, $option);
break;
default:
$innerHtml = "";
if (isset($node->children))
{
$innerHtml = Utils::nodeChildrenToHtml($node->children, $option);
$resultHtml = Utils::nodeChildrenToHtml($content->children, $option, function (Metadata $metadata) use ($option): string {
$resultHtml = '';
if ($option->entry) {
$object = Utils::findObject($metadata, $option->entry);
if (count($object) > 0) {
$resultHtml = $option->renderOptions($object, $metadata);
}
$resultHtml = $option->renderNode(
$node->type,
$node,
$innerHtml
);
break;
}
} else {
$resultHtml = Utils::textToHtml($node, $option);
}
return $resultHtml;
}

private static function textToHtml(object $node, Option $option)
{
$text = $node->text;
if (isset($node->superscript) && $node->superscript) {
$text = $option->renderMark(MarkType::get(MarkType::SUPERSCRIPT), $text);
}
if (isset($node->subscript) && $node->subscript) {
$text = $option->renderMark(MarkType::get(MarkType::SUBSCRIPT), $text);
}
if (isset($node->inlineCode) && $node->inlineCode) {
$text = $option->renderMark(MarkType::get(MarkType::INLINE_CODE), $text);
}
if (isset($node->strikethrough) && $node->strikethrough) {
$text = $option->renderMark(MarkType::get(MarkType::STRIKE_THROUGH), $text);
}
if (isset($node->underline) && $node->underline) {
$text = $option->renderMark(MarkType::get(MarkType::UNDERLINE), $text);
}
if (isset($node->italic) && $node->italic) {
$text = $option->renderMark(MarkType::get(MarkType::ITALIC), $text);
}
if (isset($node->bold) && $node->bold) {
$text = $option->renderMark(MarkType::get(MarkType::BOLD), $text);
}
return $text;
}

private static function referenceToHtml(object $node, Option $option)
{
$resultHtml = '';
if ($option->entry) {
$metadata = new Metadata($node);
$object = Utils::findObject($metadata, $option->entry);
if (count($object) > 0) {
$resultHtml = $option->renderOptions($object, $metadata);
}
}
return $resultHtml;
});
}
return $resultHtml;
}

private static function findEmbeddedObject(\DOMDocument $doc): array {
$xpath = new \DOMXPath($doc);
$elements = $xpath->query('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]');
$metadataArray = array();
foreach ($elements as $node) {
$metadataArray[] = new Metadata($node);
}
return $metadataArray;
}

private static function findObject(Metadata $metadata, array $entry): array
protected static function findObject(Metadata $metadata, array $entry): array
{
if (array_key_exists('_embedded_items', $entry))
{
Expand All @@ -174,14 +101,4 @@ private static function findObject(Metadata $metadata, array $entry): array
}
return [];
}

static function innerHTML(\DOMElement $element)
{
$doc = $element->ownerDocument;
$html = '';
foreach ($element->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
}
}
Loading