Files
emails/api/common/Factory/Model.php
2022-11-25 20:52:52 -03:00

58 lines
1.6 KiB
PHP

<?php
namespace ProVM\Common\Factory;
use ProVM\Common\Implement\Repository;
use Psr\Container\ContainerInterface;
class Model
{
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
}
protected ContainerInterface $container;
protected array $repositories;
public function getContainer(): ContainerInterface
{
return $this->container;
}
public function getRepositories(): array
{
return $this->repositories;
}
public function getRepository(string $name): string
{
return $this->getRepositories()[$name];
}
public function setContainer(ContainerInterface $container): Model
{
$this->container = $container;
return $this;
}
public function addRepository(string $name, string $repository_class_name): Model
{
$this->repositories[$name] = $repository_class_name;
return $this;
}
public function setRepositories(array $repositories): Model
{
foreach ($repositories as $name => $class) {
$this->addRepository($name, $class);
}
return $this;
}
public function find(string $model_class_name): Repository
{
$name = str_replace("ProVM\\Emails\\Model\\", '', $model_class_name);
try {
$repository_class = $this->getRepository($name);
} catch (\Exception $e) {
$repository_class = str_replace('Model', 'Repository', $model_class_name);
}
return $this->getContainer()->get($repository_class);
}
}