Skip to content

Commit 55b2ba8

Browse files
authored
Support late static binding on model factories (#26)
* Support late static binding on model factories * Make constructors final * Test fix
1 parent a7fba40 commit 55b2ba8

File tree

8 files changed

+79
-31
lines changed

8 files changed

+79
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 0.4.0
6+
7+
### Fixed
8+
9+
- Use late static binding on factories of Error
10+
511
## 0.3.0
612

713
### Added

src/Model/AbstractError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class AbstractError implements ResponseModelInterface
2727
* to occurrence of the problem, except for purposes of localization
2828
* @param int $httpStatusCode the HTTP status code applicable to this problem, expressed as a string value
2929
*/
30-
public function __construct(string $title, int $httpStatusCode)
30+
final public function __construct(string $title, int $httpStatusCode)
3131
{
3232
$this->title = $title;
3333
$this->httpStatusCode = $httpStatusCode;

src/Model/AbstractMeta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class AbstractMeta implements ResponseModelInterface
1414
private $meta;
1515
private $httpStatusCode;
1616

17-
public function __construct(array $meta, int $httpStatusCode)
17+
final public function __construct(array $meta, int $httpStatusCode)
1818
{
1919
$this->httpStatusCode = $httpStatusCode;
2020
$this->meta = $meta;

src/Model/Error.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,51 @@
99
*/
1010
class Error extends AbstractError
1111
{
12-
public static function error(string $title, int $httpCode): self
12+
/**
13+
* @return static
14+
*/
15+
public static function error(string $title, int $httpCode)
1316
{
14-
return new self($title, $httpCode);
17+
return new static($title, $httpCode);
1518
}
1619

17-
public static function serverError(string $title = 'Internal server error'): self
20+
/**
21+
* @return static
22+
*/
23+
public static function serverError(string $title = 'Internal server error')
1824
{
19-
return new self($title, 500);
25+
return new static($title, 500);
2026
}
2127

22-
public static function forbidden(string $title = 'Forbidden'): self
28+
/**
29+
* @return static
30+
*/
31+
public static function forbidden(string $title = 'Forbidden')
2332
{
24-
return new self($title, 403);
33+
return new static($title, 403);
2534
}
2635

27-
public static function notFound(string $title = 'Not Found'): self
36+
/**
37+
* @return static
38+
*/
39+
public static function notFound(string $title = 'Not Found')
2840
{
29-
return new self($title, 404);
41+
return new static($title, 404);
3042
}
3143

32-
public static function unauthorized(string $title = 'Unauthorized'): self
44+
/**
45+
* @return static
46+
*/
47+
public static function unauthorized(string $title = 'Unauthorized')
3348
{
34-
return new self($title, 401);
49+
return new static($title, 401);
3550
}
3651

37-
public static function invalid(string $title = 'Bad Request'): self
52+
/**
53+
* @return static
54+
*/
55+
public static function invalid(string $title = 'Bad Request')
3856
{
39-
return new self($title, 400);
57+
return new static($title, 400);
4058
}
4159
}

src/Model/ErrorWithViolation.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
*/
1212
class ErrorWithViolation extends AbstractError
1313
{
14-
public static function create(ConstraintViolationInterface $violation, string $title = 'Validation failed'): self
14+
/**
15+
* @return static
16+
*/
17+
public static function create(ConstraintViolationInterface $violation, string $title = 'Validation failed')
1518
{
16-
$model = new self($title, 400);
19+
$model = new static($title, 400);
1720
$model->setSource([
1821
'parameter' => $violation->getPropertyPath(),
1922
'message' => $violation->getMessage(),

src/Model/Message.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,35 @@
99
*/
1010
class Message extends AbstractMeta
1111
{
12-
public function __construct(string $message, int $httpStatusCode)
12+
/**
13+
* @return static
14+
*/
15+
final public static function create(string $message, int $httpStatusCode)
1316
{
14-
parent::__construct(['message' => $message], $httpStatusCode);
17+
return new static(['message' => $message], $httpStatusCode);
1518
}
1619

17-
public static function ok(string $message = 'OK'): self
20+
/**
21+
* @return static
22+
*/
23+
public static function ok(string $message = 'OK')
1824
{
19-
return new self($message, 200);
25+
return static::create($message, 200);
2026
}
2127

22-
public static function created(string $message = 'Created'): self
28+
/**
29+
* @return static
30+
*/
31+
public static function created(string $message = 'Created')
2332
{
24-
return new self($message, 201);
33+
return static::create($message, 201);
2534
}
2635

27-
public static function accepted(string $message = 'Accepted'): self
36+
/**
37+
* @return static
38+
*/
39+
public static function accepted(string $message = 'Accepted')
2840
{
29-
return new self($message, 202);
41+
return static::create($message, 202);
3042
}
3143
}

tests/Unit/Model/ErrorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,18 @@ public function testInvalid(): void
8383
$error->getErrorData()
8484
);
8585
}
86+
87+
public function testExtendError(): void
88+
{
89+
$error = AppError::invalid('someTitle');
90+
self::assertEquals(['payload' => 'custom'], $error->getPayload());
91+
}
92+
}
93+
94+
class AppError extends Error
95+
{
96+
public function getPayload(): array
97+
{
98+
return ['payload' => 'custom'];
99+
}
86100
}

tests/Unit/Model/MultipleErrorTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MultipleErrorTest extends TestCase
1616
{
1717
public function testAddError(): void
1818
{
19-
$error = new DummyError();
19+
$error = new DummyError('someTitle', 400);
2020
$multipleError = new MultipleError(400, []);
2121
$multipleError->addError($error);
2222

@@ -25,7 +25,7 @@ public function testAddError(): void
2525

2626
public function testGetPayload(): void
2727
{
28-
$error = new MultipleError(400, [new DummyError()]);
28+
$error = new MultipleError(400, [new DummyError('someTitle', 400)]);
2929
self::assertEquals(
3030
[
3131
'errors' => [
@@ -45,11 +45,6 @@ public function testGetHttpStatusCode(): void
4545

4646
class DummyError extends AbstractError
4747
{
48-
public function __construct()
49-
{
50-
parent::__construct('someTitle', 400);
51-
}
52-
5348
public function getErrorData(): array
5449
{
5550
return ['someKey' => 'someValue'];

0 commit comments

Comments
 (0)