src/Service/Common/Owner/OwnerFactory.php line 113
<?php
namespace App\Service\Common\Owner;
use App\Entity\Common\Owner;
use App\Entity\UUID\HaveUuidInterface;
use App\Model\Common\OwnerConfig;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccessor;
class OwnerFactory
{
/**
* @var array
*/
private $config;
public function __construct(array $config)
{
// $this->config = $config;
$test = [];
$op = $this->configureOptions();
foreach ($config as $key => $value) {
$test[$key] = $op->resolve($value);
$test[$key]['render'] = $this->parseRender($test[$key]['render']);
}
$this->config = $test;
// dump($test);
}
private function parseRender(array $render): array
{
return array_map(function ($r) {
if (is_string($r)) {
return [
'type' => 'template',
'name' => $r,
];
}
return $r;
}, $render);
}
private function configureOptions(): OptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setRequired(
[
'title',
'class',
]
);
$resolver->setDefaults(
[
'identity' => 'id',
'caption' => 'caption',
'forum' => function (OptionsResolver $resolver) {
$resolver->setDefaults(
[
'is_single' => false,
]
);
},
'repository' => function (OptionsResolver $resolver) {
$resolver->setDefaults(
[
'single' => 'find4Owner',
'forum_threads' => 'findForum',
'admin_list' => 'findAdminQb',
]
);
},
'render' => [],
// 'render' => function (OptionsResolver $resolver) {
//
//
// },
]
);
return $resolver;
}
public function getOwner(HaveUuidInterface $obj): Owner
{
$owner = new Owner();
// $key = $this->findKey(get_class($obj));
// $owner->setType($key);
// dump(get_class($obj),$key);
// $config = $this->get($key);
$pa = new PropertyAccessor();
// $owner->setId($pa->getValue($obj, $config->getId()));
$owner->setUuid($obj->getUuid());
$owner->setActive($pa->getValue($obj, 'active') ?: true);
$owner->setDeleted($pa->getValue($obj, 'deleted') ?: false);
return $owner;
}
public function findKey($class): ?string
{
foreach ($this->config as $key => $value) {
if ($value['class'] === $class) {
return $key;
}
}
return null;
}
public function get($key): OwnerConfig
{
return new OwnerConfig($key, $this->config[$key]);
}
public function all()
{
return $this->config;
}
}