Created
August 2, 2017 11:49
-
-
Save gielfeldt/a63e12d2a8f36dc04bd23af92cf47c54 to your computer and use it in GitHub Desktop.
controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SwaggerService\RestService\MyService1\v1; | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Request; | |
/** | |
* @SWG\Swagger( | |
* @SWG\Info( | |
* version="1.0.0", | |
* title="My service", | |
* description="A service", | |
* ) | |
* ) | |
*/ | |
class MyService1 | |
{ | |
/** | |
* @SWG\Get( | |
* path="/find-employees", | |
* description="Returns all employees", | |
* operationId="findEmployees", | |
* produces={"application/json"}, | |
* x={"swaql-table":"employees"}, | |
* @SWG\Parameter( | |
* name="position", | |
* in="query", | |
* description="position", | |
* required=false, | |
* type="array", | |
* @SWG\Items(type="string"), | |
* collectionFormat="csv" | |
* ), | |
* @SWG\Response( | |
* response=200, | |
* description="list of employees", | |
* ) | |
* ) | |
*/ | |
public function findEmployees(Application $app, Request $request) | |
{ | |
$employees = $this->_employee(); | |
$position = $request->get('position') ? explode(',', $request->get('position')) : null; | |
$employees = !empty($position) ? array_filter($employees, function ($value) use ($position) { | |
return in_array($value['position'], $position); | |
}) : $employees; | |
return $app->json(array_values($employees)); | |
} | |
/** | |
* @SWG\Get( | |
* path="/employee/{id}", | |
* description="Return an employee", | |
* operationId="findEmployee", | |
* produces={"application/json"}, | |
* x={"swaql-table":"employee"}, | |
* @SWG\Parameter( | |
* description="ID of employee to return", | |
* in="path", | |
* name="id", | |
* required=true, | |
* type="integer", | |
* format="int64" | |
* ), | |
* @SWG\Response( | |
* response=200, | |
* description="found employee", | |
* ), | |
* @SWG\Response( | |
* response=404, | |
* description="employee not found", | |
* ) | |
* ) | |
*/ | |
public function findEmployee(Application $app, Request $request, $id) | |
{ | |
$employees = $this->_employee(); | |
return !empty($employees[$id]) ? $app->json($employees[$id]) : $app->abort(404, sprintf('Employee %d not found', $id)); | |
} | |
/** | |
* @SWG\Get( | |
* path="/find-companies", | |
* description="Returns all companies", | |
* operationId="findCompanies", | |
* produces={"application/json"}, | |
* x={"swaql-table":"companies"}, | |
* @SWG\Response( | |
* response=200, | |
* description="list of employees", | |
* ) | |
* ) | |
*/ | |
public function findCompanies(Application $app, Request $request) | |
{ | |
$companies = $this->_company(); | |
return $app->json(array_values($companies)); | |
} | |
/** | |
* @SWG\Get( | |
* path="/company/{id}", | |
* description="Return a company", | |
* operationId="findCompany", | |
* produces={"application/json"}, | |
* x={"swaql-table":"company"}, | |
* @SWG\Parameter( | |
* description="ID of company to return", | |
* in="path", | |
* name="id", | |
* required=true, | |
* type="integer", | |
* format="int64" | |
* ), | |
* @SWG\Response( | |
* response=200, | |
* description="found company", | |
* ), | |
* @SWG\Response( | |
* response=404, | |
* description="company not found", | |
* ) | |
* ) | |
*/ | |
public function findCompany(Application $app, Request $request, $id) | |
{ | |
$companies = $this->_company(); | |
return !empty($companies[$id]) ? $app->json($companies[$id]) : $app->abort(404, sprintf('Employee %d not found', $id)); | |
} | |
/** | |
* DATABASE TABLE EMPLOYEE!!! | |
*/ | |
protected function _employee() | |
{ | |
return [ | |
1 => [ | |
'id' => 1, | |
'name' => 'John', | |
'position' => 'Receptionist', | |
'salary' => 100, | |
'company' => 1, | |
], | |
2 => [ | |
'id' => 2, | |
'name' => 'Jane', | |
'position' => 'CEO', | |
'salary' => 200, | |
'company' => 2, | |
], | |
3 => [ | |
'id' => 3, | |
'name' => 'Jack', | |
'position' => 'Developer', | |
'salary' => 150, | |
'company' => 2, | |
], | |
4 => [ | |
'id' => 4, | |
'name' => 'Jill', | |
'position' => 'Manager', | |
'salary' => 170, | |
'company' => 1, | |
], | |
5 => [ | |
'id' => 5, | |
'name' => 'Doe', | |
'position' => 'Mailboy', | |
'salary' => 20, | |
'company' => 0, | |
], | |
]; | |
} | |
/** | |
* DATABASE TABLE COMPANY!!! | |
*/ | |
protected function _company() | |
{ | |
return [ | |
1 => [ | |
'id' => 1, | |
'name' => 'Shell', | |
], | |
2 => [ | |
'id' => 2, | |
'name' => 'Texaco', | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment