|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Application\Formater\Twig; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Application\Extension\ExtensionService; |
|
5
|
|
|
use Hal\Application\Rule\Validator; |
|
6
|
|
|
|
|
7
|
|
|
class FormatingExtension extends \Twig_Extension |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Validator |
|
11
|
|
|
*/ |
|
12
|
|
|
private $validator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Validator |
|
16
|
|
|
* |
|
17
|
|
|
* @param Validator $validator |
|
18
|
|
|
*/ |
|
19
|
|
|
function __construct(Validator $validator) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
$this->validator = $validator; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @inheritdoc |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getFilters() |
|
28
|
|
|
{ |
|
29
|
|
|
return array( |
|
30
|
|
|
new \Twig_SimpleFilter('textify', array($this, 'textify')) |
|
31
|
|
|
, new \Twig_SimpleFilter('rule', array($this, 'rule')) |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getFunctions() |
|
39
|
|
|
{ |
|
40
|
|
|
return array( |
|
41
|
|
|
new \Twig_SimpleFunction('extensions_menu', array($this, 'extensionsMenu'), array('is_safe' => array('html'))) |
|
42
|
|
|
, new \Twig_SimpleFunction('extensions_js', array($this, 'extensionsJs'), array('is_safe' => array('html'))) |
|
43
|
|
|
, new \Twig_SimpleFunction('extensions_content', array($this, 'extensionsContent'), array('is_safe' => array('html'))) |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* String as readable text |
|
49
|
|
|
* |
|
50
|
|
|
* @param $v |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function textify($v) |
|
54
|
|
|
{ |
|
55
|
|
|
return ucfirst(preg_replace( '/([a-z0-9])([A-Z])/', "$1 $2", $v )); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check value according rule |
|
60
|
|
|
* |
|
61
|
|
|
* @param $key |
|
62
|
|
|
* @param $v |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function rule($v, $key) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->validator->validate($key, $v); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param ExtensionService $extensions |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function extensionsMenu(ExtensionService $extensions) |
|
75
|
|
|
{ |
|
76
|
|
|
$html = ''; |
|
77
|
|
|
foreach($extensions->getRepository()->all() as $extension) { |
|
78
|
|
|
$helper = $extension->getReporterHtmlSummary(); |
|
79
|
|
|
if(!$helper) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
foreach($helper->getMenus() as $name => $label) { |
|
83
|
|
|
$html .= sprintf('<li id="link-%s"><a>%s</a></li>', $name, $label); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return $html; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param ExtensionService $extensions |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function extensionsJs(ExtensionService $extensions) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$html = ''; |
|
96
|
|
|
foreach($extensions->getRepository()->all() as $extension) { |
|
97
|
|
|
$helper = $extension->getReporterHtmlSummary(); |
|
98
|
|
|
if(!$helper) { |
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
$html .= $helper->renderJs(); |
|
102
|
|
|
} |
|
103
|
|
|
return $html; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param ExtensionService $extensions |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public function extensionsContent(ExtensionService $extensions) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$html = ''; |
|
113
|
|
|
foreach($extensions->getRepository()->all() as $extension) { |
|
114
|
|
|
$helper = $extension->getReporterHtmlSummary(); |
|
115
|
|
|
if(!$helper) { |
|
116
|
|
|
continue; |
|
117
|
|
|
} |
|
118
|
|
|
$html .= $helper->renderHtml(); |
|
119
|
|
|
} |
|
120
|
|
|
return $html; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inherit |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getName() |
|
127
|
|
|
{ |
|
128
|
|
|
return 'hal_formating_extension'; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.