Last active
November 5, 2015 23:01
-
-
Save loiclefloch/7289b29f877edd4fe6cd to your computer and use it in GitHub Desktop.
Symfony REST API
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 Example\ApiBundle\Controller; | |
use FOS\RestBundle\Controller\FOSRestController; | |
use FOS\RestBundle\View\View; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Serializer\Exception\LogicException; | |
use Symfony\Component\Validator\ConstraintViolation; | |
class BaseController extends FOSRestController | |
{ | |
/** | |
* @param array $data the content of the response. It will be serialize. | |
* @param int $httpCode the http code of the response | |
* @return Response The response object | |
*/ | |
protected function buildResponse($data = array(), $httpCode = Response::HTTP_OK) | |
{ | |
$view = View::create(); | |
$view->setStatusCode($httpCode); | |
$view->setData($data); | |
return $this->handleView($view); | |
} | |
/** | |
* This function build a Response object based on the $data and $httpCode given. | |
* @param array $data | |
* @param int $httpCode You must use Response defines | |
* @return Response The response to return | |
*/ | |
protected function successResponse($data = array(), $httpCode = Response::HTTP_OK) | |
{ | |
return $this->buildResponse($data, $httpCode); | |
} | |
/** | |
* Send a formatted error response | |
* @param $code int The api error code | |
* @param $message string A comprehensive message of the error, for the developper | |
* @param $httpCode int The httpCode to return | |
* @return Response The response to return | |
*/ | |
protected function errorResponse($code, $message, $httpCode = Response::HTTP_BAD_REQUEST) | |
{ | |
return $this->buildResponse([ | |
'code' => $code, | |
'message' => $message | |
], $httpCode); | |
} | |
protected function notFoundResponse() | |
{ | |
return $this->errorResponse(404, "Not found", Response::HTTP_NOT_FOUND); | |
} | |
// --------------------------------------------------------------------------------------------------------------- | |
protected function getRepository($name = NULL) | |
{ | |
assert($name !== null, "repository name is null"); | |
return $this->getDoctrine()->getManager()->getRepository("ApiBundle:" . $name); | |
} | |
protected function persistEntity($entity) | |
{ | |
$em = $this->get('doctrine')->getManager(); | |
$em->persist($entity); | |
try { | |
$em->flush(); | |
} catch (Exception $e) { | |
die('ERROR: ' . $e->getMessage()); | |
} | |
} | |
protected function flush() | |
{ | |
$em = $this->get('doctrine')->getManager(); | |
try { | |
$em->flush(); | |
} catch (Exception $e) { | |
die('ERROR: ' . $e->getMessage()); | |
} | |
} | |
protected function removeEntity($entity) | |
{ | |
$em = $this->get('doctrine')->getManager(); | |
$em->remove($entity); | |
try { | |
$em->flush(); | |
} catch (Exception $e) { | |
die('ERROR: ' . $e->getMessage()); | |
} | |
} | |
protected function getLogger() | |
{ | |
return $this->get('logger'); | |
} | |
// --------------------------------------------------------------------------------------------------------------- | |
/** | |
* Rename the given index on the given array | |
* @param $newIndexes | |
* @param $data | |
* @return array | |
*/ | |
protected function renameArrayIndexes($newIndexes, $data) | |
{ | |
$final = array(); | |
foreach ($data as $name => $value) { | |
if (isset($newIndexes[$name])) | |
$name = $newIndexes[$name]; | |
$final[$name] = $value; | |
} | |
return ($final); | |
} | |
/** | |
* Return an array with the errors of the form | |
* @param $form | |
* @return array | |
*/ | |
protected function formErrorsToArray($form) | |
{ | |
$errors = array(); | |
foreach ($form->getErrors() as $error) { | |
$message = $error->getMessage(); | |
if ($message != NULL) | |
$errors[] = $message . (substr($message, -1) != '.' ? '.' : NULL); | |
} | |
foreach ($form->all() as $key => $child) { | |
if (($err = $this->formErrorsToArray($child))) | |
$errors[$key] = $err; | |
} | |
return ($errors); | |
} | |
// --------------------------------------------------------------------------------------------------------------- | |
protected function entityHaveError($entity) | |
{ | |
$validator = $this->get('validator'); | |
$errors = $validator->validate($entity); | |
return count($errors) !== 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment