Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ throw new ApiProblemException(
- [UnauthorizedProblem](#unauthorizedproblem)
- [ValidationApiProblem](#validationapiproblem)
- [BadRequestProblem](#badrequestproblem)
- [ConflictProblem](#conflictproblem)

#### ExceptionApiProblem

Expand Down Expand Up @@ -206,6 +207,21 @@ new BadRequestProblem('Bad request. Bad!.');
}
````

#### ConflictProblem

```php
use Phpro\ApiProblem\Http\ConflictProblem;
new ConflictProblem('Duplicated key for book with ID 20.');
```

```json
{
"status": 409,
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
"title": "Conflict",
"detail": "Duplicated key for book with ID 20."
}
````

### Creating your own problem

Expand All @@ -215,7 +231,7 @@ Since the RFC is very loose, we made the interface as easy as possible:
```php
use Phpro\ApiProblem\ApiProblemInterface;

class MyProblem implements ApiProbelmInterface
class MyProblem implements ApiProblemInterface
{
public function toArray(): array
{
Expand Down
37 changes: 37 additions & 0 deletions spec/Http/ConflictProblemSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace spec\Phpro\ApiProblem\Http;

use Phpro\ApiProblem\Http\ConflictProblem;
use Phpro\ApiProblem\Http\HttpApiProblem;
use PhpSpec\ObjectBehavior;

class ConflictProblemSpec extends ObjectBehavior
{
public function let(): void
{
$this->beConstructedWith('conflict');
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ConflictProblem::class);
}

public function it_is_an_http_api_problem(): void
{
$this->shouldHaveType(HttpApiProblem::class);
}

public function it_can_parse_to_array(): void
{
$this->toArray()->shouldBe([
'status' => 409,
'type' => HttpApiProblem::TYPE_HTTP_RFC,
'title' => HttpApiProblem::getTitleForStatusCode(409),
'detail' => 'conflict',
]);
}
}
15 changes: 15 additions & 0 deletions src/Http/ConflictProblem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Phpro\ApiProblem\Http;

class ConflictProblem extends HttpApiProblem
{
public function __construct(string $reason)
{
parent::__construct(409, [
'detail' => $reason,
]);
}
}