/
rest-calculator.php
134 lines (104 loc) · 2.87 KB
/
rest-calculator.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
<?php
/*
Plugin Name: REST Calculator
Description: Add numbers using WP REST API!
Version: 0.1.0
Author: Luca Tumedei
Author URI: http://theaveragedev.com
*/
if (function_exists('add_action')) {
add_action('rest_api_init', function () {
register_rest_route('calc', 'add/(?P<o1>[-\.\d]+)/(?P<o2>[-\.\d]+)', [
'methods' => 'GET',
'callback' => [new Calculator, 'add'],
]);
register_rest_route('calc', 'times/(?P<o1>[-\.\d]+)/(?P<o2>[-\.\d]+)', [
'methods' => 'GET',
'callback' => [new Calculator, 'times'],
]);
});
}
class Calculator {
public function add(WP_REST_Request $req) {
return $this->process($req, 'add');
}
protected function process(WP_REST_Request $req, $op = 'add') {
try {
$operand_1 = new Operand($req->get_param('o1'));
$operand_2 = new Operand($req->get_param('o2'));
} catch (InvalidArgumentException $e) {
$response = new WP_REST_Response('Bad operands');
$response->set_status(400);
return $response;
}
switch ($op) {
default:
case 'add':
$operation = new Addition($operand_1, $operand_2);
break;
case 'times':
$operation = new Division($operand_1, $operand_2);
break;
}
$value = $operation->get_value();
set_transient('last_operation', $operation . ' with result ' . $value);
$response = new WP_REST_Response($value);
$response->set_status(200);
return $response;
}
public function times(WP_REST_Request $req) {
if (get_current_user_id() == 0 || !current_user_can('read')) {
$response = new WP_REST_Response('Divisions are for subscribers only');
$response->set_status(403);
return $response;
}
return $this->process($req, 'times');
}
}
class Operand {
private $value;
function __construct($value) {
if (!(filter_var($value, FILTER_VALIDATE_INT) || '0' == $value)) {
throw new InvalidArgumentException('Not an int');
}
$this->value = (int)$value;
}
function get_value() {
return $this->value;
}
}
class Operation {
protected $o1;
protected $o2;
function __construct(Operand $o1, Operand $o2) {
$this->o1 = $o1;
$this->o2 = $o2;
}
}
class Addition extends Operation {
function __toString() {
$v1 = $this->o1->get_value();
$v2 = $this->o2->get_value();
$line_1 = 'Add: ' . $v1 . '+' . $v2 . '=' . $this->get_value();
$line_2 = trailingslashit("Route: /wp-json/calc/add/{$v1}/{$v2}");
return $line_1 . "\n" . $line_2;
}
function get_value() {
return $this->o1->get_value() + $this->o2->get_value();
}
}
class Division extends Operation {
function __toString() {
$v1 = $this->o1->get_value();
$v2 = $this->o2->get_value();
$line_1 = 'Divide: ' . $v1 . '/' . $v2 . '=' . $this->get_value();
$line_2 = trailingslashit("Route: /wp-json/calc/times/{$v1}/{$v2}");
return $line_1 . "\n" . $line_2;
}
function get_value() {
if ($this->o2->get_value() == 0) {
return 'really?!';
}
return $this->o1->get_value() / $this->o2->get_value();
}
}