daux.io/libs/Tree/Content.php

154 lines
3.4 KiB
PHP
Raw Normal View History

<?php namespace Todaymade\Daux\Tree;
use RuntimeException;
use Webuni\FrontMatter\FrontMatter;
2016-07-29 22:38:03 +02:00
class Content extends ContentAbstract
2015-04-23 00:32:30 +02:00
{
2015-11-11 00:09:47 +01:00
/** @var string */
2015-07-17 23:38:06 +02:00
protected $content;
2015-11-11 00:09:47 +01:00
/** @var Content */
protected $previous;
2015-11-11 00:09:47 +01:00
/** @var Content */
protected $next;
2015-11-11 00:09:47 +01:00
/** @var array */
protected $attributes;
/** @var bool */
protected $manuallySetContent = false;
protected function getFrontMatter()
2015-07-17 23:38:06 +02:00
{
if ($this->manuallySetContent) {
$content = $this->content;
2020-04-22 21:55:53 +02:00
} elseif (!$this->getPath()) {
2020-04-22 22:24:52 +02:00
throw new RuntimeException('Empty content');
} else {
$content = file_get_contents($this->getPath());
2015-07-17 23:38:06 +02:00
}
$frontMatter = new FrontMatter();
// Remove BOM if it's present
if (substr($content, 0, 3) == "\xef\xbb\xbf") {
$content = substr($content, 3);
2015-11-11 00:09:47 +01:00
}
return $frontMatter->parse($content);
}
public function getContent(): string
{
if ($this->attributes === null) {
$this->parseAttributes();
}
return $this->content;
2015-07-17 23:38:06 +02:00
}
public function setContent(string $content): void
2015-07-17 23:38:06 +02:00
{
$this->manuallySetContent = true;
2015-07-17 23:38:06 +02:00
$this->content = $content;
}
/**
* @return Content
*/
public function getPrevious(): ?Content
{
return $this->previous;
}
public function setPrevious(Content $previous)
{
$this->previous = $previous;
}
/**
* @return Content
*/
public function getNext(): ?Content
{
return $this->next;
}
public function setNext(Content $next)
{
$this->next = $next;
}
public function isIndex()
{
2016-01-31 11:59:35 +01:00
// At some point, it was recommended that
// an index page starts with an underscore.
// This is not mandatory anymore, both with
// and without underscore are supported.
return $this->name == 'index' || $this->name == '_index';
}
public function getTitle(): string
2015-11-11 00:09:47 +01:00
{
if ($title = $this->getAttribute('title')) {
return $title;
}
return parent::getTitle();
}
protected function parseAttributes()
{
// We set an empty array first to
// avoid a loop when "parseAttributes"
// is called in "getContent"
$this->attributes = [];
$document = $this->getFrontMatter();
$this->attributes = array_replace_recursive($this->attributes, $document->getData());
2018-06-06 23:37:26 +02:00
$this->content = $document->getContent();
2015-11-11 00:09:47 +01:00
}
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
}
2017-06-07 23:40:12 +02:00
/**
2020-04-22 22:24:52 +02:00
* Get one or all attributes for the content.
*
* @param null|string $key
2017-06-07 23:40:12 +02:00
*
2020-04-22 22:24:52 +02:00
* @return null|array|mixed
2017-06-07 23:40:12 +02:00
*/
2015-11-11 00:09:47 +01:00
public function getAttribute($key = null)
{
if ($this->attributes === null) {
$this->parseAttributes();
}
if (is_null($key)) {
return $this->attributes;
}
if (!array_key_exists($key, $this->attributes)) {
return null;
}
return $this->attributes[$key];
}
public function dump()
{
$dump = parent::dump();
$dump['prev'] = $this->getPrevious() ? $this->getPrevious()->getUrl() : '';
$dump['next'] = $this->getNext() ? $this->getNext()->getUrl() : '';
return $dump;
}
}