daux.io/libs/Tree/Entry.php

240 lines
4.7 KiB
PHP
Raw Normal View History

<?php namespace Todaymade\Daux\Tree;
2015-04-23 00:32:30 +02:00
abstract class Entry
{
2015-07-17 23:38:06 +02:00
/** @var string */
2015-04-23 00:32:30 +02:00
protected $title;
2015-07-17 23:38:06 +02:00
/** @var string */
2015-04-23 00:32:30 +02:00
protected $name;
2015-07-17 23:38:06 +02:00
/** @var Content */
2015-04-23 00:32:30 +02:00
protected $index_page;
2015-07-17 23:38:06 +02:00
/** @var Content */
2015-04-23 00:32:30 +02:00
protected $first_page;
2015-07-17 23:38:06 +02:00
/** @var string */
2015-04-23 00:32:30 +02:00
protected $uri;
2015-07-17 23:38:06 +02:00
/** @var Directory */
protected $parent;
2015-07-17 23:38:06 +02:00
/** @var string */
protected $path;
2015-07-17 23:38:06 +02:00
/** @var integer */
2015-04-23 00:32:30 +02:00
protected $last_modified;
2015-07-17 23:38:06 +02:00
/**
* @param Directory $parent
* @param string $uri
2015-07-17 23:38:06 +02:00
* @param string $path
* @param integer $last_modified
2015-07-17 23:38:06 +02:00
*/
public function __construct(Directory $parent, $uri, $path = null, $last_modified = null)
2015-04-23 00:32:30 +02:00
{
$this->setUri($uri);
$this->setParent($parent);
if ($path) {
$this->path = $path;
}
if ($last_modified) {
$this->last_modified = $last_modified;
}
2015-04-23 00:32:30 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* @return string
*/
2015-04-23 00:32:30 +02:00
public function getName()
{
return $this->name;
}
2015-07-17 23:38:06 +02:00
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
2015-04-23 00:32:30 +02:00
public function getUri()
{
return $this->uri;
}
2015-07-17 23:38:06 +02:00
/**
* @param string $uri
*/
2015-07-16 11:08:16 +02:00
public function setUri($uri)
2015-04-23 00:32:30 +02:00
{
if ($this->parent) {
$this->parent->removeChild($this);
}
2015-07-16 11:08:16 +02:00
$this->uri = $uri;
if ($this->parent) {
$this->parent->addChild($this);
}
2015-04-23 00:32:30 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* @return Content
*/
2015-04-23 00:32:30 +02:00
public function getIndexPage()
{
return $this->index_page;
}
2015-07-17 23:38:06 +02:00
/**
* @param Content $index_page
*/
2015-04-23 00:32:30 +02:00
public function setIndexPage($index_page)
{
$this->index_page = $index_page;
}
/**
2015-07-18 01:14:44 +02:00
* @return Content|false
*/
2015-04-23 00:32:30 +02:00
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
2015-07-18 01:14:44 +02:00
if (!$this instanceof Directory) {
return false;
}
2015-07-17 23:38:06 +02:00
// First we try to find a real page
foreach ($this->getEntries() as $node) {
2015-07-18 01:14:44 +02:00
if ($node instanceof Content) {
if (!$node->getParent() && $node->title == 'index') {
2015-07-18 01:14:44 +02:00
//the homepage should not count as first page
continue;
}
2015-07-18 01:14:44 +02:00
$this->setFirstPage($node);
2015-07-18 01:14:44 +02:00
return $node;
}
2015-07-18 01:14:44 +02:00
}
// If we can't find one we check in the sub-directories
foreach ($this->getEntries() as $node) {
2015-07-18 01:14:44 +02:00
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->setFirstPage($page);
2015-07-18 01:14:44 +02:00
return $page;
}
}
2015-07-18 01:14:44 +02:00
2015-04-23 00:32:30 +02:00
return false;
}
2015-07-17 23:38:06 +02:00
/**
* @param Content $first_page
*/
2015-04-23 00:32:30 +02:00
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
2015-07-17 23:38:06 +02:00
/**
* @return string
*/
2015-04-23 00:32:30 +02:00
public function getTitle()
{
return $this->title;
}
2015-07-17 23:38:06 +02:00
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return Directory
2015-07-17 23:38:06 +02:00
*/
public function getParent()
2015-04-23 00:32:30 +02:00
{
return $this->parent;
2015-04-23 00:32:30 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* Return all parents starting with the root
*
* @return array<Directory>
2015-07-17 23:38:06 +02:00
*/
public function getParents()
2015-07-17 23:38:06 +02:00
{
$parents = [];
if ($this->parent && !$this->parent instanceof Root) {
$parents = $this->parent->getParents();
$parents[] = $this->parent;
}
2015-07-17 23:38:06 +02:00
return $parents;
2015-04-23 00:32:30 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* @param Directory $parent
2015-07-17 23:38:06 +02:00
*/
protected function setParent(Directory $parent)
2015-07-17 23:38:06 +02:00
{
if ($this->parent) {
$this->parent->removeChild($this);
2015-07-16 11:08:16 +02:00
}
$parent->addChild($this);
$this->parent = $parent;
2015-07-16 11:08:16 +02:00
}
2015-04-23 00:32:30 +02:00
2015-07-17 23:38:06 +02:00
/**
* @return string
*/
public function getPath()
2015-04-23 00:32:30 +02:00
{
return $this->path;
2015-04-23 00:32:30 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* @return string
*/
public function getUrl()
2015-04-23 00:32:30 +02:00
{
$url = '';
if ($this->getParent() && !$this->getParent() instanceof Root) {
$url = $this->getParent()->getUrl() . '/' . $url;
}
$url .= $this->getUri();
return $url;
2015-04-23 00:32:30 +02:00
}
public function dump()
2015-04-23 00:32:30 +02:00
{
return [
'title' => $this->getTitle(),
'type' => get_class($this),
'name' => $this->getName(),
'uri' => $this->getUri(),
'url' => $this->getUrl(),
'index' => $this->getIndexPage()? $this->getIndexPage()->getUrl() : '',
'first' => $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '',
'path' => $this->path
];
}
2015-04-23 00:32:30 +02:00
}