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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Model/AbstractError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/AbstractMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 30 additions & 12 deletions src/Model/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 5 additions & 2 deletions src/Model/ErrorWithViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
28 changes: 20 additions & 8 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Model/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
9 changes: 2 additions & 7 deletions tests/Unit/Model/MultipleErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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' => [
Expand All @@ -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'];
Expand Down