8
0
daux.io/libs/Tree/Directory.php

321 Zeilen
8.1 KiB
PHP

<?php namespace Todaymade\Daux\Tree;
use ArrayIterator;
use RuntimeException;
use Todaymade\Daux\Config;
class Directory extends Entry implements \ArrayAccess, \IteratorAggregate
2015-04-23 00:32:30 +02:00
{
/** @var Entry[] */
protected $children = [];
/** @var Content */
protected $first_page;
2015-04-23 00:32:30 +02:00
public function sort()
{
2015-10-28 00:01:41 +01:00
// Separate the values into buckets to sort them separately
$buckets = [
'up_numeric' => [],
'up' => [],
'index' => [],
2015-10-28 00:01:41 +01:00
'numeric' => [],
'normal' => [],
'down_numeric' => [],
'down' => [],
];
foreach ($this->children as $key => $entry) {
2020-01-08 08:43:02 +01:00
// In case of generated pages, the name might be empty.
// Thus we are falling back to other solutions, otherwise the page would disappear from the tree.
2015-10-28 00:01:41 +01:00
$name = $entry->getName();
2020-01-08 08:43:02 +01:00
if (!$name) {
$name = $entry->getTitle();
}
if (!$name) {
$name = $key;
}
2018-05-05 18:16:24 +02:00
if (!$name) {
continue;
}
2020-01-08 08:43:02 +01:00
if ($name == 'index' || $name == '_index') {
$buckets['index'][$key] = $entry;
continue;
}
2016-07-27 21:32:51 +02:00
if ($name[0] == '-') {
2015-10-28 00:01:41 +01:00
if (is_numeric($name[1])) {
2016-07-27 21:32:51 +02:00
$exploded = explode('_', $name);
2015-10-28 00:01:41 +01:00
$buckets['down_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
continue;
}
$buckets['down'][$key] = $entry;
continue;
}
if ($name[0] == '+') {
if (is_numeric($name[1])) {
$exploded = explode('_', $name);
$buckets['up_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
continue;
}
$buckets['up'][$key] = $entry;
continue;
}
2015-10-28 00:01:41 +01:00
if (is_numeric($name[0])) {
2016-07-27 21:32:51 +02:00
$exploded = explode('_', $name);
2015-10-28 00:01:41 +01:00
$buckets['numeric'][abs($exploded[0])][$key] = $entry;
continue;
}
$buckets['normal'][$key] = $entry;
}
$final = [];
foreach ($buckets as $name => $bucket) {
if (substr($name, -7) == 'numeric') {
2015-10-28 00:01:41 +01:00
ksort($bucket);
foreach ($bucket as $sub_bucket) {
$final = $this->sortBucket($sub_bucket, $final);
}
} else {
$final = $this->sortBucket($bucket, $final);
}
}
$this->children = $final;
}
private function sortBucket($bucket, $final)
{
2018-05-05 18:16:24 +02:00
uasort($bucket, function(Entry $a, Entry $b) {
2015-10-28 00:01:41 +01:00
return strcasecmp($a->getName(), $b->getName());
});
foreach ($bucket as $key => $value) {
$final[$key] = $value;
}
return $final;
}
2015-07-18 21:45:38 +02:00
/**
* @return Entry[]
*/
public function getEntries()
{
return $this->children;
}
public function addChild(Entry $entry): void
{
$this->children[$entry->getUri()] = $entry;
}
public function removeChild(Entry $entry): void
{
unset($this->children[$entry->getUri()]);
}
/**
* @return Config
*/
public function getConfig(): Config
{
if (!$this->parent) {
2016-07-27 21:32:51 +02:00
throw new \RuntimeException('Could not retrieve configuration. Are you sure that your tree has a Root ?');
}
return $this->parent->getConfig();
}
public function getLocalIndexPage() {
$index_key = $this->getConfig()->getIndexKey();
if (isset($this->children[$index_key])) {
return $this->children[$index_key];
}
return false;
}
/**
* @return Content|null
*/
public function getIndexPage(): ?Content
{
$indexPage = $this->getLocalIndexPage();
if ($indexPage instanceof Content) {
return $indexPage;
}
if ($this->getConfig()->shouldInheritIndex() && $first_page = $this->seekFirstPage()) {
return $first_page;
}
return null;
}
/**
* Seek the first available page from descendants
* @return Content|null
*/
public function seekFirstPage(): ?Content
2015-08-16 22:26:00 +02:00
{
2016-07-27 21:32:51 +02:00
if ($this instanceof self) {
$index_key = $this->getConfig()->getIndexKey();
if (isset($this->children[$index_key]) && $this->children[$index_key] instanceof Content) {
2015-08-16 22:26:00 +02:00
return $this->children[$index_key];
}
foreach ($this->children as $node_key => $node) {
if ($node instanceof Content) {
return $node;
}
2016-07-27 21:32:51 +02:00
if ($node instanceof self
2015-08-16 22:26:00 +02:00
&& strpos($node->getUri(), '.') !== 0
2016-07-27 21:32:51 +02:00
&& $childNode = $node->seekFirstPage()) {
2015-08-16 22:26:00 +02:00
return $childNode;
}
}
}
2016-07-27 21:32:51 +02:00
2015-08-16 22:26:00 +02:00
return null;
}
/**
2015-07-20 15:59:52 +02:00
* @return Content|null
*/
public function getFirstPage(): ?Content
{
if ($this->first_page) {
return $this->first_page;
}
// First we try to find a real page
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
2015-07-19 01:16:04 +02:00
if ($this instanceof Root && $this->getIndexPage() == $node) {
// The homepage should not count as first page
continue;
}
$this->setFirstPage($node);
2016-07-27 21:32:51 +02:00
return $node;
}
}
// If we can't find one we check in the sub-directories
foreach ($this->getEntries() as $node) {
2016-07-27 21:32:51 +02:00
if ($node instanceof self && $page = $node->getFirstPage()) {
$this->setFirstPage($page);
2016-07-27 21:32:51 +02:00
return $page;
}
}
2015-07-20 15:59:52 +02:00
return null;
}
/**
* @param Content $first_page
*/
public function setFirstPage(Content $first_page)
{
$this->first_page = $first_page;
}
2015-07-29 08:30:41 +02:00
/**
* Used when creating the navigation.
* Hides folders without showable content
*
* @return bool
*/
public function hasContent(): bool
2015-07-29 08:30:41 +02:00
{
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
return true;
2016-07-27 21:32:51 +02:00
} elseif ($node instanceof self) {
2015-07-29 08:30:41 +02:00
if ($node->hasContent()) {
return true;
}
}
}
return false;
}
public function dump()
{
$dump = parent::dump();
$dump['index'] = $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '';
$dump['first'] = $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '';
foreach ($this->getEntries() as $entry) {
$dump['children'][] = $entry->dump();
}
return $dump;
}
/**
* Whether a offset exists
* @param mixed $offset An offset to check for.
2016-07-27 21:32:51 +02:00
* @return bool true on success or false on failure.
*/
public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->children);
}
/**
* Offset to retrieve
* @param mixed $offset The offset to retrieve.
* @return Entry Can return all value types.
*/
public function offsetGet($offset)
{
return $this->children[$offset];
}
/**
* Offset to set
* @param mixed $offset The offset to assign the value to.
* @param Entry $value The value to set.
* @return void
*/
public function offsetSet($offset, $value)
{
if (!$value instanceof Entry) {
2016-07-27 21:32:51 +02:00
throw new RuntimeException('The value is not of type Entry');
}
$this->addChild($value);
}
/**
* Offset to unset
* @param string $offset the offset to unset
* @return void
*/
public function offsetUnset($offset)
{
unset($this->children[$offset]);
}
public function getIterator()
{
return new ArrayIterator($this->children);
}
}