Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function createBuilder($type = 'Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType', $data = null, array $options = array())
 {
     $name = null;
     $typeName = null;
     if ($type instanceof ResolvedFormTypeInterface) {
         if (method_exists($type, 'getBlockPrefix')) {
             // As of Symfony 3.0, the block prefix of the type is used as
             // default name
             $name = $type->getBlockPrefix();
         } else {
             // BC
             $typeName = $type->getName();
         }
     } elseif ($type instanceof FormTypeInterface) {
         // BC
         $typeName = $type->getName();
     } elseif (is_string($type)) {
         // BC
         $typeName = $type;
     } else {
         throw new UnexpectedTypeException($type, 'string, Symfony\\Component\\Form\\ResolvedFormTypeInterface or Symfony\\Component\\Form\\FormTypeInterface');
     }
     if (null === $name) {
         if (false === strpos($typeName, '\\')) {
             // No FQCN - leave unchanged for BC
             $name = $typeName;
         } else {
             // FQCN
             $name = StringUtil::fqcnToBlockPrefix($typeName);
         }
     }
     return $this->createNamedBuilder($name, $type, $data, $options);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function createBuilder($type = 'Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType', $data = null, array $options = array())
 {
     if (!is_string($type)) {
         throw new UnexpectedTypeException($type, 'string');
     }
     return $this->createNamedBuilder(StringUtil::fqcnToBlockPrefix($type), $type, $data, $options);
 }
Ejemplo n.º 3
0
 /**
  * Returns the prefix of the template block name for this type.
  *
  * The block prefixes default to the underscored short class name with
  * the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
  *
  * @return string The prefix of the template block name
  */
 public function getBlockPrefix()
 {
     $fqcn = get_class($this);
     $name = $this->getName();
     // For BC: Use the name as block prefix if one is set
     return $name !== $fqcn ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
 }
Ejemplo n.º 4
0
 public function getBlockPrefix()
 {
     if ($this->type) {
         return $this->type;
     }
     return StringUtil::fqcnToBlockPrefix($this->class);
 }
Ejemplo n.º 5
0
 public function preSubmit(FormEvent $event)
 {
     $data = $event->getData();
     if (!is_string($data)) {
         return;
     }
     $event->setData(StringUtil::trim($data));
 }
 public function test()
 {
     $form = $this->factory->create($this->getExtensionTypeName());
     $config = $form->getConfig();
     $this->assertTrue($config->hasOption('select2'));
     $select2Opts = $config->getOption('select2');
     $this->assertTrue(array_key_exists('ajax_route', $select2Opts));
     $this->assertNull($select2Opts['ajax_route']);
     $this->assertEquals('sonatra_form_extensions_ajax_' . StringUtil::fqcnToBlockPrefix($this->getExtensionTypeName()), $config->getAttribute('select2_ajax_route'));
 }
 /**
  * @dataProvider spaceProvider
  */
 public function testTrimUtf8Separators($hex)
 {
     // Convert hexadecimal representation into binary
     // H: hex string, high nibble first (UCS-2BE)
     // *: repeat until end of string
     $binary = pack('H*', $hex);
     // Convert UCS-2BE to UTF-8
     $symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE');
     $symbol = $symbol . "ab\ncd" . $symbol;
     $this->assertSame("ab\ncd", StringUtil::trim($symbol));
 }
Ejemplo n.º 8
0
    public function __construct(FormTypeInterface $innerType, array $typeExtensions = array(), ResolvedFormTypeInterface $parent = null)
    {
        $fqcn = get_class($innerType);
        $name = $innerType->getName();
        $hasCustomName = $name !== $fqcn;

        if (method_exists($innerType, 'getBlockPrefix')) {
            $reflector = new \ReflectionMethod($innerType, 'getName');
            $isOldOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';

            $reflector = new \ReflectionMethod($innerType, 'getBlockPrefix');
            $isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';

            // Bundles compatible with both 2.3 and 2.8 should implement both methods
            // Anyone else should only override getBlockPrefix() if they actually
            // want to have a different block prefix than the default one
            if ($isOldOverwritten && !$isNewOverwritten) {
                @trigger_error(get_class($this->innerType).': The FormTypeInterface::getName() method is deprecated since version 2.8 and will be removed in 3.0. Remove it from your classes. Use getBlockPrefix() if you want to customize the template block prefix. This method will be added to the FormTypeInterface with Symfony 3.0.', E_USER_DEPRECATED);
            }

            $blockPrefix = $innerType->getBlockPrefix();
        } else {
            @trigger_error(get_class($this->innerType).': The FormTypeInterface::getBlockPrefix() method will be added in version 3.0. You should extend AbstractType or add it to your implementation.', E_USER_DEPRECATED);

            // Deal with classes that don't extend AbstractType
            // Calculate block prefix from the FQCN by default
            $blockPrefix = $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
        }

        // As of Symfony 2.8, getName() returns the FQCN by default
        // Otherwise check that the name matches the old naming restrictions
        if ($hasCustomName && !preg_match('/^[a-z0-9_]*$/i', $name)) {
            throw new InvalidArgumentException(sprintf(
                'The "%s" form type name ("%s") is not valid. Names must only contain letters, numbers, and "_".',
                get_class($innerType),
                $name
            ));
        }

        foreach ($typeExtensions as $extension) {
            if (!$extension instanceof FormTypeExtensionInterface) {
                throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
            }
        }

        $this->name = $name;
        $this->blockPrefix = $blockPrefix;
        $this->innerType = $innerType;
        $this->typeExtensions = $typeExtensions;
        $this->parent = $parent;
    }
Ejemplo n.º 9
0
 /**
  * @dataProvider spaceProvider
  */
 public function testTrimUtf8Separators($hex)
 {
     if (!function_exists('mb_convert_encoding')) {
         $this->markTestSkipped('The "mb_convert_encoding" function is not available');
     }
     // Convert hexadecimal representation into binary
     // H: hex string, high nibble first (UCS-2BE)
     // *: repeat until end of string
     $binary = pack('H*', $hex);
     // Convert UCS-2BE to UTF-8
     $symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE');
     $symbol = $symbol . "ab\ncd" . $symbol;
     $this->assertSame("ab\ncd", StringUtil::trim($symbol));
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $name = $form->getName();
     $blockName = $options['block_name'] ?: $form->getName();
     $translationDomain = $options['translation_domain'];
     $labelFormat = $options['label_format'];
     if ($view->parent) {
         if ('' !== ($parentFullName = $view->parent->vars['full_name'])) {
             $id = sprintf('%s_%s', $view->parent->vars['id'], $name);
             $fullName = sprintf('%s[%s]', $parentFullName, $name);
             $uniqueBlockPrefix = sprintf('%s_%s', $view->parent->vars['unique_block_prefix'], $blockName);
         } else {
             $id = $name;
             $fullName = $name;
             $uniqueBlockPrefix = '_' . $blockName;
         }
         if (null === $translationDomain) {
             $translationDomain = $view->parent->vars['translation_domain'];
         }
         if (!$labelFormat) {
             $labelFormat = $view->parent->vars['label_format'];
         }
     } else {
         $id = $name;
         $fullName = $name;
         $uniqueBlockPrefix = '_' . $blockName;
         // Strip leading underscores and digits. These are allowed in
         // form names, but not in HTML4 ID attributes.
         // http://www.w3.org/TR/html401/struct/global.html#adef-id
         $id = ltrim($id, '_0123456789');
     }
     $blockPrefixes = array();
     for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
         if (method_exists($type, 'getBlockPrefix')) {
             array_unshift($blockPrefixes, $type->getBlockPrefix());
         } else {
             @trigger_error(get_class($type) . ': The ResolvedFormTypeInterface::getBlockPrefix() method will be added in version 3.0. You should add it to your implementation.', E_USER_DEPRECATED);
             $fqcn = get_class($type->getInnerType());
             $name = $type->getName();
             $hasCustomName = $name !== $fqcn;
             array_unshift($blockPrefixes, $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn));
         }
     }
     $blockPrefixes[] = $uniqueBlockPrefix;
     $view->vars = array_replace($view->vars, array('form' => $view, 'id' => $id, 'name' => $name, 'full_name' => $fullName, 'disabled' => $form->isDisabled(), 'label' => $options['label'], 'label_format' => $labelFormat, 'multipart' => false, 'attr' => $options['attr'], 'block_prefixes' => $blockPrefixes, 'unique_block_prefix' => $uniqueBlockPrefix, 'translation_domain' => $translationDomain, 'cache_key' => $uniqueBlockPrefix . '_' . $form->getConfig()->getType()->getName()));
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function getBlockPrefix()
 {
     return StringUtil::fqcnToBlockPrefix(get_class($this));
 }
Ejemplo n.º 12
0
 /**
  * @dataProvider fqcnToBlockPrefixProvider
  */
 public function testFqcnToBlockPrefix($fqcn, $expectedBlockPrefix)
 {
     $blockPrefix = StringUtil::fqcnToBlockPrefix($fqcn);
     $this->assertSame($expectedBlockPrefix, $blockPrefix);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if (null === $options['select2']['ajax_route']) {
         $builder->setAttribute('select2_ajax_route', 'sonatra_form_extensions_ajax_' . StringUtil::fqcnToBlockPrefix($this->type));
     }
 }