Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Improved coverage data #56

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "^7.2.5|^8.0",
"ext-json": "*",
"andrey-helldar/support": "^1.30.1",
"andrey-helldar/support": "^2.0",
"symfony/http-foundation": "^4.0|^5.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Helldar\ApiResponse\Concerns;

use Helldar\Support\Facades\Is;
use Helldar\Support\Facades\Helpers\Is;

trait Errors
{
Expand Down
14 changes: 0 additions & 14 deletions src/Concerns/Exceptions/Laravel/Api.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Concerns/Exceptions/Laravel/Web.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Contracts/Parseable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Helldar\ApiResponse\Contracts;

/** @mixin \Helldar\Support\Traits\Makeable */
/** @mixin \Helldar\Support\Concerns\Makeable */
interface Parseable
{
public function setData($data): self;
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Helldar\ApiResponse\Contracts;

/** @mixin \Helldar\Support\Traits\Makeable */
/** @mixin \Helldar\Support\Concerns\Makeable */
interface Wrapper
{
public function wrap(bool $wrap = true): self;
Expand Down
13 changes: 12 additions & 1 deletion src/Exceptions/Laravel/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Helldar\ApiResponse\Exceptions\Laravel;

use Helldar\Support\Facades\Arr;
use Helldar\Support\Facades\Helpers\Arr;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -38,6 +38,17 @@ protected function prepareJsonResponse($request, Throwable $e)
);
}

protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
if ($e->response) {
return $e->response;
}

return $this->isJson($request)
? $this->invalidJson($request, $e)
: $this->invalid($request, $e);
}

