@@ -14,7 +14,9 @@ discard block |
||
14 | 14 | */ |
15 | 15 | public function __construct() |
16 | 16 | { |
17 | - if (!extension_loaded('pcre')) throw new \Exception("The Twig PCRE extension requires PHP extension 'pcre' (see http://www.php.net/pcre)."); |
|
17 | + if (!extension_loaded('pcre')) { |
|
18 | + throw new \Exception("The Twig PCRE extension requires PHP extension 'pcre' (see http://www.php.net/pcre)."); |
|
19 | + } |
|
18 | 20 | } |
19 | 21 | |
20 | 22 | |
@@ -43,7 +45,9 @@ discard block |
||
43 | 45 | */ |
44 | 46 | protected function assertNoEval($pattern) |
45 | 47 | { |
46 | - if (preg_match('/(.).*\1(.+)$/', trim($pattern), $match) && strpos($match[1], 'e') !== false) throw new \Exception("Using the eval modifier for regular expressions is not allowed"); |
|
48 | + if (preg_match('/(.).*\1(.+)$/', trim($pattern), $match) && strpos($match[1], 'e') !== false) { |
|
49 | + throw new \Exception("Using the eval modifier for regular expressions is not allowed"); |
|
50 | + } |
|
47 | 51 | } |
48 | 52 | |
49 | 53 | |
@@ -56,7 +60,9 @@ discard block |
||
56 | 60 | */ |
57 | 61 | public function quote($value, $delimiter = '/') |
58 | 62 | { |
59 | - if (!isset($value)) return null; |
|
63 | + if (!isset($value)) { |
|
64 | + return null; |
|
65 | + } |
|
60 | 66 | return preg_quote($value, $delimiter); |
61 | 67 | } |
62 | 68 | |
@@ -71,7 +77,9 @@ discard block |
||
71 | 77 | { |
72 | 78 | $this->assertNoEval($pattern); |
73 | 79 | |
74 | - if (!isset($value)) return null; |
|
80 | + if (!isset($value)) { |
|
81 | + return null; |
|
82 | + } |
|
75 | 83 | return preg_match($pattern, $value); |
76 | 84 | } |
77 | 85 | |
@@ -86,8 +94,12 @@ discard block |
||
86 | 94 | { |
87 | 95 | $this->assertNoEval($pattern); |
88 | 96 | |
89 | - if (!isset($value)) return null; |
|
90 | - if (!preg_match($pattern, $value, $matches)) return null; |
|
97 | + if (!isset($value)) { |
|
98 | + return null; |
|
99 | + } |
|
100 | + if (!preg_match($pattern, $value, $matches)) { |
|
101 | + return null; |
|
102 | + } |
|
91 | 103 | return isset($matches[$group]) ? $matches[$group] : null; |
92 | 104 | } |
93 | 105 | |
@@ -102,8 +114,12 @@ discard block |
||
102 | 114 | { |
103 | 115 | $this->assertNoEval($pattern); |
104 | 116 | |
105 | - if (!isset($value)) return null; |
|
106 | - if (!preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER)) return array(); |
|
117 | + if (!isset($value)) { |
|
118 | + return null; |
|
119 | + } |
|
120 | + if (!preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER)) { |
|
121 | + return array(); |
|
122 | + } |
|
107 | 123 | return isset($matches[$group]) ? $matches[$group] : array(); |
108 | 124 | } |
109 | 125 | |
@@ -119,9 +135,13 @@ discard block |
||
119 | 135 | { |
120 | 136 | $this->assertNoEval($pattern); |
121 | 137 | |
122 | - if (!isset($values)) return null; |
|
138 | + if (!isset($values)) { |
|
139 | + return null; |
|
140 | + } |
|
123 | 141 | |
124 | - if (is_string($flags)) $flags = $flags == 'invert' ? PREG_GREP_INVERT : 0; |
|
142 | + if (is_string($flags)) { |
|
143 | + $flags = $flags == 'invert' ? PREG_GREP_INVERT : 0; |
|
144 | + } |
|
125 | 145 | return preg_grep($pattern, $values, $flags); |
126 | 146 | } |
127 | 147 | |
@@ -138,7 +158,9 @@ discard block |
||
138 | 158 | { |
139 | 159 | $this->assertNoEval($pattern); |
140 | 160 | |
141 | - if (!isset($value)) return null; |
|
161 | + if (!isset($value)) { |
|
162 | + return null; |
|
163 | + } |
|
142 | 164 | return preg_replace($pattern, $replacement, $value, $limit); |
143 | 165 | } |
144 | 166 | |
@@ -155,7 +177,9 @@ discard block |
||
155 | 177 | { |
156 | 178 | $this->assertNoEval($pattern); |
157 | 179 | |
158 | - if (!isset($value)) return null; |
|
180 | + if (!isset($value)) { |
|
181 | + return null; |
|
182 | + } |
|
159 | 183 | return preg_filter($pattern, $replacement, $value, $limit); |
160 | 184 | } |
161 | 185 | |
@@ -170,7 +194,9 @@ discard block |
||
170 | 194 | { |
171 | 195 | $this->assertNoEval($pattern); |
172 | 196 | |
173 | - if (!isset($value)) return null; |
|
197 | + if (!isset($value)) { |
|
198 | + return null; |
|
199 | + } |
|
174 | 200 | return preg_split($pattern, $value); |
175 | 201 | } |
176 | 202 |
@@ -43,7 +43,9 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public function sum($array) |
45 | 45 | { |
46 | - if (!isset($array)) return null; |
|
46 | + if (!isset($array)) { |
|
47 | + return null; |
|
48 | + } |
|
47 | 49 | return array_sum((array)$array); |
48 | 50 | } |
49 | 51 | |
@@ -55,7 +57,9 @@ discard block |
||
55 | 57 | */ |
56 | 58 | public function product($array) |
57 | 59 | { |
58 | - if (!isset($array)) return null; |
|
60 | + if (!isset($array)) { |
|
61 | + return null; |
|
62 | + } |
|
59 | 63 | return array_product((array)$array); |
60 | 64 | } |
61 | 65 | |
@@ -67,7 +71,9 @@ discard block |
||
67 | 71 | */ |
68 | 72 | public function values($array) |
69 | 73 | { |
70 | - if (!isset($array)) return null; |
|
74 | + if (!isset($array)) { |
|
75 | + return null; |
|
76 | + } |
|
71 | 77 | return array_values((array)$array); |
72 | 78 | } |
73 | 79 | |
@@ -91,13 +97,19 @@ discard block |
||
91 | 97 | */ |
92 | 98 | public function HTMLAttributes($array) |
93 | 99 | { |
94 | - if (!isset($array)) return null; |
|
100 | + if (!isset($array)) { |
|
101 | + return null; |
|
102 | + } |
|
95 | 103 | |
96 | 104 | $str = ""; |
97 | 105 | foreach ($array as $key=>$value) { |
98 | - if (!isset($value) || $value === false) continue; |
|
106 | + if (!isset($value) || $value === false) { |
|
107 | + continue; |
|
108 | + } |
|
99 | 109 | |
100 | - if ($value === true) $value = $key; |
|
110 | + if ($value === true) { |
|
111 | + $value = $key; |
|
112 | + } |
|
101 | 113 | $str .= ' ' . $key . '="' . addcslashes($value, '"') . '"'; |
102 | 114 | } |
103 | 115 | return trim($str); |
@@ -31,7 +31,9 @@ discard block |
||
31 | 31 | */ |
32 | 32 | public function paragraph($value) |
33 | 33 | { |
34 | - if (!isset($value)) return null; |
|
34 | + if (!isset($value)) { |
|
35 | + return null; |
|
36 | + } |
|
35 | 37 | return '<p>' . preg_replace(array('~\n(\s*)\n\s*~', '~(?<!</p>)\n\s*~'), array("</p>\n\$1<p>", "<br>\n"), trim($value)) . '</p>'; |
36 | 38 | } |
37 | 39 | |
@@ -44,7 +46,9 @@ discard block |
||
44 | 46 | */ |
45 | 47 | public function line($value, $line=1) |
46 | 48 | { |
47 | - if (!isset($value)) return null; |
|
49 | + if (!isset($value)) { |
|
50 | + return null; |
|
51 | + } |
|
48 | 52 | |
49 | 53 | $lines = explode("\n", $value); |
50 | 54 | return isset($lines[$line-1]) ? $lines[$line-1] : null; |
@@ -60,7 +64,9 @@ discard block |
||
60 | 64 | */ |
61 | 65 | public function less($value, $replace = '...', $break = '<!-- pagebreak -->') |
62 | 66 | { |
63 | - if (!isset($value)) return null; |
|
67 | + if (!isset($value)) { |
|
68 | + return null; |
|
69 | + } |
|
64 | 70 | |
65 | 71 | $pos = stripos($value, $break); |
66 | 72 | return $pos === false ? $value : substr($value, 0, $pos) . $replace; |
@@ -76,7 +82,9 @@ discard block |
||
76 | 82 | */ |
77 | 83 | public function truncate($value, $length, $replace = '...') |
78 | 84 | { |
79 | - if (!isset($value)) return null; |
|
85 | + if (!isset($value)) { |
|
86 | + return null; |
|
87 | + } |
|
80 | 88 | return strlen($value) <= $length ? $value : substr($value, 0, $length - strip_tags($replace)) . $replace; |
81 | 89 | } |
82 | 90 | |
@@ -91,7 +99,9 @@ discard block |
||
91 | 99 | */ |
92 | 100 | public function linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal') |
93 | 101 | { |
94 | - if (!isset($value)) return null; |
|
102 | + if (!isset($value)) { |
|
103 | + return null; |
|
104 | + } |
|
95 | 105 | |
96 | 106 | // Link attributes |
97 | 107 | $attr = ''; |
@@ -108,12 +118,17 @@ discard block |
||
108 | 118 | foreach ((array)$protocols as $protocol) { |
109 | 119 | switch ($protocol) { |
110 | 120 | case 'http': |
111 | - case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s<>]+)|(www\.[^\s<>]+?\.[^\s<>]+))(?<![\.,:;\?!\'"\|])~i' : '~(?:(https?)://([^\s<>]+)|([^\s<>]+?\.[^\s<>]+)(?<![\.,:]))~i', function ($match) use ($protocol, &$links, $attr) { if ($match[1]) $protocol = $match[1]; $link = $match[2] ?: $match[3]; return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">' . rtrim($link, '/') . '</a>') . '>'; }, $value); break; |
|
121 | + case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s<>]+)|(www\.[^\s<>]+?\.[^\s<>]+))(?<![\.,:;\?!\'"\|])~i' : '~(?:(https?)://([^\s<>]+)|([^\s<>]+?\.[^\s<>]+)(?<![\.,:]))~i', function ($match) use ($protocol, &$links, $attr) { if ($match[1]) { |
|
122 | + $protocol = $match[1]; |
|
123 | + } |
|
124 | + $link = $match[2] ?: $match[3]; return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">' . rtrim($link, '/') . '</a>') . '>'; }, $value); break; |
|
112 | 125 | case 'mail': $value = preg_replace_callback('~([^\s<>]+?@[^\s<>]+?\.[^\s<>]+)(?<![\.,:;\?!\'"\|])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break; |
113 | 126 | case 'twitter': $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="https://twitter.com/' . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . '">' . $match[0] . '</a>') . '>'; }, $value); break; |
114 | 127 | |
115 | 128 | default: |
116 | - if (strpos($protocol, ':') === false) $protocol .= in_array($protocol, array('ftp', 'tftp', 'ssh', 'scp')) ? '://' : ':'; |
|
129 | + if (strpos($protocol, ':') === false) { |
|
130 | + $protocol .= in_array($protocol, array('ftp', 'tftp', 'ssh', 'scp')) ? '://' : ':'; |
|
131 | + } |
|
117 | 132 | $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '([^\s<>]+?)(?<![\.,:;\?!\'"\|])~i' : '~([^\s<>]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break; |
118 | 133 | } |
119 | 134 | } |
@@ -14,7 +14,9 @@ discard block |
||
14 | 14 | */ |
15 | 15 | public function __construct() |
16 | 16 | { |
17 | - if (!extension_loaded('intl')) throw new \Exception("Jasny's Date Twig extension requires PHP extension 'intl' (see http://www.php.net/intl)."); |
|
17 | + if (!extension_loaded('intl')) { |
|
18 | + throw new \Exception("Jasny's Date Twig extension requires PHP extension 'intl' (see http://www.php.net/intl)."); |
|
19 | + } |
|
18 | 20 | } |
19 | 21 | |
20 | 22 | |
@@ -88,11 +90,16 @@ discard block |
||
88 | 90 | */ |
89 | 91 | public function localDate($date, $format=null, $calendar='gregorian') |
90 | 92 | { |
91 | - if (!isset($date)) return null; |
|
93 | + if (!isset($date)) { |
|
94 | + return null; |
|
95 | + } |
|
92 | 96 | |
93 | 97 | if ($date instanceof \DateTime); |
94 | - elseif (is_int($date)) $date = \DateTime::createFromFormat('U', $date); |
|
95 | - else $date = new \DateTime((string)$date); |
|
98 | + elseif (is_int($date)) { |
|
99 | + $date = \DateTime::createFromFormat('U', $date); |
|
100 | + } else { |
|
101 | + $date = new \DateTime((string)$date); |
|
102 | + } |
|
96 | 103 | |
97 | 104 | $calendar = $calendar == 'traditional' ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN; |
98 | 105 | list($format, $pattern) = $this->getFormat($format, $calendar); |
@@ -111,11 +118,16 @@ discard block |
||
111 | 118 | */ |
112 | 119 | public function localTime($date, $format='short', $calendar='gregorian') |
113 | 120 | { |
114 | - if (!isset($date)) return null; |
|
121 | + if (!isset($date)) { |
|
122 | + return null; |
|
123 | + } |
|
115 | 124 | |
116 | 125 | if ($date instanceof \DateTime); |
117 | - elseif (is_int($date)) $date = \DateTime::createFromFormat('U', $date); |
|
118 | - else $date = new \DateTime((string)$date); |
|
126 | + elseif (is_int($date)) { |
|
127 | + $date = \DateTime::createFromFormat('U', $date); |
|
128 | + } else { |
|
129 | + $date = new \DateTime((string)$date); |
|
130 | + } |
|
119 | 131 | |
120 | 132 | $calendar = $calendar == 'traditional' ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN; |
121 | 133 | list($format, $pattern) = $this->getFormat($format, $calendar); |
@@ -134,11 +146,16 @@ discard block |
||
134 | 146 | */ |
135 | 147 | public function localDateTime($date, $format=null, $calendar='gregorian') |
136 | 148 | { |
137 | - if (!isset($date)) return null; |
|
149 | + if (!isset($date)) { |
|
150 | + return null; |
|
151 | + } |
|
138 | 152 | |
139 | 153 | if ($date instanceof \DateTime); |
140 | - elseif (is_int($date)) $date = \DateTime::createFromFormat('U', $date); |
|
141 | - else $date = new \DateTime((string)$date); |
|
154 | + elseif (is_int($date)) { |
|
155 | + $date = \DateTime::createFromFormat('U', $date); |
|
156 | + } else { |
|
157 | + $date = new \DateTime((string)$date); |
|
158 | + } |
|
142 | 159 | |
143 | 160 | $calendar = $calendar == 'traditional' ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN; |
144 | 161 | |
@@ -165,23 +182,33 @@ discard block |
||
165 | 182 | */ |
166 | 183 | protected function splitDuration($seconds, $max) |
167 | 184 | { |
168 | - if ($max < 1 || $seconds < 60) return array($seconds); |
|
185 | + if ($max < 1 || $seconds < 60) { |
|
186 | + return array($seconds); |
|
187 | + } |
|
169 | 188 | |
170 | 189 | $minutes = floor($seconds / 60); |
171 | 190 | $seconds = $seconds % 60; |
172 | - if ($max < 2 || $minutes < 60) return array($seconds, $minutes); |
|
191 | + if ($max < 2 || $minutes < 60) { |
|
192 | + return array($seconds, $minutes); |
|
193 | + } |
|
173 | 194 | |
174 | 195 | $hours = floor($minutes / 60); |
175 | 196 | $minutes = $minutes % 60; |
176 | - if ($max < 3 || $hours < 24) return array($seconds, $minutes, $hours); |
|
197 | + if ($max < 3 || $hours < 24) { |
|
198 | + return array($seconds, $minutes, $hours); |
|
199 | + } |
|
177 | 200 | |
178 | 201 | $days = floor($hours / 24); |
179 | 202 | $hours = $hours % 24; |
180 | - if ($max < 4 || $days < 7) return array($seconds, $minutes, $hours, $days); |
|
203 | + if ($max < 4 || $days < 7) { |
|
204 | + return array($seconds, $minutes, $hours, $days); |
|
205 | + } |
|
181 | 206 | |
182 | 207 | $weeks = floor($days / 7); |
183 | 208 | $days = $days % 7; |
184 | - if ($max < 5 || $weeks < 52) return array($seconds, $minutes, $hours, $days, $weeks); |
|
209 | + if ($max < 5 || $weeks < 52) { |
|
210 | + return array($seconds, $minutes, $hours, $days, $weeks); |
|
211 | + } |
|
185 | 212 | |
186 | 213 | $years = floor($weeks / 52); |
187 | 214 | $weeks = $weeks % 52; |
@@ -205,12 +232,24 @@ discard block |
||
205 | 232 | $this->splitDuration($seconds, count($units)-1) + array_fill(0, 6, null); |
206 | 233 | |
207 | 234 | $duration = ''; |
208 | - if (isset($years) && isset($units[5])) $duration .= $seperator . $years . $units[5]; |
|
209 | - if (isset($weeks) && isset($units[4])) $duration .= $seperator . $weeks . $units[4]; |
|
210 | - if (isset($days) && isset($units[3])) $duration .= $seperator . $days . $units[3]; |
|
211 | - if (isset($hours) && isset($units[2])) $duration .= $seperator . $hours . $units[2]; |
|
212 | - if (isset($minutes) && isset($units[1])) $duration .= $seperator . $minutes . $units[1]; |
|
213 | - if (isset($seconds) && isset($units[0])) $duration .= $seperator . $seconds . $units[0]; |
|
235 | + if (isset($years) && isset($units[5])) { |
|
236 | + $duration .= $seperator . $years . $units[5]; |
|
237 | + } |
|
238 | + if (isset($weeks) && isset($units[4])) { |
|
239 | + $duration .= $seperator . $weeks . $units[4]; |
|
240 | + } |
|
241 | + if (isset($days) && isset($units[3])) { |
|
242 | + $duration .= $seperator . $days . $units[3]; |
|
243 | + } |
|
244 | + if (isset($hours) && isset($units[2])) { |
|
245 | + $duration .= $seperator . $hours . $units[2]; |
|
246 | + } |
|
247 | + if (isset($minutes) && isset($units[1])) { |
|
248 | + $duration .= $seperator . $minutes . $units[1]; |
|
249 | + } |
|
250 | + if (isset($seconds) && isset($units[0])) { |
|
251 | + $duration .= $seperator . $seconds . $units[0]; |
|
252 | + } |
|
214 | 253 | |
215 | 254 | return trim($duration, $seperator); |
216 | 255 | } |
@@ -223,7 +262,9 @@ discard block |
||
223 | 262 | */ |
224 | 263 | public function age($date) |
225 | 264 | { |
226 | - if (!$date instanceof \DateTime) $date = new \DateTime($date); |
|
265 | + if (!$date instanceof \DateTime) { |
|
266 | + $date = new \DateTime($date); |
|
267 | + } |
|
227 | 268 | return $date->diff(new \DateTime())->format('%y'); |
228 | 269 | } |
229 | 270 | } |