-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMathController.php
95 lines (78 loc) · 2.47 KB
/
MathController.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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Helpers\Helper;
use Exception;
use GuzzleHttp\Client as HttpClient;
class MathController extends Controller
{
/**
* Base URL for math.js API.
*
* @var string
*/
private $mathBaseUrl = 'https://math.decapi.net/?expr=';
/**
* HTTP client
*
* @var GuzzleHttp\Client
*/
private $client;
/**
* HTTP client settings
*
* @var array
*/
private $clientSettings = [
'http_errors' => false,
];
public function __construct(HttpClient $client)
{
$this->client = $client;
}
/**
* Evaluates a math expression.
*
* @param Request $request
* @return Response
*/
public function evaluate(Request $request)
{
$exp = $request->input('exp', null);
$round = intval($request->input('round', 0));
if (empty($exp)) {
return Helper::text('A math expression (exp) has to be specified.');
}
$decimalSeparator = $request->input('separator', '.');
// Spaces are unnecessary in the expression, and will actually error the evaluation
// if spaces are used as a 'thousand separator'
$exp = str_replace(' ', '', $exp);
if ($decimalSeparator === '.') {
// API does not like comma used as a thousand separator
$exp = str_replace(',', '', $exp);
} else {
$exp = str_replace('.', '', $exp);
$exp = str_replace($decimalSeparator, '.', $exp);
}
try {
$url = sprintf('%s%s', $this->mathBaseUrl, urlencode($exp));
$response = $this->client->request('GET', $url, $this->clientSettings);
if ($response->getStatusCode() !== 200) {
return Helper::text(sprintf('An error occurred calculating the expression: %s', $exp));
}
$result = (string) $response->getBody();
if (strlen($result) === 0) {
return Helper::text('No result.');
}
if ($request->exists('round') === true) {
// Before we can use `round()` we need to convert it to a float.
$result = (float) $result;
$result = round($result, $round);
}
return Helper::text($result);
} catch (Exception $e) {
return Helper::text('An error occurred evaluating: ' . $exp);
}
}
}