public function __construct()
 {
     // Get the current request method.
     $this->method = strtoupper($_SERVER['REQUEST_METHOD']);
     // Get the requested URI.
     $this->uri = $_SERVER['REQUEST_URI'];
     // Check if the type of the request is allowed.
     // If it's not - exit with error code 405.
     if (!in_array($this->method, $this->allowedMethods)) {
         return Response::methodNotAllowed($this->method, $this->uri);
     } else {
         // Check if X-HTTP-Method header is added to the POST request
         // and check for validity (either a DELETE or a PUT request).
         if ($this->method === 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
             if ($_SERVER['HTTP_X_HTTP_METHOD'] === 'DELETE') {
                 $this->method = 'DELETE';
             } else {
                 if ($_SERVER['HTTP_X_HTTP_METHOD'] === 'PUT') {
                     $this->method = 'PUT';
                 } else {
                     return Response::methodNotAllowed($this->method, $this->uri);
                 }
             }
         }
     }
     // If it's a put request then add the input add to the arguments.
     if ($this->method === 'PUT') {
         $input = json_decode(file_get_contents('php://input'));
         foreach ($input as $key => $value) {
             $this->arguments[$key] = $value;
         }
     }
     $request = explode('/', $this->uri);
     $this->controller = count($request) > 1 ? ucwords($request[1] . 'Controller') : null;
     if (count($request) > 2 && $request[2] !== null) {
         if (is_numeric($request[2])) {
             $this->action = null;
             $this->arguments['id'] = $request[2];
         } else {
             $this->action = strtolower($request[2]);
         }
     }
     if (count($request) > 3) {
         $this->arguments['id'] = $request[3];
     }
 }
 /**
  * Delete a job by given id.
  * Note: This will also delete all candidates applied for the job.
  *
  * @api DELETE /jobs/:id - Delete job.
  * @apiParam Integer id - Job id..
  * @apiExample Example CURL usage
  *      curl -i --request DELETE http://localhost/jobs/1
  *
  * @param $args Array
  */
 public function delete($args)
 {
     // return bad request response if there is not an ID passed.
     if (!isset($args['id'])) {
         return Response::badRequest(['message' => 'Missing job id.']);
     }
     $output = $this->jobs->delete($args);
     // return bad request response if there are validation errors
     if (isset($output['error']) && $output['error'] === 'validation') {
         return Response::badRequest($output['data']);
     }
     // return no content response if there are no records for a job
     if ($output === 0) {
         return Response::noContent();
     }
     return Response::success(['message' => 'Successfully deleted ' . $output . ' record(s)']);
 }
Exemple #3
0
 private static function setDefaultResponse()
 {
     return \App\Middleware\JsonResponseMiddleware::notFound();
 }
 /**
  * Search for job's application(s) by given id.
  *
  * @api GET /candidates/search/:id
  * @apiParam Integer id - Job's application id.
  * @apiExample Example CURL usage
  *      curl -i --request GET http://localhost/candidates/search/1
  *
  * @param $args Array
  */
 public function search($args)
 {
     // return bad request response if there is not an ID passed.
     if (!isset($args['id'])) {
         return Response::badRequest(['message' => 'Missing application id.']);
     }
     $output = $this->candidates->search($args['id']);
     // return bad request response if there are some kind of a validation errors
     if (isset($output['error']) && $output['error'] === 'validation') {
         return Response::badRequest($output['data']);
     }
     // return no content response if there are no records for the job application
     if (count($output) < 1) {
         return Response::noContent();
     }
     return Response::success($output);
 }