|
| 1 | +<?php |
| 2 | + |
| 3 | +/** FizzBuzz REST api allowing GET requests in the following format: domain.com/api/fizzbuzz/100 |
| 4 | + * where 100 could be any number |
| 5 | + * To use this api just require the class and call: |
| 6 | + * $api = new Api(); |
| 7 | + * echo $api->run(); |
| 8 | + * Author: @absolutic |
| 9 | + */ |
| 10 | +class FizzBuzzApi |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + public $apiName = 'api'; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var array |
| 19 | + */ |
| 20 | + public $actions = [ |
| 21 | + 'fizzbuzz' => 'fizzBuzzAction' |
| 22 | + ]; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $method = 'GET'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + public $requestUri = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * Api constructor. |
| 36 | + */ |
| 37 | + public function __construct() |
| 38 | + { |
| 39 | + // Set headers to allow client requests |
| 40 | + header("Access-Control-Allow-Orgin: *"); |
| 41 | + header("Access-Control-Allow-Methods: GET"); |
| 42 | + header("Content-Type: application/json"); |
| 43 | + |
| 44 | + // Read Url and params to properties |
| 45 | + $this->requestUri = explode('/', trim($_SERVER['REQUEST_URI'],'/')); |
| 46 | + |
| 47 | + // If request method is not GET return error |
| 48 | + if ($this->method !== $_SERVER['REQUEST_METHOD']) { |
| 49 | + return $this->response('Invalid request method'); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return mixed |
| 55 | + */ |
| 56 | + public function run() |
| 57 | + { |
| 58 | + if (array_shift($this->requestUri) !== $this->apiName) { |
| 59 | + throw new RuntimeException('API not found', 404); |
| 60 | + } |
| 61 | + |
| 62 | + $uriAction = array_shift($this->requestUri); |
| 63 | + if (!in_array($uriAction, array_keys($this->actions))) { |
| 64 | + throw new RuntimeException('Action not found', 404); |
| 65 | + } else { |
| 66 | + $action = $this->actions[$uriAction]; |
| 67 | + } |
| 68 | + |
| 69 | + // Check and run api method |
| 70 | + if (method_exists($this, $action)) { |
| 71 | + return $this->{$action}(); |
| 72 | + } else { |
| 73 | + throw new RuntimeException('Invalid Method', 405); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * |
| 79 | + */ |
| 80 | + public function fizzBuzzAction() |
| 81 | + { |
| 82 | + $number = array_shift($this->requestUri); |
| 83 | + $result = []; |
| 84 | + foreach (range(1, $number) as $i) { |
| 85 | + $result[] = $this->fizzbuzz($i, 15, 'FizzBuzz') |
| 86 | + ?: $this->fizzbuzz($i, 5, 'Fizz') |
| 87 | + ?: $this->fizzbuzz($i, 3, 'Buzz') ?: $i; |
| 88 | + } |
| 89 | + return $this->response($result, 200); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param $val |
| 94 | + * @param $num |
| 95 | + * @param $str |
| 96 | + * @return bool |
| 97 | + */ |
| 98 | + protected function fizzBuzz($val, $num, $str) |
| 99 | + { |
| 100 | + return $val % $num == 0 ? $str : false; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param $data |
| 105 | + * @param int $status |
| 106 | + * @return false|string |
| 107 | + */ |
| 108 | + protected function response($data, $status = 500) |
| 109 | + { |
| 110 | + header("HTTP/1.1 " . $status . " " . $this->requestStatus($status)); |
| 111 | + return json_encode($data); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param $code |
| 116 | + * @return mixed |
| 117 | + */ |
| 118 | + private function requestStatus($code) |
| 119 | + { |
| 120 | + $status = array( |
| 121 | + 200 => 'OK', |
| 122 | + 404 => 'Not Found', |
| 123 | + 405 => 'Method Not Allowed', |
| 124 | + 500 => 'Internal Server Error', |
| 125 | + ); |
| 126 | + return $status[$code]?: $status[500]; |
| 127 | + } |
| 128 | +} |
0 commit comments