Skip to content

Commit daf27c6

Browse files
Add Conflict Problem (#7)
* Add conflict problem * Fix typo
1 parent 44126d0 commit daf27c6

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ throw new ApiProblemException(
4141
- [UnauthorizedProblem](#unauthorizedproblem)
4242
- [ValidationApiProblem](#validationapiproblem)
4343
- [BadRequestProblem](#badrequestproblem)
44+
- [ConflictProblem](#conflictproblem)
4445

4546
#### ExceptionApiProblem
4647

@@ -206,6 +207,21 @@ new BadRequestProblem('Bad request. Bad!.');
206207
}
207208
````
208209

210+
#### ConflictProblem
211+
212+
```php
213+
use Phpro\ApiProblem\Http\ConflictProblem;
214+
new ConflictProblem('Duplicated key for book with ID 20.');
215+
```
216+
217+
```json
218+
{
219+
"status": 409,
220+
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
221+
"title": "Conflict",
222+
"detail": "Duplicated key for book with ID 20."
223+
}
224+
````
209225

210226
### Creating your own problem
211227

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

218-
class MyProblem implements ApiProbelmInterface
234+
class MyProblem implements ApiProblemInterface
219235
{
220236
public function toArray(): array
221237
{

spec/Http/ConflictProblemSpec.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\Phpro\ApiProblem\Http;
6+
7+
use Phpro\ApiProblem\Http\ConflictProblem;
8+
use Phpro\ApiProblem\Http\HttpApiProblem;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class ConflictProblemSpec extends ObjectBehavior
12+
{
13+
public function let(): void
14+
{
15+
$this->beConstructedWith('conflict');
16+
}
17+
18+
public function it_is_initializable(): void
19+
{
20+
$this->shouldHaveType(ConflictProblem::class);
21+
}
22+
23+
public function it_is_an_http_api_problem(): void
24+
{
25+
$this->shouldHaveType(HttpApiProblem::class);
26+
}
27+
28+
public function it_can_parse_to_array(): void
29+
{
30+
$this->toArray()->shouldBe([
31+
'status' => 409,
32+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
33+
'title' => HttpApiProblem::getTitleForStatusCode(409),
34+
'detail' => 'conflict',
35+
]);
36+
}
37+
}

src/Http/ConflictProblem.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\ApiProblem\Http;
6+
7+
class ConflictProblem extends HttpApiProblem
8+
{
9+
public function __construct(string $reason)
10+
{
11+
parent::__construct(409, [
12+
'detail' => $reason,
13+
]);
14+
}
15+
}

0 commit comments

Comments
 (0)