Repository navigation
Expand file tree
/
Copy pathMyAPI.class.php
More file actions
119 lines (96 loc) · 3.83 KB
/
Copy pathMyAPI.class.php
File metadata and controls
119 lines (96 loc) · 3.83 KB
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
<?php
/*
* Copyright (c) 2019. Paul Blacknell https://github.com/blacknell
*/
require __DIR__ . '/../vendor/autoload.php';
use Blacknell\RestApiService\RestAPI;
class MyAPI extends RestAPI
{
/**
* MyAPI constructor.
*
* @param $request
* @param \Monolog\Logger|null $logger
*/
public function __construct($request, \Monolog\Logger $logger = null)
{
parent::__construct($request, $logger);
}
// first all the protected functions which are exposed as REST API endpoints
/**
* @return array
*
* /home/v1/daylight/littlehampton/yesterday maps to api.php?request=daylight/littlehampton/yesterday
* daylight /littlehampton/yesterday
* endpoint /verb /args[0] /args[1]
*/
protected function daylight()
{
if ($this->method == 'GET') {
try {
$query = '';
if (isset($this->args[0]) && $this->args[0] != '') {
$query = $this->args[0];
}
switch ($this->verb) {
case 'littlehampton':
$latitude = 50.807861;
$longitude = -0.544835;
break;
default:
throw(new OutOfBoundsException('Unknown verb (location)', 400));
break;
}
switch ($query) {
case 'yesterday':
{
$now = new DateTime();
$yesterday = new DateTime();
$yesterday->sub(new DateInterval('P1D'));
$sunrise = date_sunrise($yesterday->getTimeStamp(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, 90.83333, $yesterday->getOffset() / 3600);
$sunset = date_sunset($yesterday->getTimeStamp(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, 90.83333, $yesterday->getOffset() / 3600);
$start = new DateTime();
$start->setTimeStamp($sunrise);
$finish = new DateTime();
$finish->setTimeStamp($sunset);
$obj = [
'description' => 'Between sunrise and sunset yesterday',
'location' => [
'latitude' => $latitude,
'longitude' => $longitude,
],
'now' => $now,
'sunrise' => $start,
'sunset' => $finish,
];
break;
}
default:
{
throw(new OutOfBoundsException('Unknown args[0] (when)', 400));
break;
}
}
return array("result" => $obj, "code" => 200);
} catch (OutOfBoundsException $e) {
$this->logger->error($e->getMessage(), $this->toObject());
$this->logger->error($e->getTraceAsString());
return array("result" => ['error' => $e->getMessage(), 'code' => $e->getCode()], "code" => $e->getCode());
} catch (Exception $e) {
$this->logger->error($e->getMessage(), $this->toObject());
$this->logger->error($e->getTraceAsString());
return array("result" => ['error' => $e->getMessage(), 'code' => 400], "code" => 400);
}
}
else {
header("Allow: GET");
return array("result" => ['error' => "Only accepts GET requests", 'code' => 405], "code" => 405);
}
}
//
//
// now any private helper functions
//
//
}
?>