/** * {@inheritDoc} */ public function createCharacteristicFromDefinition(Definition $definition, $value = null) { $characteristic = null; // TODO characteristic classes map switch ($definition->getType()) { case 'text': $characteristic = new TextCharacteristic(); break; case 'html': $characteristic = new HtmlCharacteristic(); break; case 'number': $characteristic = new NumberCharacteristic(); break; case 'boolean': $characteristic = new BooleanCharacteristic(); break; case 'datetime': $characteristic = new DatetimeCharacteristic(); break; case 'choice': $characteristic = new ChoiceCharacteristic(); if (null !== $value && !$value instanceof ChoiceCharacteristicValue) { $tmp = $value; $value = new ChoiceCharacteristicValue(); $value->setIdentifier($definition->getIdentifier())->setValue($tmp); } break; default: throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $definition->getType())); } $characteristic->setIdentifier($definition->getIdentifier())->setValue($value); return $characteristic; }
/** * Appends a form that matches the characteristic type. * * @param \Symfony\Component\Form\FormInterface $form * @param \Ekyna\Component\Characteristics\Schema\Definition $definition */ private function appendProperForm(FormInterface $form, Definition $definition) { $identifier = $definition->getIdentifier(); if ($form->has($identifier)) { return; } $type = sprintf('ekyna_%s_characteristic', $definition->getType()); $options = []; if ($definition->getType() == 'choice') { $options['identifier'] = $identifier; } $parentData = null; if (null !== $this->parentDatas && null !== ($characteristic = $this->parentDatas->findCharacteristicByIdentifier($identifier))) { $parentData = $characteristic->display($definition); } $form->add($identifier, $type, array_merge(['property_path' => '[' . $identifier . ']', 'label' => $definition->getTitle(), 'parent_data' => $parentData], $options)); }