public function testBcDefaultFormatter()
 {
     $formatter = new RawFormatter();
     $env = $this->getMock('\\Twig_Environment');
     $pool = new Pool();
     $pool->add('foo', $formatter, $env);
     $this->assertSame('foo', $pool->getDefaultFormatter());
 }
 public function testUnexpectedException()
 {
     $this->setExpectedException('RuntimeException');
     $formatter = new RawFormatter();
     $env = $this->getMock('\\Twig_Environment');
     $env->expects($this->once())->method('render')->will($this->throwException(new \RuntimeException('Error')));
     $pool = new Pool();
     $pool->add('foo', $formatter, $env);
     $pool->transform('foo', 'Salut');
 }
 /**
  * @param FormEvent $event
  */
 public function postSubmit(FormEvent $event)
 {
     $accessor = PropertyAccess::createPropertyAccessor();
     $format = $accessor->getValue($event->getData(), $this->formatField);
     $source = $accessor->getValue($event->getData(), $this->sourceField);
     // make sure the listener works with array
     $data = $event->getData();
     $accessor->setValue($data, $this->targetField, $this->pool->transform($format, $source));
     $event->setData($data);
 }
 public function testWithValidFormatter()
 {
     $formatter = $this->getMock('Sonata\\FormatterBundle\\Formatter\\FormatterInterface');
     $formatter->expects($this->once())->method('transform')->will($this->returnCallback(function ($text) {
         return strtoupper($text);
     }));
     $pool = new Pool();
     $pool->add('myformat', $formatter);
     $listener = new FormatterListener($pool, '[format]', '[source]', '[target]');
     $event = new FormEvent($this->getMock('Symfony\\Component\\Form\\Test\\FormInterface'), array('format' => 'myformat', 'source' => 'data', 'target' => null));
     $listener->postSubmit($event);
     $expected = array('format' => 'myformat', 'source' => 'data', 'target' => 'DATA');
     $this->assertSame($expected, $event->getData());
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if (is_array($options['format_field'])) {
         list($formatField, $formatPropertyPath) = $options['format_field'];
         $options['format_field_options']['property_path'] = $formatPropertyPath;
     } else {
         $formatField = $options['format_field'];
         $options['format_field_options']['property_path'] = $formatField;
     }
     $options['format_field_options']['data'] = $this->pool->getDefaultFormatter();
     if (is_array($options['source_field'])) {
         list($sourceField, $sourcePropertyPath) = $options['source_field'];
         $options['source_field_options']['property_path'] = $sourcePropertyPath;
     } else {
         $sourceField = $options['source_field'];
         $options['source_field_options']['property_path'] = $sourceField;
     }
     $builder->add($formatField, 'choice', $options['format_field_options']);
     // If there's only one possible format, do not display the choices
     $formatChoices = $builder->get($formatField)->getOption('choices');
     if (count($formatChoices) === 1) {
         // Remove the choice field
         unset($options['format_field_options']['choices']);
         $builder->remove($formatField);
         // Replace it with an hidden field
         $builder->add($formatField, 'hidden', $options['format_field_options']);
     }
     $builder->add($sourceField, 'textarea', $options['source_field_options']);
     /*
      * The listener option only work if the source field is after the current field
      */
     if ($options['listener']) {
         if (!$options['event_dispatcher'] instanceof EventDispatcherInterface) {
             throw new \RuntimeException('The event_dispatcher option must be an instance of EventDispatcherInterface');
         }
         $listener = new FormatterListener($this->pool, $options['format_field_options']['property_path'], $options['source_field_options']['property_path'], $options['target_field']);
         $options['event_dispatcher']->addListener(FormEvents::SUBMIT, array($listener, 'postSubmit'));
     }
 }
 /**
  * Write a post, this method is used by both POST and PUT action methods.
  *
  * @param Request  $request Symfony request
  * @param int|null $id      A post identifier
  *
  * @return FormInterface
  */
 protected function handleWritePost($request, $id = null)
 {
     $post = $id ? $this->getPost($id) : null;
     $form = $this->formFactory->createNamed(null, 'sonata_news_api_form_post', $post, array('csrf_protection' => false));
     $form->bind($request);
     if ($form->isValid()) {
         $post = $form->getData();
         $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
         $this->postManager->save($post);
         $view = \FOS\RestBundle\View\View::create($post);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }
 /**
  * Write a product, this method is used by both POST and PUT action methods
  *
  * @param string       $provider A product provider name
  * @param Request      $request  Symfony request
  * @param integer|null $id       A product identifier
  *
  * @return \FOS\RestBundle\View\View|FormInterface
  */
 protected function handleWriteProduct($provider, $request, $id = null)
 {
     $product = $id ? $this->getProduct($id) : null;
     $manager = $this->productPool->getManager($provider);
     $form = $this->formFactory->createNamed(null, 'sonata_product_api_form_product', $product, array('csrf_protection' => false, 'data_class' => $manager->getClass(), 'provider_name' => $provider));
     $form->bind($request);
     if ($form->isValid()) {
         $product = $form->getData();
         $product->setDescription($this->formatterPool->transform($product->getDescriptionFormatter(), $product->getRawDescription()));
         $product->setShortDescription($this->formatterPool->transform($product->getShortDescriptionFormatter(), $product->getRawShortDescription()));
         $manager->save($product);
         $view = \FOS\RestBundle\View\View::create($product);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function preUpdate($post)
 {
     $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$this->pool->has($value)) {
         $this->context->addViolation($constraint->message);
     }
 }
 /**
  * @param string $text
  * @param string $type
  *
  * @return string
  */
 public function transform($text, $type)
 {
     return $this->pool->transform($type, $text);
 }