mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 17:41:50 +01:00
Collection/storage of templates
This commit is contained in:
65
src/Collection/Templates.php
Normal file
65
src/Collection/Templates.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Meritoo\Common\Collection;
|
||||
|
||||
use Meritoo\Common\Exception\ValueObject\Template\TemplateNotFoundException;
|
||||
use Meritoo\Common\ValueObject\Template;
|
||||
|
||||
/**
|
||||
* Collection/storage of templates
|
||||
*
|
||||
* @author Meritoo <github@meritoo.pl>
|
||||
* @copyright Meritoo <http://www.meritoo.pl>
|
||||
*/
|
||||
class Templates extends Collection
|
||||
{
|
||||
/**
|
||||
* Finds and returns template with given index
|
||||
*
|
||||
* @param string $index Index that contains required template
|
||||
* @throws TemplateNotFoundException
|
||||
* @return Template
|
||||
*/
|
||||
public function findTemplate(string $index): Template
|
||||
{
|
||||
/* @var Template $template */
|
||||
$template = $this->getByIndex($index);
|
||||
|
||||
if ($template instanceof Template) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
// Oops, template not found
|
||||
throw TemplateNotFoundException::create($index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns the collection from given array
|
||||
*
|
||||
* @param array $templates Pairs of key-value where: key - template's index, value - template's content
|
||||
* @return Templates
|
||||
*/
|
||||
public static function fromArray(array $templates): Templates
|
||||
{
|
||||
// No templates. Nothing to do.
|
||||
if (empty($templates)) {
|
||||
return new static();
|
||||
}
|
||||
|
||||
$result = new static();
|
||||
|
||||
foreach ($templates as $index => $template) {
|
||||
$result->add(new Template($template), $index);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user