/** * @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 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'); }
/** * 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())); }
/** * @param string $text * @param string $type * * @return string */ public function transform($text, $type) { return $this->pool->transform($type, $text); }