/**
  * {@inheritDoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     $resolver->setRequired(['attribute_type']);
     $resolver->setDefaults(['empty_value' => 'orob2b.attribute.attribute_type_constraint.none']);
     $resolver->setNormalizers(['choices' => function (Options $options, $value) {
         if (!empty($value)) {
             return $value;
         }
         $choices = [];
         if ($options['attribute_type'] instanceof AttributeTypeInterface) {
             $constraints = $options['attribute_type']->getOptionalConstraints();
         } elseif (is_string($options['attribute_type'])) {
             $attributeType = $this->attributeTypeRegistry->getTypeByName($options['attribute_type']);
             if (!$attributeType) {
                 throw new \LogicException(sprintf('Attribute type name "%s" is not exist in attribute type registry.', $options['attribute_type']));
             }
             $constraints = $attributeType->getOptionalConstraints();
         } else {
             throw new UnexpectedTypeException($options['attribute_type'], 'OroB2B\\Bundle\\AttributeBundle\\AttributeType\\AttributeTypeInterface or string');
         }
         foreach ($constraints as $choice) {
             $choices[$choice->getAlias()] = 'orob2b.validation.constraint.alias.' . $choice->getAlias();
         }
         return $choices;
     }]);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $attribute = $options['data'];
     if (!$attribute instanceof Attribute) {
         throw new UnexpectedTypeException($attribute, 'Attribute');
     }
     $attributeType = $this->typeRegistry->getTypeByName($attribute->getType());
     if (!$attributeType) {
         throw new LogicException(sprintf('Attribute type "%s" not found', $attribute->getType()));
     }
     $this->addMainFields($builder);
     $this->addValidationFields($builder, $attributeType);
     if ($attributeType instanceof OptionAttributeTypeInterface) {
         $this->addDefaultOptionsField($builder, $attribute, $attributeType);
     } else {
         $this->addDefaultValueField($builder, $attribute, $attributeType);
     }
     $this->addPropertyFields($builder, $attributeType);
     $builder->addViewTransformer(new AttributeTransformer($this->managerRegistry, $this->typeRegistry, $attribute));
     $this->addDisabledFields($builder);
     $builder->addViewTransformer(new AttributeDisabledFieldsTransformer());
 }
 public function testGetTypeByNameResult()
 {
     $this->assertEquals($this->attributeTypeMock, $this->registry->getTypeByName(self::ATTRIBUTE_TYPE_NAME));
 }