false, 'message' => 'Bad Request!', ]; public function __construct( private EntityManagerInterface $em ) { } #[Route('/change/{id<\d+>}/boolean', name: 'change_boolean', methods: ['PATCH'])] public function changeBoolean(int $id, Request $request): JsonResponse { $entity = $request->request->get('entity'); $field = $request->request->get('field') ?? self::FIELD_IS_ACTIVE; $token = $request->request->get('_token'); if (in_array($field, self::ALLOWED_FIELDS)) { $fieldSet = 'set' . $field; $fieldGet = 'get' . $field; } else { $this->response['message'] = 'Invalid field to change!'; return $this->json($this->response, 500); } if (!$entity = $this->em->getRepository($entity)->findOneBy(['id' => $id])) { $this->response['message'] = 'Not Found!'; return $this->json($this->response, 404); } if ($request->isXmlHttpRequest() && $this->isCsrfTokenValid('check' . $id, $token)) { $entity->$fieldSet(!$entity->$fieldGet()); $this->em->flush(); return $this->json( $this->response = [ 'success' => true, 'message' => $entity->getIsActive(), ] ); } return $this->json($this->response, 400); } }