src/Models/Dom/RichText.php
private static function serialize(\stdClass $element, $content, $linkResolver)
    {

        $classCode = '';
        if (isset($element->label)) {
            $classCode = ' class="' . $element->label . '"';
        }
        // Blocks
        switch ($element->type) {
            case 'heading1':
                return nl2br('<h1' . $classCode . '>' . $content . '</h1>');
            case 'heading2':
                return nl2br('<h2' . $classCode . '>' . $content . '</h2>');
            case 'heading3':
                return nl2br('<h3' . $classCode . '>' . $content . '</h3>');
            case 'heading4':
                return nl2br('<h4' . $classCode . '>' . $content . '</h4>');
            case 'heading5':
                return nl2br('<h5' . $classCode . '>' . $content . '</h5>');
            case 'heading6':
                return nl2br('<h6' . $classCode . '>' . $content . '</h6>');
            case 'paragraph':
                return nl2br('<p' . $classCode . '>' . $content . '</p>');
            case 'preformatted':
                return '<pre' . $classCode . '>' . $content . '</pre>';
            case 'list-item':
            case 'o-list-item':
                return nl2br('<li' . $classCode . '>' . $content . '</li>');
            case 'image':
                return (
                    '<p class="block-img' . (isset($element->label) ? (' ' . $element->label) : '') . '">' .
                    '<img src="' . $element->url . '" alt="' . htmlentities($element->alt) . '">' .
                    '</p>'
                );
            case 'embed':
                $providerAttr = '';
                $element->oembed = (object) $element->oembed;
                if ($element->oembed->provider_name) {
                    $providerAttr = ' data-oembed-provider="' . strtolower($element->oembed->provider_name) . '"';
                }
                if ($element->oembed->html) {
                    return (
                        '<div data-oembed="' . $element->oembed->embed_url . '" data-oembed-type="' . strtolower($element->oembed->type) . '"' . $providerAttr . '>' .
                        $element->oembed->html .
                        '</div>'
                    );
                }
                return '';
        }
        // Spans
        $attributes = array();
        switch ($element->type) {
            case 'strong':
                $nodeName = 'strong';
                break;
            case 'em':
                $nodeName = 'em';
                break;
            case 'hyperlink':
                $nodeName = 'a';
                $element->data = (object) $element->data;
                if (isset($element->data->target)) {
                    $attributes = array_merge(array(
                        'target' => $element->data->target,
                        'rel' => 'noopener',
                    ), $attributes);
                }
                if ($element->data->link_type === 'Document') {
                    $attributes['href'] = $linkResolver ? $linkResolver($element->data) : '';
                } else {
                    $attributes['href'] = $element->data->url;
                }
                if ($attributes['href'] === null) {
                    // We have no link (LinkResolver said it is not valid,
                    // or something else went wrong). Abort this span.
                    return $content;
                }
                break;
            default:
                // throw new \Exception("Unknown span type " . get_class($span));
                $nodeName = 'span';
        }
        if (isset($element->label)) {
            $attributes['class'] = $element->label;
        } elseif (isset($element->data->label)) {
            $attributes['class'] = $element->data->label;
        }
        $html = '<' . $nodeName;
        foreach ($attributes as $k => $v) {
            $html .= (' ' . $k . '="' . $v . '"');
        }
        $html .= ('>' . $content . '</' . $nodeName . '>');
        return $html;
    }
private static function serialize(\stdClass $element, $content, $linkResolver)
    {

        $classCode = '';
        if (isset($element->label)) {
            $classCode = ' class="' . $element->label . '"';
        }
        // Blocks
        switch ($element->type) {
            case 'heading1':
                return nl2br('<h1' . $classCode . '>' . $content . '</h1>');
            case 'heading2':
                return nl2br('<h2' . $classCode . '>' . $content . '</h2>');
            case 'heading3':
                return nl2br('<h3' . $classCode . '>' . $content . '</h3>');
            case 'heading4':
                return nl2br('<h4' . $classCode . '>' . $content . '</h4>');
            case 'heading5':
                return nl2br('<h5' . $classCode . '>' . $content . '</h5>');
            case 'heading6':
                return nl2br('<h6' . $classCode . '>' . $content . '</h6>');
            case 'paragraph':
                return nl2br('<p' . $classCode . '>' . $content . '</p>');
            case 'preformatted':
                return '<pre' . $classCode . '>' . $content . '</pre>';
            case 'list-item':
            case 'o-list-item':
                return nl2br('<li' . $classCode . '>' . $content . '</li>');
            case 'image':
                return (
                    '<p class="block-img' . (isset($element->label) ? (' ' . $element->label) : '') . '">' .
                    '<img src="' . $element->url . '" alt="' . htmlentities($element->alt) . '">' .
                    '</p>'
                );
            case 'embed':
                $providerAttr = '';
                $element->oembed = (object) $element->oembed;
                if ($element->oembed->provider_name) {
                    $providerAttr = ' data-oembed-provider="' . strtolower($element->oembed->provider_name) . '"';
                }
                if ($element->oembed->html) {
                    return (
                        '<div data-oembed="' . $element->oembed->embed_url . '" data-oembed-type="' . strtolower($element->oembed->type) . '"' . $providerAttr . '>' .
                        $element->oembed->html .
                        '</div>'
                    );
                }
                return '';
        }
        // Spans
        $attributes = array();
        switch ($element->type) {
            case 'strong':
                $nodeName = 'strong';
                break;
            case 'em':
                $nodeName = 'em';
                break;
            case 'hyperlink':
                $nodeName = 'a';
                $element->data = (object) $element->data;
                if (isset($element->data->target)) {
                    $attributes = array_merge(array(
                        'target' => $element->data->target,
                        'rel' => 'noopener',
                    ), $attributes);
                }
                if ($element->data->link_type === 'Document') {
                    $attributes['href'] = $linkResolver ? $linkResolver($element->data) : '';
                } else {
                    $attributes['href'] = $element->data->url;
                }
                if ($attributes['href'] === null) {
                    // We have no link (LinkResolver said it is not valid,
                    // or something else went wrong). Abort this span.
                    return $content;
                }
                break;
            default:
                // throw new \Exception("Unknown span type " . get_class($span));
                $nodeName = 'span';
        }
        if (isset($element->label)) {
            $attributes['class'] = $element->label;
        } elseif (isset($element->data->label)) {
            $attributes['class'] = $element->data->label;
        }
        $html = '<' . $nodeName;
        foreach ($attributes as $k => $v) {
            $html .= (' ' . $k . '="' . $v . '"');
        }
        $html .= ('>' . $content . '</' . $nodeName . '>');
        return $html;
    }
Transforms an element into HTML


@param \stdClass $element element to serialize
@param string $content inner HTML content of the element
@param LinkResolver $linkResolver   the link resolver

@return string the HTML representation of the element