/**
  * @param $value
  * @param $filter
  * @throws \Twig_Error_Runtime
  * @return \FM\BbcodeBundle\Decoda\Decoda
  */
 public function filter($value, $filter)
 {
     if (!is_string($value)) {
         throw new \Twig_Error_Runtime('The filter can be applied to strings only.');
     }
     $messages = $this->container->getParameter('fm_bbcode.config.messages');
     $messages = empty($messages) ? array() : json_decode(\file_get_contents($messages), true);
     $code = new Decoda($value, $messages);
     $current_filter = $this->filter_sets[$filter];
     $locale = $current_filter['locale'];
     $xhtml = $current_filter['xhtml'];
     if (empty($locale)) {
         // apply locale from the session
         if ('default' == $this->locale) {
             $code->setLocale($this->container->get('session')->getLocale());
             // apply locale defined in the configuration
         } else {
             // apply locale from the template
             $code->setLocale($this->locale);
         }
     } else {
         $code->setLocale($locale);
     }
     if (true === $xhtml) {
         $code->setXhtml(true);
     }
     $decoda_manager = new DecodaManager($code, $current_filter['filters'], $current_filter['hooks'], $current_filter['whitelist']);
     return $decoda_manager->getResult()->parse();
 }
示例#2
0
 /**
  * @dataProvider getMessage
  */
 public function testMessage($defaultLocale, $locale, $value, $expect)
 {
     if ($expect instanceof \Exception) {
         $this->setExpectedException(get_class($expect), $expect->getMessage());
     }
     $result = new Decoda();
     $result->setMessages(array('en-us' => array('foo' => 'foo-en-us'), 'fr-be' => array('foo' => 'foo-fr-be'), 'fr' => array('foo' => 'foo-fr'), 'en' => array('foo' => 'foo-en')));
     if (null !== $defaultLocale) {
         $result->setDefaultLocale($defaultLocale);
     }
     if (null !== $locale) {
         $result->setLocale($locale);
     }
     $this->assertEquals($expect, $result->message($value));
 }
 /**
  * Gets a DecodaPhpEngine.
  *
  * @return DecodaPhpEngine
  */
 private function getPhpEngine()
 {
     if (null === $this->phpEngine) {
         $this->phpEngine = new PhpEngine();
         foreach ($this->options['templates'] as $template) {
             // Use bundle hierachy
             $paths = $this->locator->locate($template['path'], null, false);
             foreach ($paths as $path) {
                 $this->phpEngine->addPath($path);
             }
         }
         $decoda = new Decoda();
         $defaultPaths = $decoda->getEngine()->getPaths();
         foreach ($defaultPaths as $path) {
             $this->phpEngine->addPath($path);
         }
     }
     return $this->phpEngine;
 }
 /**
  * @param Decoda $code
  * @param array $whitelist
  * @return \Decoda
  */
 protected function apply_whitelist(Decoda $code, array $whitelist)
 {
     return $code->whitelist($whitelist);
 }
示例#5
0
 /**
  * Gets a pre-configured decoda.
  *
  * @return Decoda
  */
 private function getPreConfiguredDecoda()
 {
     if (null !== $this->preConfiguredDecoda) {
         return $this->preConfiguredDecoda;
     }
     $decoda = new Decoda();
     if (null !== $this->options['messages']) {
         $decoda->addMessages($this->messageLoader->load($this->options['messages']));
     }
     $decoda->setEngine($this->getPhpEngine());
     $decoda->setDefaultLocale($this->options['default_locale']);
     $decoda->setLocale($this->getLocale());
     $decoda->defaults();
     $this->preConfiguredDecoda = $decoda;
     return $decoda;
 }
 /**
  * @param mixed $entries All entries.
  * @param mixed $without Entries to be removed.
  * @return array Remaining entries of {@code $value} after removing the entries of {@code $without}.
  */
 public function BBCode($value, $locale = null)
 {
     if (!is_string($value)) {
         throw new \Twig_Error_Runtime('The filter can be applied to strings only.');
     }
     $code = new Decoda($value);
     if (empty($locale)) {
         // apply locale from the session
         if ('default' == $this->locale) {
             $code->setLocale($this->container->get('session')->getLocale());
             // apply locale defined in the configuration
         } else {
             // apply locale from the template
             $code->setLocale($this->locale);
         }
     } else {
         $code->setLocale($locale);
     }
     if (true === $this->xhtml) {
         $code->setXhtml(true);
     }
     if ($this->default == 'enabled') {
         $code->addFilter(new \DefaultFilter());
     }
     if ($this->block == 'enabled') {
         $code->addFilter(new \BlockFilter());
     }
     if ($this->code == 'enabled') {
         $code->addFilter(new \CodeFilter());
     }
     if ($this->email == 'enabled') {
         $code->addFilter(new \EmailFilter());
     }
     if ($this->image == 'enabled') {
         $code->addFilter(new \ImageFilter());
     }
     if ($this->list == 'enabled') {
         $code->addFilter(new \ListFilter());
     }
     if ($this->quote == 'enabled') {
         $code->addFilter(new \QuoteFilter());
     }
     if ($this->text == 'enabled') {
         $code->addFilter(new \TextFilter());
     }
     if ($this->url == 'enabled') {
         $code->addFilter(new \UrlFilter());
     }
     if ($this->video == 'enabled') {
         $code->addFilter(new \VideoFilter());
     }
     return $code->parse(true);
 }