|
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('ocFilterDifficulty', [$this, 'ocFilterDifficulty']), |
|
24
|
|
|
new TwigFilter('ocFilterTerrain', [$this, 'ocFilterTerrain']), |
|
25
|
|
|
new TwigFilter('rot13', [$this, 'ocFilterROT13']), |
|
26
|
|
|
new TwigFilter('rot13gc', [$this, 'ocFilterROT13gc']), |
|
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
|
|
|
* @param $number |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
private function convertDifficultyTerrainRating($number) |
|
46
|
|
|
: string { |
|
47
|
|
|
if ($number % 2 === 0) { |
|
48
|
|
|
return number_format($number / 2, 0); |
|
49
|
|
|
} else { |
|
50
|
|
|
return number_format($number / 2, 1); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* calculate and format difficulty value |
|
56
|
|
|
* |
|
57
|
|
|
* @param $number |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function ocFilterDifficulty($number) |
|
62
|
|
|
: string { |
|
63
|
|
|
return 'D' . $this->convertDifficultyTerrainRating($number); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* calculate and format terrain value |
|
68
|
|
|
* |
|
69
|
|
|
* @param $number |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function ocFilterTerrain($number) |
|
74
|
|
|
: string { |
|
75
|
|
|
return 'T' . $this->convertDifficultyTerrainRating($number); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* convert string via ROT13 |
|
80
|
|
|
* |
|
81
|
|
|
* @param $string |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function ocFilterROT13($string) |
|
86
|
|
|
: string { |
|
87
|
|
|
return str_rot13($string); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* convert string via ROT13, but ignore characters in [] brackets |
|
92
|
|
|
* |
|
93
|
|
|
* @param $string |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function ocFilterROT13gc($string) |
|
98
|
|
|
: string { |
|
99
|
|
|
return str_rot13_gc($string); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|