protected function getExceptionMessage(Throwable $e)
{
$converted = parent::convertExceptionToArray($e);
Expand Down
8 changes: 5 additions & 3 deletions src/Exceptions/Laravel/Eight/ApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Helldar\ApiResponse\Exceptions\Laravel\Eight;

use Helldar\ApiResponse\Concerns\Exceptions\Laravel\Api;
use Helldar\ApiResponse\Exceptions\Laravel\BaseHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Support\Responsable;
Expand All @@ -13,8 +12,6 @@

abstract class ApiHandler extends BaseHandler
{
use Api;

public function render($request, Throwable $e)
{
if (method_exists($e, 'render') && $response = $e->render($request)) {
Expand Down Expand Up @@ -47,4 +44,9 @@ public function render($request, Throwable $e)

return $this->prepareJsonResponse($request, $e);
}

protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
return $this->response($e);
}
}
3 changes: 0 additions & 3 deletions src/Exceptions/Laravel/Eight/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Helldar\ApiResponse\Exceptions\Laravel\Eight;

use Helldar\ApiResponse\Concerns\Exceptions\Laravel\Web;
use Helldar\ApiResponse\Exceptions\Laravel\BaseHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Support\Responsable;
Expand All @@ -13,8 +12,6 @@

abstract class Handler extends BaseHandler
{
use Web;

public function render($request, Throwable $e)
{
if (method_exists($e, 'render') && $response = $e->render($request)) {
Expand Down
8 changes: 5 additions & 3 deletions src/Exceptions/Laravel/Seven/ApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Helldar\ApiResponse\Exceptions\Laravel\Seven;

use Helldar\ApiResponse\Concerns\Exceptions\Laravel\Api;
use Helldar\ApiResponse\Exceptions\Laravel\BaseHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Support\Responsable;
Expand All @@ -13,8 +12,6 @@

abstract class ApiHandler extends BaseHandler
{
use Api;

public function render($request, Throwable $e)
{
if (method_exists($e, 'render') && $response = $e->render($request)) {
Expand All @@ -37,4 +34,9 @@ public function render($request, Throwable $e)

return $this->prepareJsonResponse($request, $e);
}

protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
return $this->response($e);
}
}
3 changes: 0 additions & 3 deletions src/Exceptions/Laravel/Seven/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Helldar\ApiResponse\Exceptions\Laravel\Seven;

use Helldar\ApiResponse\Concerns\Exceptions\Laravel\Web;
use Helldar\ApiResponse\Exceptions\Laravel\BaseHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Support\Responsable;
Expand All @@ -13,8 +12,6 @@

abstract class Handler extends BaseHandler
{
use Web;

public function render($request, Throwable $e)
{
if (method_exists($e, 'render') && $response = $e->render($request)) {
Expand Down
42 changes: 25 additions & 17 deletions src/Parsers/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
namespace Helldar\ApiResponse\Parsers;

use Exception as BaseException;
use Helldar\Support\Facades\Instance;
use Helldar\Support\Facades\Helpers\Instance;
use Throwable;

/**
* @property \Exception|\Throwable $data
*/
/** @property \Exception|\Throwable $data */
final class Exception extends Parser
{
protected $is_error = true;

public function getData()
{
if ($data = $this->getThrowableContent()) {
return $data;
}

return $this->data;
return $this->getThrowableContent() ?: $this->data;
}

public function getStatusCode(): int
Expand All @@ -28,20 +22,34 @@ public function getStatusCode(): int
return $this->status_code;
}

if ($code = Instance::callsWhenNotEmpty($this->data, ['getStatusCode', 'getCode'], 400)) {
return $code;
}

return parent::getStatusCode();
return $this->callStatusCode() ?: parent::getStatusCode();
}

/**
* @return array|int|string|null
*/
protected function getThrowableContent()
{
return Instance::of($this->data, [BaseException::class, Throwable::class])
? Instance::callsWhenNotEmpty($this->data, ['getOriginalContent', 'getContent', 'getResponse', 'getMessage'])
: null;
return $this->isThrowable() ? $this->callContent() : null;
}

protected function callStatusCode(): ?int
{
return $this->call(['getStatusCode', 'getCode']);
}

protected function callContent()
{
return $this->call(['getOriginalContent', 'getContent', 'getResponse', 'getMessage']);
}

protected function isThrowable(): bool
{
return Instance::of($this->data, [BaseException::class, Throwable::class]);
}

protected function call(array $methods)
{
return Instance::callWhen($this->data, $methods);
}
}
2 changes: 1 addition & 1 deletion src/Parsers/Laravel/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Helldar\ApiResponse\Parsers\Laravel;

use Helldar\ApiResponse\Parsers\Parser;
use Helldar\Support\Facades\Arr;
use Helldar\Support\Facades\Helpers\Arr;
use Illuminate\Http\JsonResponse;

/** @property \Illuminate\Http\Resources\Json\JsonResource $data */
Expand Down
6 changes: 3 additions & 3 deletions src/Parsers/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Helldar\ApiResponse\Concerns\Errors;
use Helldar\ApiResponse\Contracts\Parseable;
use Helldar\ApiResponse\Services\Response;
use Helldar\Support\Facades\Instance;
use Helldar\Support\Facades\Is;
use Helldar\Support\Traits\Makeable;
use Helldar\Support\Concerns\Makeable;
use Helldar\Support\Facades\Helpers\Instance;
use Helldar\Support\Facades\Helpers\Is;

abstract class Parser implements Parseable
{
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Helldar\ApiResponse\Wrappers\Error;
use Helldar\ApiResponse\Wrappers\Resolver;
use Helldar\ApiResponse\Wrappers\Success;
use Helldar\Support\Traits\Makeable;
use Helldar\Support\Concerns\Makeable;
use Symfony\Component\HttpFoundation\JsonResponse;

final class Response implements Responsable
Expand Down
6 changes: 3 additions & 3 deletions src/Support/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Helldar\ApiResponse\Parsers\Laravel\Resource as LaravelResourceParser;
use Helldar\ApiResponse\Parsers\Laravel\Validation as LaravelValidationParser;
use Helldar\ApiResponse\Parsers\Main;
use Helldar\Support\Facades\Instance;
use Helldar\Support\Facades\Is;
use Helldar\Support\Traits\Makeable;
use Helldar\Support\Concerns\Makeable;
use Helldar\Support\Facades\Helpers\Instance;
use Helldar\Support\Facades\Helpers\Is;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Validation\ValidationException;
use Throwable;
Expand Down
4 changes: 2 additions & 2 deletions src/Wrappers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Helldar\ApiResponse\Wrappers;

use Helldar\ApiResponse\Contracts\Resolver as ResolverContract;
use Helldar\Support\Facades\Arr;
use Helldar\Support\Traits\Makeable;
use Helldar\Support\Concerns\Makeable;
use Helldar\Support\Facades\Helpers\Arr;

final class Resolver implements ResolverContract
{
Expand Down
4 changes: 2 additions & 2 deletions src/Wrappers/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Helldar\ApiResponse\Contracts\Parseable;
use Helldar\ApiResponse\Contracts\Resolver;
use Helldar\ApiResponse\Contracts\Wrapper as WrapperContract;
use Helldar\Support\Facades\Arr;
use Helldar\Support\Traits\Makeable;
use Helldar\Support\Concerns\Makeable;
use Helldar\Support\Facades\Helpers\Arr;

abstract class Wrapper implements WrapperContract
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Concerns/Laravel/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests\Fixtures\Concerns\Laravel;

use Helldar\Support\Facades\Str;
use Helldar\Support\Facades\Helpers\Str;
use Illuminate\Foundation\Application as LaravelApp;

trait Application
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Entities/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests\Fixtures\Entities;

use Helldar\Support\Facades\Instance;
use Helldar\Support\Facades\Helpers\Instance;
use Illuminate\Testing\TestResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response as MainResponse;
Expand Down