Added FormClassNameService

This commit is contained in:
Kevin Frantz 2019-01-26 20:22:17 +01:00
parent a94639b992
commit 1d8a0f0b19
3 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?php
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
final class FormClassNameService implements FormClassNameServiceInterface
{
const ENTITY_BASE_PATH = 'App\\Entity';
const FORM_BASE_PATH = 'App\\Form';
const SUFFIX = 'Type';
/**
* @param EntityInterface $entity
*
* @return string
*/
public function getName(EntityInterface $entity): string
{
$class = get_class($entity);
$replaced = str_replace(self::ENTITY_BASE_PATH, self::FORM_BASE_PATH, $class);
$withSuffix = $replaced.self::SUFFIX;
return $withSuffix;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
interface FormClassNameServiceInterface
{
/**
* @param EntityInterface $entity
*
* @return string The name of the form of the entity
*/
public function getName(EntityInterface $entity): string;
}

View File

@ -0,0 +1,21 @@
<?php
namespace tests\Unit\Domain\FormManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\FormManagement\FormClassNameService;
use App\Entity\Source\PureSource;
/**
* @author kevinfrantz
*/
class FormClassNameServiceTest extends TestCase
{
public function testGetName()
{
$entity = new PureSource();
$formNameService = new FormClassNameService();
$entityForm = $formNameService->getName($entity);
$this->assertEquals('App\\Form\\Source\\PureSourceType', $entityForm);
}
}