|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Class created by Matheus Marques https://github.com/Matheus2212 |
| 5 | + * 2021-06-12 -> First version of the API class |
| 6 | + * 2021-06-18 -> Updated ALL methods for the class |
| 7 | + * 2021-06-19 -> Defined methods for routes and route groups for the class |
| 8 | + * 2021-06-20 -> removed all own methods and made the API class work together with the Friendly URL class |
| 9 | + * 2021-06-22 -> Fully integrated API class with Friendly URL class (now its a dependency) |
| 10 | + * */ |
| 11 | + |
| 12 | +class API |
| 13 | +{ |
| 14 | + |
| 15 | + private $version = 0; |
| 16 | + |
| 17 | + private $url = null; |
| 18 | + |
| 19 | + private $request = null; |
| 20 | + |
| 21 | + private $URLclass = null; |
| 22 | + |
| 23 | + private $routes = array(); |
| 24 | + |
| 25 | + private $data = array(); |
| 26 | + |
| 27 | + private $mode = ""; |
| 28 | + |
| 29 | + public function __construct($url, $version) |
| 30 | + { |
| 31 | + header("Access-Control-Allow-Origin: *"); |
| 32 | + header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding"); |
| 33 | + header('Access-Control-Allow-Credentials: true'); |
| 34 | + header('Access-Control-Max-Age: 86400'); |
| 35 | + header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT'); |
| 36 | + |
| 37 | + if ($url && is_object($url)) { |
| 38 | + $this->version = $version; |
| 39 | + $this->url = $url->getURL() . "v" . explode(".", $this->version)[0] . "/"; |
| 40 | + $now = str_replace(preg_replace("/http(s)?\:\/\//", "", $url->getURL()), "", preg_replace("/http(s)?\:\/\//", "", $url->agora())); |
| 41 | + $this->parts = explode("/", $now); |
| 42 | + $this->URLclass = $url; |
| 43 | + $this->URLclass->partes = $this->parts; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function __set($key, $value) |
| 48 | + { |
| 49 | + $this->$key = $value; |
| 50 | + } |
| 51 | + |
| 52 | + public function version() |
| 53 | + { |
| 54 | + $this->join(array('version' => $this->version)); |
| 55 | + return $this->version; |
| 56 | + } |
| 57 | + |
| 58 | + public function join($array, $status = true) |
| 59 | + { |
| 60 | + $this->data = array_merge($this->data, $array); |
| 61 | + $this->data = array_merge($this->data, array("status" => $status)); |
| 62 | + if (isset($this->data['error'])) { |
| 63 | + $this->data['status'] = false; |
| 64 | + } |
| 65 | + $this->data["time"] = time(); |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + public function define($route, $callback) |
| 70 | + { |
| 71 | + $parts = explode("/", $route); |
| 72 | + $parts = array_filter($parts, function ($item) { |
| 73 | + if (trim($item) === "") { |
| 74 | + return false; |
| 75 | + } else { |
| 76 | + return true; |
| 77 | + } |
| 78 | + }); |
| 79 | + sort($parts); |
| 80 | + $route = $parts[0]; |
| 81 | + $this->routes[$route] = array("callback" => $callback); |
| 82 | + if (isset($parts[1])) { |
| 83 | + $this->routes[$route]["url"] = $parts[1]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public function route() |
| 88 | + { |
| 89 | + $operation = $this->URLclass->get(0); |
| 90 | + if (isset($this->routes[$operation])) { |
| 91 | + $reflection = new ReflectionFunction($this->routes[$operation]["callback"]); |
| 92 | + $totalParams = $reflection->getNumberOfParameters(); |
| 93 | + if ($totalParams > 1) { |
| 94 | + if ($this->request === null) { |
| 95 | + $this->join(array("error" => "002", "message" => "No request was sent")); |
| 96 | + } else { |
| 97 | + $this->routes[$operation]["callback"]($this, $this->request, true); |
| 98 | + } |
| 99 | + } else { |
| 100 | + $this->routes[$operation]["callback"]($this); |
| 101 | + } |
| 102 | + } else { |
| 103 | + $this->join(array("error" => "001", "message" => "This route doesn't exists")); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private function APIReturn() |
| 108 | + { |
| 109 | + switch ($this->mode) { |
| 110 | + case "json": |
| 111 | + function recursive_json($data) |
| 112 | + { |
| 113 | + if (is_array($data)) { |
| 114 | + return array_map("recursive_json", $data); |
| 115 | + } else { |
| 116 | + return utf8_encode($data); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return json_encode(recursive_json($this->data)); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public function type($type) |
| 126 | + { |
| 127 | + switch ($type) { |
| 128 | + case "json": |
| 129 | + header("Content-type: application/json"); |
| 130 | + $this->request = json_decode(file_get_contents('php://input'), true); |
| 131 | + $this->mode = "json"; |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public function response($echo = false) |
| 137 | + { |
| 138 | + $data = $this->APIReturn(); |
| 139 | + if ($echo) { |
| 140 | + echo $data; |
| 141 | + } |
| 142 | + return $data; |
| 143 | + } |
| 144 | +} |
0 commit comments