ParsedMarkdown.php in Markdown 8.2
File
src/Render/ParsedMarkdown.php
View source
<?php
namespace Drupal\markdown\Render;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Language\LanguageInterface;
class ParsedMarkdown implements ParsedMarkdownInterface {
use RefinableCacheableDependencyTrait;
protected $expire = ParsedMarkdownInterface::PERMANENT;
protected $html;
protected $id;
protected $label;
protected $markdown;
protected $language;
protected $size;
public function __construct($markdown = '', $html = '', LanguageInterface $language = NULL) {
$this->html = trim($html);
$this->markdown = trim($markdown);
$this->language = $language;
}
public function __toString() {
return $this
->getHtml();
}
public static function create($markdown = '', $html = '', LanguageInterface $language = NULL) {
return new static($markdown, $html, $language);
}
public function count() {
return $this
->getSize();
}
public function getExpire($from_time = NULL) {
$expire = $this->expire;
if (is_string($expire)) {
$expire = strtotime($expire, $from_time ?: REQUEST_TIME);
}
return $expire;
}
public function getHtml() {
return $this->html;
}
public function getId() {
if ($this->id === NULL) {
$this->id = Crypt::hashBase64($this
->getMarkdown() . $this
->getHtml());
}
return $this->id;
}
public function getLabel() {
return $this->label ?: $this
->getId();
}
public function getMarkdown() {
return static::normalizeMarkdown($this->markdown);
}
public function getSize($formatted = FALSE, $decimals = 2) {
if ($this->size === NULL) {
$this->size = Unicode::strlen($this
->getHtml());
}
return $formatted ? number_format($this->size, $decimals) : $this->size;
}
public function jsonSerialize() {
return $this
->__toString();
}
public function matches($markdown) {
if ($markdown instanceof static) {
return $markdown
->getMarkdown() === $this
->getMarkdown();
}
return static::normalizeMarkdown($markdown) === $this
->getMarkdown();
}
public static function normalizeMarkdown($markdown) {
return $markdown === '' ? '' : preg_replace('/\\r\\n|\\n/', "\n", (string) $markdown);
}
public function serialize() {
$data['object'] = serialize(get_object_vars($this));
$data['gzip'] = extension_loaded('zlib');
if ($data['gzip']) {
$data['object'] = base64_encode(gzencode($data['object'], 9));
}
return serialize($data);
}
public function setExpire($expire = ParsedMarkdownInterface::PERMANENT) {
$this->expire = $expire;
return $this;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function unserialize($serialized) {
$data = unserialize($serialized);
if ($data['gzip']) {
if (extension_loaded('zlib')) {
$data['object'] = gzdecode(base64_decode($data['object']));
}
else {
$this->markdown = sprintf('This cached %s object was stored using gzip compression. Unable to decompress. The PHP on this server must have the "zlib" extension installed.', static::class);
$this->html = $this->markdown;
return;
}
}
$object = unserialize($data['object']);
foreach ($object as $prop => $value) {
$this->{$prop} = $value;
}
}
}