public function testClassToSnakeCase() { $this->assertEquals('not_found', StringUtil::classToSnakeCase(new NotFoundHttpException(), 'HttpException')); $this->assertEquals('bad_request', StringUtil::classToSnakeCase(new BadRequestHttpException(), 'HttpException')); $this->assertEquals('not_blank', StringUtil::classToSnakeCase(new Constraint\NotBlank())); $this->assertEquals('email', StringUtil::classToSnakeCase(new Constraint\Email())); }
/** * @param $error * @param null $child * @return array */ protected function toErrorArray($error, $child = null) { $data = []; if (is_null($child)) { $data['field'] = '#'; } else { $data['field'] = $child->getName(); } if (!is_null($error->getCause()) && !is_null($error->getCause()->getConstraint())) { $data['code'] = self::FIELD_ERROR_PREFIX . StringUtil::classToSnakeCase($error->getCause()->getConstraint()); } else { if (stristr($error->getMessage(), 'csrf')) { $data['code'] = self::FIELD_ERROR_PREFIX . 'csrf'; } else { $data['code'] = self::FIELD_ERROR_PREFIX . 'general'; } } $data['message'] = $error->getMessage(); return $data; }
/** * @param FormError $error * @param FormInterface|null $form * @return ErrorField */ protected function toErrorArray(FormError $error, FormInterface $form = null) { if (is_null($form)) { $field = self::FIELD_ROOT; } else { $field = $form->getName(); } if (!is_null($error->getCause()) && !is_null($error->getCause()->getConstraint())) { $code = $this->getErrorCode(StringUtil::classToSnakeCase($error->getCause()->getConstraint())); } else { $code = $this->getErrorCodeByMessage($error); } return new ErrorField($field, $code, $error->getMessage()); }
/** * @param string $errorCode * @param string $trim * @return string */ protected function getExceptionErrorCode($errorCode, $trim = null) { return sprintf($errorCode, StringUtil::classToSnakeCase($this->exception, $trim)); }
/** * @return array */ public function toArray() { $return = []; if ($this->getReturnStatusCode()) { $return['statusCode'] = $this->getStatusCode(); } if (isset($this->exception)) { if ($this->exception instanceof FormValidationException) { $error = $this->exception->toArray(); } elseif ($this->exception instanceof HttpException) { $error = ['code' => self::ERROR_HTTP_PREFIX . StringUtil::classToSnakeCase($this->exception, 'HttpException'), 'message' => $this->exception->getMessage()]; } else { $error = ['code' => trim(self::ERROR_GENERAL_PREFIX . StringUtil::classToSnakeCase($this->exception, 'Exception'), '.'), 'message' => $this->exception->getMessage()]; } $return['error'] = $error; } if (isset($this->data)) { $return['data'] = $this->data; } if (isset($this->location)) { $return['location'] = $this->location; } if (isset($this->pagination)) { $return['pagination'] = $this->pagination; } return $return; }
/** * @param FormError $error * @param FormInterface|null $form * @return array */ protected function toErrorArray(FormError $error, FormInterface $form = null) { $data = []; if (is_null($form)) { $data['field'] = '#'; } else { $data['field'] = $form->getName(); } if (!is_null($error->getCause()) && !is_null($error->getCause()->getConstraint())) { $data['code'] = $this->getErrorCode(StringUtil::classToSnakeCase($error->getCause()->getConstraint())); } else { $this->getErrorCodeByMessage($error); } $data['message'] = $error->getMessage(); return $data; }