예제 #1
0
 /**
  * [ВНИМАНИЕ]
  * Если у сущности генератор первичного ключа отсутствует, т.е. необходимо задать вручную ключ, то необходимо удалить
  * первичный ключ из структуры, чтобы не было попыток найти эту сущность. Ошибка описана в ссылке ниже
  * @link https://github.com/jamesmoey/extjs-bundle/issues/17
  * @inheritdoc
  */
 public function run()
 {
     $this->checkIsInit();
     $obj = json_decode($this->requestContent, false, 512, JSON_BIGINT_AS_STRING);
     $response = new RestResponse();
     $savedPKValueFromRequestData = null;
     $PKFieldName = $this->getPKFieldName();
     $entityNotHaveIdGenerator = $this->classMetadata->generatorType == ClassMetadata::GENERATOR_TYPE_NONE;
     if ($entityNotHaveIdGenerator) {
         if (!$this->isObjectHasPKField($obj) && !$this->isObjectHasNewPKField($obj)) {
             $response->setSuccess(false)->setStatusCode(RestResponse::STATUS_CODE_WRONG_INPUT_DATA)->setMessage(sprintf("Не указан первичный ключ (%s)", $PKFieldName));
             return $response;
         } elseif (!$this->isObjectHasPKField($obj) && $this->isObjectHasNewPKField($obj)) {
             $savedPKValueFromRequestData = $this->getNewPKValueFromObject($obj);
             unset($obj->{$this->getNewPKValueFromObject($obj)});
         } elseif ($this->isObjectHasPKField($obj) && !$this->isObjectHasNewPKField($obj)) {
             $savedPKValueFromRequestData = $obj->{$PKFieldName};
             unset($obj->{$PKFieldName});
         }
     }
     $serializedObj = json_encode($obj);
     $entity = $this->serializer->deserialize($serializedObj, $this->classMetadata->getName(), 'json', DeserializationContext::create()->setGroups([Action::GROUP_DEFAULT, Action::GROUP_POST]));
     if ($entityNotHaveIdGenerator) {
         EntityHelper::changePKValue($this->em, $entity, $savedPKValueFromRequestData);
     }
     $validator = $this->validator;
     /** @var ConstraintViolationList $validations */
     $validations = $validator->validate($entity);
     if (sizeof($validations) === 0) {
         try {
             $this->em->persist($entity);
             $this->em->flush();
             $this->em->refresh($entity);
             $response->setSuccess(true)->setData($entity)->setStatusCode(RestResponse::STATUS_CODE_OK);
         } catch (DBALException $e) {
             $response->setSuccess(false)->setMessage($e->getMessage())->setStatusCode(RestResponse::STATUS_CODE_WRONG_INPUT_DATA);
         }
     } else {
         $response->setSuccess(false)->setStatusCode(RestResponse::STATUS_CODE_WRONG_INPUT_DATA)->setErrors($validations);
     }
     return $response;
 }
 public function testHttpStatusMethods()
 {
     $this->obj->setStatusCode(RestResponse::STATUS_CODE_OK);
     $this->assertEquals(RestResponse::STATUS_CODE_OK, $this->obj->getStatusCode());
 }
 /**
  * @param RestResponse $restResponse
  * @param array $serializationGroups
  * @param int $httpOKStatus при POST запросе задайте этот параметр в 201
  *
  * @return View
  */
 public static function generateViewFromRestResponse(RestResponse $restResponse, $serializationGroups = [], $httpOKStatus = 200)
 {
     $HTTPStatusCode = 200;
     switch ($restResponse->getStatusCode()) {
         case RestResponse::STATUS_CODE_OK:
             if (!in_array($httpOKStatus, [200, 201])) {
                 $HTTPStatusCode = 200;
             } else {
                 $HTTPStatusCode = $httpOKStatus;
             }
             break;
         case RestResponse::STATUS_CODE_ENTITY_NOT_FOUND:
             $HTTPStatusCode = 404;
             break;
         case RestResponse::STATUS_CODE_WRONG_INPUT_DATA:
             $HTTPStatusCode = 400;
             break;
     }
     $view = View::create($restResponse->asArray(), $HTTPStatusCode)->setSerializationContext(self::createSerializerContext($serializationGroups));
     return $view;
 }