本文整理汇总了PHP中Tobscure\JsonApi\Document::setErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP Document::setErrors方法的具体用法?PHP Document::setErrors怎么用?PHP Document::setErrors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tobscure\JsonApi\Document
的用法示例。
在下文中一共展示了Document::setErrors方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* @param Exception $e
* @return JsonApiResponse
*/
public function handle(Exception $e)
{
$response = $this->errorHandler->handle($e);
$document = new Document();
$document->setErrors($response->getErrors());
return new JsonApiResponse($document, $response->getStatus());
}
示例2: handle
public function handle(Exception $e)
{
if ($e instanceof JsonApiSerializableInterface) {
$status = $e->getStatusCode();
$errors = $e->getErrors();
} elseif ($e instanceof ValidationException) {
$status = 422;
$errors = $e->errors()->toArray();
$errors = array_map(function ($field, $messages) {
return ['detail' => implode("\n", $messages), 'source' => ['pointer' => '/data/attributes/' . $field]];
}, array_keys($errors), $errors);
} elseif ($e instanceof ModelNotFoundException) {
$status = 404;
$errors = [];
} else {
$status = 500;
$error = ['code' => $status, 'title' => 'Internal Server Error'];
if ($this->app->inDebugMode()) {
$error['detail'] = (string) $e;
}
$errors = [$error];
}
$document = new Document();
$document->setErrors($errors);
return new JsonApiResponse($document, $status);
}
示例3: getMiddleware
/**
* {@inheritdoc}
*/
protected function getMiddleware(Application $app)
{
$pipe = new MiddlewarePipe();
$apiPath = parse_url($app->url('api'), PHP_URL_PATH);
if ($app->isInstalled() && $app->isUpToDate()) {
$pipe->pipe($apiPath, $app->make('Flarum\\Http\\Middleware\\AuthenticateWithCookie'));
$pipe->pipe($apiPath, $app->make('Flarum\\Api\\Middleware\\AuthenticateWithHeader'));
$pipe->pipe($apiPath, $app->make('Flarum\\Http\\Middleware\\ParseJsonBody'));
$pipe->pipe($apiPath, $app->make('Flarum\\Api\\Middleware\\FakeHttpMethods'));
$pipe->pipe($apiPath, $app->make('Flarum\\Http\\Middleware\\DispatchRoute', ['routes' => $app->make('flarum.api.routes')]));
$pipe->pipe($apiPath, $app->make('Flarum\\Api\\Middleware\\HandleErrors'));
} else {
$pipe->pipe($apiPath, function () {
$document = new Document();
$document->setErrors([['code' => 503, 'title' => 'Service Unavailable']]);
return new JsonApiResponse($document, 503);
});
}
return $pipe;
}
示例4: __construct
/**
* JsonResponse overloaded Constructor to assign JSON-API Content-Type
* and create JSON-API formatted response
*/
public function __construct(array $errors, int $status = 200, array $meta = null)
{
$document = new Document();
$document->setErrors($errors);
parent::__construct($document, $status);
}
示例5: processAction
public function processAction($args)
{
$document = new Document();
$element = NULL;
$meta = array();
try {
switch ($args['action']) {
case "init_repository":
$this->initRepository($args['psw']);
$session = $this->getSession();
$element = new Resource($session, new SessionSerializer());
break;
case "log_in":
$this->logIn($args['psw']);
$session = $this->getSession();
$element = new Resource($session, new SessionSerializer());
break;
case "log_out":
$this->logOut();
$session = $this->getSession();
$element = new Resource($session, new SessionSerializer());
break;
case "get_session":
$session = $this->getSession();
$element = new Resource($session, new SessionSerializer());
break;
case "get_post":
$postId = isset($args['id']) ? $args['id'] : NULL;
$post = $this->getPost($postId);
$element = (new Resource($post, new PostSerializer()))->fields(['tags']);
break;
case "get_posts":
$parameters = new Parameters($args);
$sort = $parameters->getSort();
if ($sort == NULL) {
$sort = array();
}
$limit = $parameters->getLimit();
if ($limit == NULL) {
$limit = -1;
}
$offset = $parameters->getOffset();
if ($offset == NULL) {
$offset = 0;
}
$posts = $this->getPosts($sort, $limit, $offset);
$element = (new Collection($posts, new PostSerializer()))->fields(['tags']);
break;
case "publish_post":
$id = $this->publishPost($args['title'], $args['body'], $args['tags'], $args['draft'], $args['token']);
$element = new Resource(new stdClass(), new ResultSerializer());
$document->addMeta("id", $id);
break;
case "update_post":
$id = $this->updatePost($args['id'], $args['title'], $args['body'], $args['tags'], $args['draft'], $args['token']);
$element = new Resource(new stdClass(), new ResultSerializer());
$document->addMeta("id", $id);
break;
case "delete_post":
$this->deletePost($args['id'], $args['token']);
$element = new Resource(new stdClass(), new ResultSerializer());
break;
default:
throw new Exception("unknown action " . $args['action'], self::EBL_ERROR_BADREQUEST);
break;
}
} catch (Exception $e) {
http_response_code($e->getCode());
$document->setErrors(array(array("message" => $e->getMessage())));
}
if ($element != NULL) {
$document->setData($element);
}
echo json_encode($document->toArray());
}
示例6: createErrorDocument
/**
* Create a JSON-API document with the given errors array.
*
* @param array $errors
* @return Document
*/
protected function createErrorDocument(array $errors)
{
$document = new Document();
$document->setErrors($errors);
return $document;
}