1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
<?php
namespace MyProject\TestBundle\Twig\Extension;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
class MyProjectTwigExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
'number' => new \Twig_Filter_Method($this, 'numberFilter'),
'match' => new \Twig_Filter_Method($this, 'matchFilter'),
'hasError' => new \Twig_Filter_Method($this, 'hasErrorFilter'),
);
}
public function numberFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
{
$number = number_format($number, $decimals, $decPoint, $thousandsSep);
$number = $number . ' $';
return $number;
}
public function matchFilter($route, $pattern = '')
{
$pattern = "/$pattern/";
if(preg_match($pattern, $route))
{
return true;
}
else
{
return false;
}
return null;
}
public function hasErrorFilter(\Symfony\Component\Form\FormView $formView)
{
if($formView->has('errors'))
{
if(!$formView->get('valid'))
{
return true;
}
}
if($formView->hasChildren())
{
foreach($formView->getChildren() as $subFormView)
{
if($subFormView->has('errors'))
{
if(!$subFormView->get('valid'))
{
return true;
}
}
}
}
return false;
}
public function getName()
{
return 'myproject_twig_extension';
}
} |
Partager