|
1
|
|
|
<?php |
|
2
|
|
|
namespace Vivait\BootstrapBundle\Twig; |
|
3
|
|
|
|
|
4
|
|
|
use \Twig_Filter_Function; |
|
5
|
|
|
use Symfony\Component\Intl\NumberFormatter\NumberFormatter; |
|
6
|
|
|
|
|
7
|
|
|
class TwigFilterExtension extends \Twig_Extension { |
|
8
|
|
|
public function getName() { |
|
9
|
|
|
return 'twig_extension'; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
public function getFilters() { |
|
14
|
|
|
return array( |
|
15
|
|
|
new \Twig_SimpleFilter('yesno', array($this, 'yesnoFilter')), |
|
16
|
|
|
new \Twig_SimpleFilter('yesnoicon', array($this, 'yesnoiconFilter')), |
|
17
|
|
|
new \Twig_SimpleFilter('gender', array($this, 'genderFilter')), |
|
18
|
|
|
new \Twig_SimpleFilter('money', array($this, 'moneyFilter')), |
|
19
|
|
|
new \Twig_SimpleFilter('printr', array($this, 'printrFilter')), |
|
20
|
|
|
new \Twig_SimpleFilter('file_exists', array($this, 'file_exists')) |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function yesnoFilter($boolean) { |
|
25
|
|
|
return $boolean?'Yes':'No'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function yesnoiconFilter($boolean) { |
|
29
|
|
|
return $boolean?'<i class="text-success glyphicon glyphicon-ok"></i>':'<i class="text-danger glyphicon glyphicon-remove"></i>'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function genderFilter($sex) { |
|
33
|
|
|
if($sex=='M') { |
|
34
|
|
|
return 'Male'; |
|
35
|
|
|
} elseif($sex=='F') { |
|
36
|
|
|
return 'Female'; |
|
37
|
|
|
} else { |
|
38
|
|
|
return 'Unknown'; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function moneyFilter($float,$currency = NULL) { |
|
|
|
|
|
|
43
|
|
|
$nf = new NumberFormatter('en', NumberFormatter::CURRENCY); |
|
44
|
|
|
if($currency) { |
|
45
|
|
|
return $nf->formatCurrency($float,$currency); |
|
46
|
|
|
} else { |
|
47
|
|
|
return number_format($float,2,'.',','); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function file_exists($file) { |
|
54
|
|
|
return file_exists($file); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
?> |
|
|
|
|
|
|
59
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.