diff --git a/CHANGELOG.md b/CHANGELOG.md index d04ec0d..57eb3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. +## 0.4.0 + +### Fixed + +- Use late static binding on factories of Error + ## 0.3.0 ### Added diff --git a/src/Model/AbstractError.php b/src/Model/AbstractError.php index 6e6f1d0..5b4226d 100644 --- a/src/Model/AbstractError.php +++ b/src/Model/AbstractError.php @@ -27,7 +27,7 @@ abstract class AbstractError implements ResponseModelInterface * to occurrence of the problem, except for purposes of localization * @param int $httpStatusCode the HTTP status code applicable to this problem, expressed as a string value */ - public function __construct(string $title, int $httpStatusCode) + final public function __construct(string $title, int $httpStatusCode) { $this->title = $title; $this->httpStatusCode = $httpStatusCode; diff --git a/src/Model/AbstractMeta.php b/src/Model/AbstractMeta.php index d661d6c..ae0afad 100644 --- a/src/Model/AbstractMeta.php +++ b/src/Model/AbstractMeta.php @@ -14,7 +14,7 @@ abstract class AbstractMeta implements ResponseModelInterface private $meta; private $httpStatusCode; - public function __construct(array $meta, int $httpStatusCode) + final public function __construct(array $meta, int $httpStatusCode) { $this->httpStatusCode = $httpStatusCode; $this->meta = $meta; diff --git a/src/Model/Error.php b/src/Model/Error.php index ac11459..d54b22f 100644 --- a/src/Model/Error.php +++ b/src/Model/Error.php @@ -9,33 +9,51 @@ */ class Error extends AbstractError { - public static function error(string $title, int $httpCode): self + /** + * @return static + */ + public static function error(string $title, int $httpCode) { - return new self($title, $httpCode); + return new static($title, $httpCode); } - public static function serverError(string $title = 'Internal server error'): self + /** + * @return static + */ + public static function serverError(string $title = 'Internal server error') { - return new self($title, 500); + return new static($title, 500); } - public static function forbidden(string $title = 'Forbidden'): self + /** + * @return static + */ + public static function forbidden(string $title = 'Forbidden') { - return new self($title, 403); + return new static($title, 403); } - public static function notFound(string $title = 'Not Found'): self + /** + * @return static + */ + public static function notFound(string $title = 'Not Found') { - return new self($title, 404); + return new static($title, 404); } - public static function unauthorized(string $title = 'Unauthorized'): self + /** + * @return static + */ + public static function unauthorized(string $title = 'Unauthorized') { - return new self($title, 401); + return new static($title, 401); } - public static function invalid(string $title = 'Bad Request'): self + /** + * @return static + */ + public static function invalid(string $title = 'Bad Request') { - return new self($title, 400); + return new static($title, 400); } } diff --git a/src/Model/ErrorWithViolation.php b/src/Model/ErrorWithViolation.php index 33514ae..642d516 100644 --- a/src/Model/ErrorWithViolation.php +++ b/src/Model/ErrorWithViolation.php @@ -11,9 +11,12 @@ */ class ErrorWithViolation extends AbstractError { - public static function create(ConstraintViolationInterface $violation, string $title = 'Validation failed'): self + /** + * @return static + */ + public static function create(ConstraintViolationInterface $violation, string $title = 'Validation failed') { - $model = new self($title, 400); + $model = new static($title, 400); $model->setSource([ 'parameter' => $violation->getPropertyPath(), 'message' => $violation->getMessage(), diff --git a/src/Model/Message.php b/src/Model/Message.php index 342a4c8..92a959e 100644 --- a/src/Model/Message.php +++ b/src/Model/Message.php @@ -9,23 +9,35 @@ */ class Message extends AbstractMeta { - public function __construct(string $message, int $httpStatusCode) + /** + * @return static + */ + final public static function create(string $message, int $httpStatusCode) { - parent::__construct(['message' => $message], $httpStatusCode); + return new static(['message' => $message], $httpStatusCode); } - public static function ok(string $message = 'OK'): self + /** + * @return static + */ + public static function ok(string $message = 'OK') { - return new self($message, 200); + return static::create($message, 200); } - public static function created(string $message = 'Created'): self + /** + * @return static + */ + public static function created(string $message = 'Created') { - return new self($message, 201); + return static::create($message, 201); } - public static function accepted(string $message = 'Accepted'): self + /** + * @return static + */ + public static function accepted(string $message = 'Accepted') { - return new self($message, 202); + return static::create($message, 202); } } diff --git a/tests/Unit/Model/ErrorTest.php b/tests/Unit/Model/ErrorTest.php index 849d51c..62732dc 100644 --- a/tests/Unit/Model/ErrorTest.php +++ b/tests/Unit/Model/ErrorTest.php @@ -83,4 +83,18 @@ public function testInvalid(): void $error->getErrorData() ); } + + public function testExtendError(): void + { + $error = AppError::invalid('someTitle'); + self::assertEquals(['payload' => 'custom'], $error->getPayload()); + } +} + +class AppError extends Error +{ + public function getPayload(): array + { + return ['payload' => 'custom']; + } } diff --git a/tests/Unit/Model/MultipleErrorTest.php b/tests/Unit/Model/MultipleErrorTest.php index 77f67f1..4909d7c 100644 --- a/tests/Unit/Model/MultipleErrorTest.php +++ b/tests/Unit/Model/MultipleErrorTest.php @@ -16,7 +16,7 @@ class MultipleErrorTest extends TestCase { public function testAddError(): void { - $error = new DummyError(); + $error = new DummyError('someTitle', 400); $multipleError = new MultipleError(400, []); $multipleError->addError($error); @@ -25,7 +25,7 @@ public function testAddError(): void public function testGetPayload(): void { - $error = new MultipleError(400, [new DummyError()]); + $error = new MultipleError(400, [new DummyError('someTitle', 400)]); self::assertEquals( [ 'errors' => [ @@ -45,11 +45,6 @@ public function testGetHttpStatusCode(): void class DummyError extends AbstractError { - public function __construct() - { - parent::__construct('someTitle', 400); - } - public function getErrorData(): array { return ['someKey' => 'someValue'];