Permalink
Browse files

tests and zend factories

  • Loading branch information...
1 parent e7136c4 commit 6e9c2c9a23b88e82731112174b08cb27c0625c1f Michael Ridgway committed May 28, 2010
View
@@ -11,11 +11,16 @@ class Configuration
protected $_metadataDriver;
/**
- * @var Element\AbstractElementFactory
+ * @var Element\ElementFactoryInterface
*/
protected $_elementFactory;
/**
+ * @var Form\FormFactoryInterface
+ */
+ protected $_formFactory;
+
+ /**
* @param DriverInterface $impl
*/
public function setMetadataDriverImpl(Mapping\Driver\DriverInterface $impl)
@@ -47,4 +52,20 @@ public function getElementFactory()
return $this->_elementFactory;
}
+ /**
+ * @param Form\FormFactoryInterface $factory
+ */
+ public function setFormFactory(Form\FormFactoryInterface $factory)
+ {
+ $this->_formFactory = $factory;
+ }
+
+ /**
+ * @return Form\FormFactoryInterface
+ */
+ public function getFormFactory()
+ {
+ return $this->_formFactory;
+ }
+
}
@@ -10,5 +10,5 @@
* @param string $type
* @param FieldMetadataInfo $metadata
*/
- public function createElement($type, \FORM\Mapping\FieldMetadataInfo $metadata);
+ public function createElement($name, \FORM\Mapping\FieldMetadataInfo $metadata);
}
@@ -2,16 +2,94 @@
namespace FORM\Extension\Zend;
-class ElementFactory extends \FORM\Element\ElementFactoryInterface
+class ElementFactory implements \FORM\Element\ElementFactoryInterface
{
/**
* {@inheritdoc}
*
* @param string $type
* @param FormMetadataInfo $metadata
*/
- public function createElement($type, \FORM\Mapping\FormMetadataInfo $metadata)
+ public function createElement($name, \FORM\Mapping\FieldMetadataInfo $metadata)
{
-
+ $func = 'create' . ucfirst($metadata->getType()) . 'Element';
+ /* @var $element Zend_Form_Element */
+ $element = call_user_func(array($this, $func), $name, $metadata);
+ $element->setLabel($metadata->getLabel());
+ $element->setDescription($metadata->getDescription());
+ if ($metadata->getOrder()) {
+ $element->setOrder($metadata->getOrder());
+ }
+ $element->setRequired($metadata->getRequired());
+ $element->setValue($metadata->getDefaultValue());
+ $element->setValidators($this->getValidators($metadata->getValidators()));
+ return $element;
+ }
+
+ public function createTextElement($name, \FORM\Mapping\FieldMetadataInfo $metadata)
+ {
+ return new \Zend_Form_Element_Text($name);
+ }
+
+ public function createTextareaElement($name, \FORM\Mapping\FieldMetadataInfo $metadata)
+ {
+ return new \Zend_Form_Element_Textarea($name);
+ }
+
+ public function createCheckboxElement($name, \FORM\Mapping\FieldMetadataInfo $metadata)
+ {
+ return new \Zend_Form_Element_Checkbox($name);
+ }
+
+ public function getValidators(array $validators)
+ {
+ $zendValidators = array();
+ foreach($validators AS $validator) {
+ $zendValidators[] = $this->getValidator($validator);
+ }
+ return $zendValidators;
+ }
+
+ public function getValidator($validator)
+ {
+ $arguments = array();
+ if (is_array($validator)) {
+ $arguments = $validator;
+ $validator = array_shift($arguments);
+ }
+
+ switch ($validator) {
+ case 'date' :
+ return new \Zend_Validate_Date();
+ case 'maxLength' :
+ return new \Zend_Validate_StringLength(0, $arguments[0]);
+ case 'integer' :
+ return new \Zend_Validate_Int();
+ case 'float' :
+ return new \Zend_Validate_Float();
+ }
+ }
+
+ public function getFilters(array $filters)
+ {
+ $zendFliters = array();
+ foreach($filters AS $filter) {
+ $zendFilters[] = $this->getFilter($filter);
+ }
+ return $zendFilters;
+ }
+
+ public function getFilter($filter)
+ {
+ $arguments = array();
+ if (is_array($filter)) {
+ $arguments = $filter;
+ $filter = array_shift($arguments);
+ }
+
+ switch ($filter) {
+ case 'boolean' :
+ return new \Zend_Filter_Boolean();
+ }
}
}
@@ -0,0 +1,16 @@
+<?php
+
+namespace FORM\Extension\Zend;
+
+class FormFactory implements \FORM\Form\FormFactoryInterface
+{
+ /**
+ * {@inheritdoc}
+ *
+ * @param FormMetadataInfo $metadata
+ */
+ public function createForm(\FORM\Mapping\FormMetadataInfo $metadata)
+ {
+ return new \Zend_Form();
+ }
+}
@@ -0,0 +1,13 @@
+<?php
+
+namespace FORM\Form;
+
+interface FormFactoryInterface
+{
+ /**
+ * Creates a form of the given type
+ *
+ * @param FormMetadataInfo $metadata
+ */
+ public function createForm(\FORM\Mapping\FormMetadataInfo $metadata);
+}
View
@@ -21,6 +21,16 @@ class FormManager
private $_metadataFactory;
/**
+ * @var Element\ElementFactoryInterface
+ */
+ private $_elementFactory;
+
+ /**
+ * @var Form\FormFactoryInterface
+ */
+ private $_formFactory;
+
+ /**
* @var array
*/
private $_repositories = array();
@@ -32,12 +42,15 @@ public function __construct(Configuration $config = null)
{
$this->_config = $config;
$this->_metadataFactory = new Mapping\MetadataFactory($this);
+ $this->_elementFactory = $config->getElementFactory();
+ $this->_formFactory = $config->getFormFactory();
}
/**
* Gets the repository for the specified model type.
*
* @param string $modelName
+ * @return FormRepository
*/
public function getRepository($modelName)
{
@@ -68,4 +81,20 @@ public function getConfiguration()
{
return $this->_config;
}
+
+ /**
+ * @return Element\ElementFactory
+ */
+ public function getElementFactory()
+ {
+ return $this->_elementFactory;
+ }
+
+ /**
+ * @return Form\FormFactory
+ */
+ public function getFormFactory()
+ {
+ return $this->_formFactory;
+ }
}
@@ -16,10 +16,10 @@ class FormRepository
/**
* @var FormManager
*/
- protected $_formManager;
+ protected $_fm;
/**
- * @var Mapping\ClassMetadata
+ * @var Mapping\FormMetadata
*/
protected $_metadata;
@@ -32,17 +32,21 @@ class FormRepository
public function __construct(FormManager $formManager, Mapping\FormMetadata $metadata)
{
$this->_className = $metadata->getName();
- $this->_formManager = $formManager;
+ $this->_fm = $formManager;
$this->_metadata = $metadata;
}
/**
* Retrieves a form mediator object.
- *
- * @param string $modelName
- * @return Mediator
*/
- public function getForm(){}
+ public function getForm()
+ {
+ $form = $this->_fm->getFormFactory()->createForm($this->_metadata);
+ foreach ($this->_metadata->getFields() AS $field) {
+ $form->addElement($this->getElement($field->getName()));
+ }
+ return $form;
+ }
/**
* Retrieves a single mediator element from the current class type
@@ -51,6 +55,7 @@ public function getForm(){}
*/
public function getElement($propertyName)
{
+ return $this->_fm->getElementFactory()->createElement($propertyName, $this->_metadata->getField($propertyName));
}
/**
@@ -31,6 +31,9 @@ public function loadFormMetadataForClass($className, \FORM\Mapping\FormMetadataI
$doctrineMetadata = $this->_em->getClassMetadata($className);
// @todo extract the form metadata
+ foreach($doctrineMetadata->fieldMappings AS $fieldName => $data) {
+ $metadata->addField($fieldName);
+ }
return $metadata;
}
@@ -47,10 +50,89 @@ public function loadFieldMetadataForClass($className, $fieldName, \FORM\Mapping\
{
$doctrineMetadata = $this->_em->getClassMetadata($className);
- if (isset($doctrineMetadata['fieldMappings'][$fieldName])) {
+ if (isset($doctrineMetadata->fieldMappings[$fieldName])) {
+ $field = $doctrineMetadata->fieldMappings[$fieldName];
// @todo extract the field metadata
+ $metadata->setType($this->columnTypeToFieldType($field['type']));
+ $metadata->setRequired($field['nullable']);
+ $metadata->setValidators($this->getValidators($field));
+ $metadata->setFilters($this->getFilters($field));
}
return $metadata;
}
+
+ /**
+ * @param string $columnType
+ */
+ public function columnTypeToFieldType($columnType)
+ {
+ switch ($columnType) {
+ case 'date' :
+ case 'time' :
+ case 'datetime' :
+ case 'string' :
+ case 'integer' :
+ case 'smallint' :
+ case 'bigint' :
+ case 'decimal' :
+ return 'text';
+ case 'text' :
+ return 'textarea';
+ case 'boolean' :
+ return 'checkbox';
+ default:
+ return $columnType;
+ }
+ }
+
+ /**
+ * @param array $field
+ * @return array
+ */
+ public function getValidators($field)
+ {
+ $validators = array();
+ switch ($field['type']) {
+ case 'date' :
+ case 'time' :
+ case 'datetime' :
+ $validators[] = 'date';
+ break;
+ case 'string' :
+ if ($field['length']) {
+ $validators[] = array('maxLength', $field['length']);
+ }
+ break;
+ case 'integer' :
+ case 'smallint' :
+ case 'bigint' :
+ $validators[] = 'integer';
+ break;
+ case 'decimal' :
+ $validators[] = 'float';
+ break;
+ case 'text' :
+ break;
+ case 'boolean' :
+ break;
+ }
+
+ return $validators;
+ }
+
+ /**
+ * @param array $field
+ * @return array
+ */
+ public function getFilters($field)
+ {
+ $filters = array();
+ switch ($field['type']) {
+ case 'boolean' :
+ $filters[] = 'boolean';
+
+ }
+ return $filters;
+ }
}
@@ -2,7 +2,7 @@
namespace FORM\Mapping;
-class FieldMetadata extends FormMetadataInfo
+class FieldMetadata extends FieldMetadataInfo
{
/**
* @param string $fieldName
Oops, something went wrong.

0 comments on commit 6e9c2c9

Please sign in to comment.