-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMiscController.php
161 lines (132 loc) · 4.61 KB
/
MiscController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exceptions\CurrencyApiException;
use App\Helpers\Helper;
use App\Repositories\CurrencyApiRepository;
use Carbon\Carbon;
use DateTimeZone;
class MiscController extends Controller
{
/**
* @var App\Repositories\CurrencyApiRepository
*/
private $currencyApi;
public function __construct(CurrencyApiRepository $currencyApi)
{
$this->currencyApi = $currencyApi;
}
/**
* Converts currency based on parameters passed.
*
* @param Request $request
* @return Response
*/
public function currency(Request $request)
{
$value = $request->input('value', null);
$from = $request->input('from', null);
$to = $request->input('to', null);
$round = intval($request->input('round', 2));
$listUrl = route('misc.currency', 'currency') . '?list';
if ($request->exists('list')) {
$currencies = $this->currencyApi->getCurrencies();
$currencies = array_map(
function($code) {
return strtoupper($code);
},
array_keys($currencies['currencies'])
);
return Helper::text('Available currencies: ' . implode(', ', $currencies));
}
if (empty($value)) {
return Helper::text('The "value" parameter has to be specified');
}
if (empty($from)) {
return Helper::text('The "from" parameter has to be specified');
}
if (empty($to)) {
return Helper::text('The "to" parameter has to be specified');
}
$value = floatval(str_replace(',', '', $value));
if ((int) $value === 0) {
$value = 1;
}
$fromValid = $this->currencyApi->isValidCurrency($from);
if (!$fromValid) {
return Helper::text(sprintf('Invalid "from" currency specified (%s) - Available currencies can be found here: %s', $from, $listUrl));
}
$toValid = $this->currencyApi->isValidCurrency($to);
if (!$toValid) {
return Helper::text(sprintf('Invalid "to" currency specified (%s) - Available currencies can be found here: %s', $to, $listUrl));
}
try {
$convert = $this->currencyApi->convert($from, $to, $value, $round);
return Helper::text($convert);
} catch (CurrencyApiException $e) {
return Helper::text(sprintf('An error occurred while converting the currency: %s', $e->getMessage()));
}
}
/**
* Display the current time in the specified timezone.
*
* @param Request $request
* @return Response
*/
public function time(Request $request)
{
$format = $request->input('format', 'h:i:s A T');
$tz = $request->input('timezone', null);
$timezones = DateTimeZone::listIdentifiers();
if (empty($tz)) {
return Helper::text(sprintf('-- Parameter `timezone` needs to be specified - Available timezones can be found here: %s', route('misc.timezones')));
}
if (!in_array($tz, $timezones)) {
return Helper::text(sprintf('-- Invalid timezone specified ("%s") - Available timezones can be found here: %s', $tz, route('misc.timezones')));
}
$time = Carbon::now()
->tz($tz)
->format($format);
return Helper::text($time);
}
/**
* Displays the time difference between two specified times.
* Similar to the Twitch `followage` endpoint.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function timeDifference(Request $request)
{
$first = $request->input('first', null);
$second = $request->input('second', null);
$precision = (int) $request->input('precision', 7);
if (empty($first)) {
return Helper::text('The `first` parameter has to be specified.');
}
$first = strtotime($first);
if (empty($second)) {
$second = time();
}
else {
$second = strtotime($second);
}
$diff = Helper::getDateDiff($first, $second, $precision);
return Helper::text($diff);
}
/**
* Lists the supported timezones.
*
* @param Request $request
* @return Response
*/
public function timezones(Request $request)
{
$timezones = DateTimeZone::listIdentifiers();
if ($request->wantsJson()) {
return Helper::json($timezones);
}
return Helper::text(implode(PHP_EOL, $timezones));
}
}