vendor/fp/jsformvalidator-bundle/src/Twig/Extension/JsFormValidatorTwigExtension.php line 71

Open in your IDE?
  1. <?php
  2. namespace Fp\JsFormValidatorBundle\Twig\Extension;
  3. use Fp\JsFormValidatorBundle\Factory\JsFormValidatorFactory;
  4. use Symfony\Component\Form\FormView;
  5. /**
  6.  * Class JsFormValidatorTwigExtension
  7.  *
  8.  * @package Fp\JsFormValidatorBundle\Twig\Extension
  9.  */
  10. class JsFormValidatorTwigExtension extends \Twig_Extension
  11. {
  12.     /**
  13.      * @var JsFormValidatorFactory
  14.      */
  15.     protected $factory;
  16.     /**
  17.      * @return JsFormValidatorFactory
  18.      * @codeCoverageIgnore
  19.      */
  20.     protected function getFactory()
  21.     {
  22.         return $this->factory;
  23.     }
  24.     /**
  25.      * @param JsFormValidatorFactory $factory
  26.      *
  27.      * @codeCoverageIgnore
  28.      */
  29.     public function __construct(JsFormValidatorFactory $factory)
  30.     {
  31.         $this->factory $factory;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function getFunctions()
  37.     {
  38.         return array(
  39.             new \Twig_SimpleFunction('init_js_validation', array($this'getJsValidator'), array(
  40.                 'is_safe' => array('html')
  41.             )),
  42.             new \Twig_SimpleFunction('js_validator_config', array($this'getConfig'), array(
  43.                 'is_safe' => array('html')
  44.             )),
  45.         );
  46.     }
  47.     public function getConfig()
  48.     {
  49.         return $this->getFactory()->getJsConfigString();
  50.     }
  51.     /**
  52.      * @param null|string|FormView $form
  53.      * @param bool                 $onLoad
  54.      * @param bool                 $wrapped
  55.      *
  56.      * @return string
  57.      */
  58.     public function getJsValidator($form null$onLoad true$wrapped true)
  59.     {
  60.         if ($form instanceof FormView) {
  61.             $form $form->vars['name'];
  62.         }
  63.         $jsModels $this->getFactory()->getJsValidatorString($form$onLoad);
  64.         if ($wrapped) {
  65.             $jsModels '<script type="text/javascript">' $jsModels '</script>';
  66.         }
  67.         return $jsModels;
  68.     }
  69.     /**
  70.      * Returns the name of the extension.
  71.      *
  72.      * @return string The extension name
  73.      * @codeCoverageIgnore
  74.      */
  75.     public function getName()
  76.     {
  77.         return 'fp_js_form_validator';
  78.     }
  79. }