<?php
namespace App\Type;
use App\Form\Setting\AbstractSettingType;
use Flagception\Manager\FeatureManagerInterface;
class TypeCollection
{
public const GROUP_GENERAL = 'General';
public const GROUP_VACANCY = 'Vacancy';
public const GROUP_ATS = 'ATS';
public const GROUP_MODULES = 'Modules';
public const GROUP_EXTERNAL_PLUGINGS = 'External plugins';
/**
* @var AbstractSettingType[]
*/
private $types;
private array $groupedTypes = [];
/**
* @var FeatureManagerInterface
*/
private $manager;
public function __construct(FeatureManagerInterface $manager)
{
$this->types = [];
$this->manager = $manager;
}
public function addType(
AbstractSettingType $type,
string $name,
?string $feature = null,
?string $template = null,
string $group = self::GROUP_GENERAL
) {
if ($feature && !$this->manager->isActive($feature)) {
return;
}
$this->validateGroupName($group);
$this->types[$name] = [
'type' => $type,
'template' => $template,
];
if (!\array_key_exists($group, $this->groupedTypes)) {
$this->groupedTypes[$group] = [];
}
$this->groupedTypes[$group][$name] = $type;
}
/**
* @param $name
*
* @return AbstractSettingType
*/
public function getTypeByName($name): ?AbstractSettingType
{
if (\array_key_exists($name, $this->types)) {
return $this->types[$name]['type'];
}
return null;
}
public function getTemplateByName(string $name): ?string
{
if (\array_key_exists($name, $this->types)) {
return $this->types[$name]['template'];
}
return null;
}
/**
* @return AbstractSettingType[]|array
*/
public function getTypes(): array
{
return $this->types;
}
public function getTypesByGroup(string $group): array
{
$this->validateGroupName($group);
return $this->groupedTypes[$group] ?? [];
}
private function validateGroupName(string $group)
{
if (!\in_array($group, [
self::GROUP_GENERAL,
self::GROUP_ATS,
self::GROUP_VACANCY,
self::GROUP_MODULES,
self::GROUP_EXTERNAL_PLUGINGS,
], true)) {
throw new \InvalidArgumentException('Setting group does not exist');
}
}
}