Skip to content

Instantly share code, notes, and snippets.

@flug
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flug/289becd1a8bc055dc439 to your computer and use it in GitHub Desktop.
Save flug/289becd1a8bc055dc439 to your computer and use it in GitHub Desktop.
An abstract class for reflect entity
<?php
namespace My\Bundle\FrontendBundle\Form;
use Symfony\Component\Form\AbstractType as SymfonyAbstractType;
use Symfony\Component\Form\FormBuilderInterface;
abstract class AbstractType extends SymfonyAbstractType
{
private $reflection;
public function __construct($namespace)
{
$this->reflection = new \ReflectionClass($namespace);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($this->reflection->getProperties(\ReflectionProperty::IS_PROTECTED) as $field) {
if ($this->isNotAvailable($field)) {
$builder->add(
$field->getName(),
$this->type($field),
$this->options($field)
)
;
}
}
}
/**
* @param \ReflectionProperty $field
* @return boolean
*/
protected abstract function isNotAvailable(\ReflectionProperty $field);
/**
* @param \ReflectionProperty $property
* @return string
*/
public function type(\ReflectionProperty $property)
{
return null;
}
/**
* @param \ReflectionProperty $property
* @return array
*/
public function options(\ReflectionProperty $property)
{
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment