1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use libphonenumber\PhoneNumber; |
6
|
|
|
use libphonenumber\PhoneNumberFormat; |
7
|
|
|
use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberFormatHelper; |
8
|
|
|
use Sylius\Bundle\SettingsBundle\Templating\Helper\SettingsHelper; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
11
|
|
|
|
12
|
|
|
class Generic extends \Twig_Extension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ContainerInterface |
16
|
|
|
*/ |
17
|
|
|
protected $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var PhoneNumberFormatHelper |
21
|
|
|
*/ |
22
|
|
|
protected $phoneNumberHelper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SettingsHelper |
26
|
|
|
*/ |
27
|
|
|
protected $settingsHelper; |
28
|
|
|
|
29
|
|
|
public function __construct(ContainerInterface $container) |
30
|
|
|
{ |
31
|
|
|
$this->container = $container; |
32
|
|
|
$this->phoneNumberHelper = $container->get('misd_phone_number.templating.helper.format'); |
33
|
|
|
$this->settingsHelper = $container->get('sylius.templating.helper.settings'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function getGlobals() |
40
|
|
|
{ |
41
|
|
|
return array( |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function getFunctions() |
49
|
|
|
{ |
50
|
|
|
$self = array('is_safe' => array('all')); |
51
|
|
|
|
52
|
|
|
return array( |
53
|
|
|
new \Twig_SimpleFunction('d', '\Kint::dump', $self), |
54
|
|
|
new \Twig_SimpleFunction('e', array($this, 'dumpExit'), $self), |
55
|
|
|
new \Twig_SimpleFunction('is_string', 'is_string'), |
56
|
|
|
new \Twig_SimpleFunction('ui_random_string', array($this, 'getRandomString'), $self), |
57
|
|
|
new \Twig_SimpleFunction('ui_percentage', array($this, 'calculatePercent')), |
58
|
|
|
new \Twig_SimpleFunction('ui_setting', array($this, 'getSettingsParameter')), |
59
|
|
|
new \Twig_SimpleFunction('ui_settings', array($this->settingsHelper, 'getSettings')), |
60
|
|
|
new \Twig_SimpleFunction('ui_param', array($this->container, 'getParameter')), |
61
|
|
|
new \Twig_SimpleFunction('ui_param_has', array($this->container, 'hasParameter')), |
62
|
|
|
new \Twig_SimpleFunction('ui_has', array($this->container, 'has')), |
63
|
|
|
new \Twig_SimpleFunction('ui_obfuscated_email', array($this, 'getObfuscatedEmail')), |
64
|
|
|
new \Twig_SimpleFunction('ui_property', array($this, 'getContextValue')), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getFilters() |
69
|
|
|
{ |
70
|
|
|
//$self = array('is_safe' => array('all')); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return array( |
73
|
|
|
new \Twig_SimpleFilter('phone_number', array($this, 'getPhoneFormat')), |
74
|
|
|
new \Twig_SimpleFilter('is_match', array($this, 'match')), |
75
|
|
|
new \Twig_SimpleFilter('ui_no_space', array($this, 'cleanWhiteSpace')), |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $key |
81
|
|
|
* @param string $default |
82
|
|
|
* |
83
|
|
|
* @return null|mixed |
84
|
|
|
*/ |
85
|
|
|
public function getSettingsParameter($key, $default = null) |
86
|
|
|
{ |
87
|
|
|
list($alias, $key) = explode('.', $key); |
88
|
|
|
|
89
|
|
|
$settings = $this->container->get('sylius.templating.helper.settings') |
90
|
|
|
->getSettings($alias) |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
if (array_key_exists($key, $settings)) { |
94
|
|
|
return $settings[$key]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $default; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getRandomString($length = 8) |
101
|
|
|
{ |
102
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
103
|
|
|
$randomString = ''; |
104
|
|
|
|
105
|
|
|
for ($i = 0; $i < $length; $i++) { |
106
|
|
|
$randomString .= $characters[rand(0, strlen($characters) - 1)]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $randomString; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param PhoneNumber $phoneNumber |
114
|
|
|
* @param int $defaultRegion |
115
|
|
|
* |
116
|
|
|
* @return null|string |
117
|
|
|
*/ |
118
|
|
|
public function getPhoneFormat( |
119
|
|
|
PhoneNumber $phoneNumber = null, |
120
|
|
|
$defaultRegion = PhoneNumberFormat::NATIONAL |
121
|
|
|
) { |
122
|
|
|
if (empty($phoneNumber)) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->phoneNumberHelper->format($phoneNumber, $defaultRegion); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $total |
131
|
|
|
* @param $pie |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function calculatePercent($total, $pie) |
136
|
|
|
{ |
137
|
|
|
if (empty($total)) { |
138
|
|
|
return '0%'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$total = ($pie / $total) * 100; |
142
|
|
|
|
143
|
|
|
if (is_float($total)) { |
144
|
|
|
return number_format($total, 1).'%'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $total.'%'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $email |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getObfuscatedEmail($email) |
156
|
|
|
{ |
157
|
|
|
if (false !== $pos = strpos($email, '@')) { |
158
|
|
|
$email = '...'.substr($email, $pos); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $email; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $context |
166
|
|
|
* @param $path |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function getContextValue($context, $path) |
171
|
|
|
{ |
172
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
173
|
|
|
|
174
|
|
|
return $accessor->getValue($context, $path); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param $string |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function cleanWhiteSpace($string) |
183
|
|
|
{ |
184
|
|
|
return preg_replace('/ /', '', $string); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function match($subject, $pattern) |
188
|
|
|
{ |
189
|
|
|
return preg_match($pattern, $subject); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function dumpExit($val) |
193
|
|
|
{ |
194
|
|
|
\Kint::dump($val); |
195
|
|
|
exit; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @api |
200
|
|
|
*/ |
201
|
|
|
public function getName() |
202
|
|
|
{ |
203
|
|
|
return 'ui_generic'; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.