ticketリソース
https://bearsunday.github.io/manuals/1.0/ja/tutorial2.html#tikcetリソース
src/Resource/App/Ticket.phpに以下を記述。
<?php
declare(strict_types=1);
namespace MyVendor\Ticket\Resource\App;
use BEAR\Resource\Annotation\JsonSchema;
use BEAR\Resource\ResourceObject;
use MyVendor\Ticket\Query\TicketQueryInterface;
class Ticket extends ResourceObject
{
public function __construct(
private TicketQueryInterface $query
){}
#[JsonSchema("ticket.json")]
public function onGet(string $id = ''): static
{
$this->body = $this->query->item($id);
return $this;
}
}
php bin/app.php get '/ticket?id=1'を実行すると、以下のエラーになる。
shotamiyazaki@kiseki0001noMacBook-Air MyVendor.Ticket % php bin/app.php get '/ticket?id=1'
500 Internal Server Error
content-type: application/vnd.error+json
{
"message": "Internal Server Error",
"logref": "072c4608",
"request": "get app://self/ticket?id=1",
"exceptions": "BEAR\\Resource\\Exception\\JsonSchemaException([id] The property id is required; [title] The property title is required; [date_created] The property date_created is required; [] Array value found, but an array is required; by /Users/shotamiyazaki/PhpstormProjects/MyVendor.Ticket/var/schema/response/ticket.json)",
"file": "/Users/shotamiyazaki/PhpstormProjects/MyVendor.Ticket/vendor/bear/resource/src/JsonSchema/Interceptor/JsonSchemaInterceptor.php(168)"
}
src/Resource/App/Ticket.phpの#[JsonSchema("ticket.json")]の部分をコメントアウトすると、エラーになることなく、以下のデータが取得できる。
shotamiyazaki@kiseki0001noMacBook-Air MyVendor.Ticket % php bin/app.php get '/ticket?id=1'
200 OK
Content-Type: application/hal+json
{
"0": {
"id": "1",
"title": "foo",
"date_created": "1970-01-01 00:00:00"
},
"_embedded": {
"project": {
"title": "Project A",
"_links": {
"self": {
"href": "/project"
}
}
}
},
"_links": {
"self": {
"href": "/ticket?id=1"
}
}
}
ticketsリソース
https://bearsunday.github.io/manuals/1.0/ja/tutorial2.html#ticketsリソース
src/resource/App/Tickets.phpに以下を記述
<?php
declare(strict_types=1);
namespace MyVendor\Ticket\Resource\App;
use BEAR\Resource\Annotation\JsonSchema;
use BEAR\Resource\Annotation\Link;
use BEAR\Resource\ResourceObject;
use Koriym\HttpConstants\ResponseHeader;
use Koriym\HttpConstants\StatusCode;
use MyVendor\Ticket\Query\TicketCommandInterface;
use MyVendor\Ticket\Query\TicketQueryInterface;
use Ray\IdentityValueModule\UuidInterface;
use function uri_template;
class Tickets extends ResourceObject
{
public function __construct(
private TicketQueryInterface $query,
private TicketCommandInterface $command,
private UuidInterface $uuid
){}
#[Link(rel: "doPost", href: '/tickets')]
#[Link(rel: "goTicket", href: '/ticket{?id}')]
#[JsonSchema("tickets.json")]
public function onGet(): static
{
$this->body = [
'tickets' => $this->query->list()
];
return $this;
}
#[Link(rel: "goTickets", href: '/tickets')]
public function onPost(string $title): static
{
$id = (string) $this->uuid;
$this->command->add($id, $title);
$this->code = StatusCode::CREATED;
$this->headers[ResponseHeader::LOCATION] = uri_template('/ticket{?id}', ['id' => $id]);
return $this;
}
}
php bin/app.php get '/tickets'を実行すると、以下のエラーになる。
shotamiyazaki@kiseki0001noMacBook-Air MyVendor.Ticket % php bin/app.php get '/tickets'
500 Internal Server Error
content-type: application/vnd.error+json
{
"message": "Internal Server Error",
"logref": "a606b750",
"request": "get app://self/tickets",
"exceptions": "BEAR\\Resource\\Exception\\JsonSchemaException([tickets[0]] Object value found, but an array is required; [tickets[1]] Object value found, but an array is required; [tickets[2]] Object value found, but an array is required; by /Users/shotamiyazaki/PhpstormProjects/MyVendor.Ticket/var/schema/response/tickets.json)",
"file": "/Users/shotamiyazaki/PhpstormProjects/MyVendor.Ticket/vendor/bear/resource/src/JsonSchema/Interceptor/JsonSchemaInterceptor.php(168)"
}
src/Resource/App/Tickets.phpの#[JsonSchema("ticket.json")]の部分をコメントアウトすると、エラーになることなく、以下のデータが取得できる。
shotamiyazaki@kiseki0001noMacBook-Air MyVendor.Ticket % php bin/app.php get '/tickets'
200 OK
Content-Type: application/hal+json
{
"tickets": [
{
"id": "1",
"title": "foo",
"date_created": "1970-01-01 00:00:00"
},
{
"id": "e8835c88-a834-427a-a4ef-13566cfc3889",
"title": "ffff",
"date_created": "2022-06-03 09:25:48"
},
{
"id": "c4009526-7913-4d69-89d2-ad36868eacda",
"title": "test",
"date_created": "2022-06-06 00:09:55"
}
],
"_links": {
"self": {
"href": "/tickets"
},
"doPost": {
"href": "/tickets"
},
"goTicket": {
"href": "/ticket"
}
}
}