2015-07-29 22:31:41 +02:00
|
|
|
<?php namespace Todaymade\Daux\ContentTypes;
|
2015-07-28 17:25:03 +02:00
|
|
|
|
|
|
|
use Todaymade\Daux\Tree\Content;
|
|
|
|
|
|
|
|
class ContentTypeHandler
|
|
|
|
{
|
|
|
|
/**
|
2016-07-27 21:32:51 +02:00
|
|
|
* @var ContentType[]
|
2015-07-28 17:25:03 +02:00
|
|
|
*/
|
|
|
|
protected $types;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ContentType[] $types
|
|
|
|
*/
|
|
|
|
public function __construct($types)
|
|
|
|
{
|
|
|
|
$this->types = array_reverse($types);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* Get all valid content file extensions.
|
2015-07-28 17:25:03 +02:00
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getContentExtensions()
|
|
|
|
{
|
|
|
|
$extensions = [];
|
|
|
|
foreach ($this->types as $type) {
|
|
|
|
$extensions = array_merge($extensions, $type->getExtensions());
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* Get the ContentType able to handle this node.
|
2015-07-28 17:25:03 +02:00
|
|
|
*
|
|
|
|
* @return ContentType
|
|
|
|
*/
|
|
|
|
public function getType(Content $node)
|
|
|
|
{
|
2015-08-06 22:05:17 +02:00
|
|
|
$path = $node->getPath() ?: $node->getName();
|
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
2015-07-28 17:25:03 +02:00
|
|
|
|
|
|
|
foreach ($this->types as $type) {
|
|
|
|
if (in_array($extension, $type->getExtensions())) {
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:05:17 +02:00
|
|
|
throw new \RuntimeException("no contentType found for $path");
|
2015-07-28 17:25:03 +02:00
|
|
|
}
|
|
|
|
}
|