Add src
This commit is contained in:
12
wap/api-calc/src/index.php
Normal file
12
wap/api-calc/src/index.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>It works</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>It works..</h1>
|
||||
</body>
|
||||
</html>
|
||||
7
wap/api-calc/src/math-api/f1/index.php
Normal file
7
wap/api-calc/src/math-api/f1/index.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once "../utils.php";
|
||||
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
echo processData($_GET);
|
||||
24
wap/api-calc/src/math-api/f2/index.php
Normal file
24
wap/api-calc/src/math-api/f2/index.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
require_once "../utils.php";
|
||||
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
$skipnum = 2;
|
||||
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$uri = explode( '/', $uri );
|
||||
if(isset($uri[1+$skipnum])){
|
||||
$data["operation"] = $uri[1+$skipnum];
|
||||
$numbers = "";
|
||||
for($i = 2; $i < sizeof($uri) - $skipnum;$i++){
|
||||
$numbers .= $uri[$i+$skipnum];
|
||||
$numbers .= ",";
|
||||
}
|
||||
$data["numbers"] = $numbers;
|
||||
|
||||
echo processData($data);
|
||||
} else {
|
||||
wrongDataError();
|
||||
}
|
||||
|
||||
7
wap/api-calc/src/math-api/f3/index.php
Normal file
7
wap/api-calc/src/math-api/f3/index.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once "../utils.php";
|
||||
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
echo processData($_POST);
|
||||
80
wap/api-calc/src/math-api/utils.php
Normal file
80
wap/api-calc/src/math-api/utils.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
function processData(Array $data) {
|
||||
$op = ["add", "sub", "mul", "div"];
|
||||
if(isset($data["operation"]) && in_array($data["operation"], $op)){
|
||||
if(isset($data["numbers"])) {
|
||||
$rawnums = explode(',', $data["numbers"]);
|
||||
$num = [];
|
||||
|
||||
for($i = 0; $i < sizeof($rawnums); $i++) {
|
||||
if (is_numeric($rawnums[$i])) {
|
||||
array_push($num, floatval($rawnums[$i]));
|
||||
}
|
||||
}
|
||||
|
||||
if(sizeof($num) < 1) {
|
||||
wrongDataError();
|
||||
}
|
||||
|
||||
header('HTTP/1.1 200 OK');
|
||||
switch($data["operation"]){
|
||||
case "add":
|
||||
$result = json_encode(array("status" => "OK", "result" => addNumbers($num)));
|
||||
break;
|
||||
case "sub":
|
||||
$result = json_encode(array("status" => "OK", "result" => subNumbers($num)));
|
||||
break;
|
||||
case "mul":
|
||||
$result = json_encode(array("status" => "OK", "result" => mulNumbers($num)));
|
||||
break;
|
||||
case "div":
|
||||
$result = json_encode(array("status" => "OK", "result" => divNumbers($num)));
|
||||
break;
|
||||
}
|
||||
return $result;
|
||||
} else {
|
||||
wrongDataError();
|
||||
}
|
||||
} else {
|
||||
wrongDataError();
|
||||
}
|
||||
}
|
||||
|
||||
function addNumbers(Array $num){
|
||||
$result = 0;
|
||||
for ($i = 0; $i < sizeof($num);$i++) {
|
||||
$result += $num[$i];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function subNumbers(Array $num) {
|
||||
$result = 0;
|
||||
for ($i = 0; $i < sizeof($num);$i++) {
|
||||
$result -= $num[$i];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function mulNumbers(Array $num) {
|
||||
$result = $num[0];
|
||||
for ($i = 1; $i < sizeof($num);$i++) {
|
||||
$result *= $num[$i];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function divNumbers(Array $num) {
|
||||
$result = $num[0];
|
||||
for ($i = 1; $i < sizeof($num);$i++) {
|
||||
$result /= $num[$i];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function wrongDataError() {
|
||||
header('HTTP/1.1 422 Unprocessable Entity');
|
||||
echo json_encode(array("status" => "ERROR", "reason" => "Wrong Data"));
|
||||
die();
|
||||
}
|
||||
Reference in New Issue
Block a user