1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Twig; |
4
|
|
|
|
5
|
|
|
use Twig\Extension\AbstractExtension; |
6
|
|
|
use Twig\TwigFilter; |
7
|
|
|
use Twig\TwigFunction; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AppExtension |
11
|
|
|
* |
12
|
|
|
* @package Oc\Twig |
13
|
|
|
*/ |
14
|
|
|
class AppExtension extends AbstractExtension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return TwigFilter[] |
18
|
|
|
*/ |
19
|
|
|
public function getFilters() |
20
|
|
|
: array |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
new TwigFilter('ocFilterD', [$this, 'oc_Filter_D']), |
24
|
|
|
new TwigFilter('ocFilterT', [$this, 'oc_Filter_T']), |
25
|
|
|
new TwigFilter('rot13', [$this, 'oc_Filter_rot13']), |
26
|
|
|
new TwigFilter('rot13gc', [$this, 'oc_Filter_rot13_gc']), |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return TwigFunction[] |
32
|
|
|
*/ |
33
|
|
|
public function getFunctions() |
34
|
|
|
: array |
35
|
|
|
{ |
36
|
|
|
return []; |
37
|
|
|
// return [new TwigFunction('area', [$this, 'calculateArea']),]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* calculate and format difficulty value |
42
|
|
|
* |
43
|
|
|
* @param $number |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function oc_Filter_D($number) |
|
|
|
|
48
|
|
|
: string { |
49
|
|
|
if ($number % 2 === 0) { |
50
|
|
|
$value = 'D' . number_format($number / 2, 0); |
51
|
|
|
} else { |
52
|
|
|
$value = 'D' . number_format($number / 2, 1); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* calculate and format terrain value |
60
|
|
|
* |
61
|
|
|
* @param $number |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function oc_Filter_T($number) |
|
|
|
|
66
|
|
|
: string { |
67
|
|
|
if ($number % 2 === 0) { |
68
|
|
|
$value = 'T' . number_format($number / 2, 0); |
69
|
|
|
} else { |
70
|
|
|
$value = 'T' . number_format($number / 2, 1); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* convert string via ROT13 |
78
|
|
|
* |
79
|
|
|
* @param $string |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function oc_Filter_rot13($string) |
84
|
|
|
: string { |
85
|
|
|
return str_rot13($string); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* convert string via ROT13, but ignore characters in [] brackets |
90
|
|
|
* |
91
|
|
|
* @param $string |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function oc_Filter_rot13_gc($string) |
96
|
|
|
: string { |
97
|
|
|
return str_rot13_gc($string); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.