1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Twig; |
4
|
|
|
|
5
|
|
|
use Oc\Repository\CoordinatesRepository; |
6
|
|
|
use Twig\Extension\AbstractExtension; |
7
|
|
|
use Twig\TwigFilter; |
8
|
|
|
use Twig\TwigFunction; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AppExtension |
12
|
|
|
* |
13
|
|
|
* @package Oc\Twig |
14
|
|
|
*/ |
15
|
|
|
class AppExtension extends AbstractExtension |
16
|
|
|
{ |
17
|
|
|
private $coordinatesRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AppExtension constructor. |
21
|
|
|
* |
22
|
|
|
* @param CoordinatesRepository $coordinatesRepository |
23
|
|
|
*/ |
24
|
|
|
public function __construct(CoordinatesRepository $coordinatesRepository) |
25
|
|
|
{ |
26
|
|
|
$this->coordinatesRepository = $coordinatesRepository; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return TwigFilter[] |
31
|
|
|
*/ |
32
|
|
|
public function getFilters() |
33
|
|
|
: array |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
new TwigFilter('ocFilterDifficulty', [$this, 'ocFilterDifficulty']), |
37
|
|
|
new TwigFilter('ocFilterTerrain', [$this, 'ocFilterTerrain']), |
38
|
|
|
new TwigFilter('rot13', [$this, 'ocFilterROT13']), |
39
|
|
|
new TwigFilter('rot13gc', [$this, 'ocFilterROT13gc']), |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return TwigFunction[] |
45
|
|
|
*/ |
46
|
|
|
public function getFunctions() |
47
|
|
|
: array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
new TwigFunction('ocFilterCoordinatesDegMin', [$this, 'ocFilterCoordinatesDegMin']), |
51
|
|
|
new TwigFunction('ocFilterCoordinatesDegMinSec', [$this, 'ocFilterCoordinatesDegMinSec']), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $number |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
private function convertDifficultyTerrainRating($number) |
61
|
|
|
: string { |
62
|
|
|
if ($number % 2 === 0) { |
63
|
|
|
return number_format($number / 2, 0); |
64
|
|
|
} else { |
65
|
|
|
return number_format($number / 2, 1); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* calculate and format difficulty value |
71
|
|
|
* |
72
|
|
|
* @param $number |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function ocFilterDifficulty($number) |
77
|
|
|
: string { |
78
|
|
|
return 'D' . $this->convertDifficultyTerrainRating($number); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* calculate and format terrain value |
83
|
|
|
* |
84
|
|
|
* @param $number |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function ocFilterTerrain($number) |
89
|
|
|
: string { |
90
|
|
|
return 'T' . $this->convertDifficultyTerrainRating($number); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* convert string via ROT13 |
95
|
|
|
* |
96
|
|
|
* @param $string |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function ocFilterROT13($string) |
101
|
|
|
: string { |
102
|
|
|
return str_rot13($string); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* convert string via ROT13, but ignore characters in [] brackets |
107
|
|
|
* |
108
|
|
|
* @param $string |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function ocFilterROT13gc($string) |
113
|
|
|
: string { |
114
|
|
|
return str_rot13_gc($string); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $lat |
119
|
|
|
* @param $lon |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function ocFilterCoordinatesDegMin($lat, $lon) |
124
|
|
|
: string { |
125
|
|
|
$this->coordinatesRepository->setLatLon($lat, $lon); |
126
|
|
|
|
127
|
|
|
$result = $this->coordinatesRepository->getDegreeMinutes(); |
128
|
|
|
|
129
|
|
|
return $result['lat'] . ' ' . $result['lon']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $lat |
134
|
|
|
* @param $lon |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function ocFilterCoordinatesDegMinSec($lat, $lon) |
139
|
|
|
: string { |
140
|
|
|
$this->coordinatesRepository->setLatLon($lat, $lon); |
141
|
|
|
|
142
|
|
|
$result = $this->coordinatesRepository->getDegreeMinutesSeconds(); |
143
|
|
|
|
144
|
|
|
return $result['lat'] . ' ' . $result['lon']; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|