Last active
August 29, 2015 14:24
-
-
Save flug/289becd1a8bc055dc439 to your computer and use it in GitHub Desktop.
An abstract class for reflect entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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