Skip to content

Commit

Permalink
general refactoring and code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
nnjeim committed Sep 19, 2021
1 parent abe285c commit ec491ec
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 234 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ All notable changes will be documented in this file
- adjusted translations and addition of respond with not authenticated helper.
- removed dependency on the Http Response facade.

## 1.0.2 - 2021-09-19
- general refactoring



49 changes: 25 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ use Nnjeim\Respond\Respond;
['response' => $response, 'status' => $status] = Fetch::setBaseUri('https://someapi.com/')->get('countries');
if ($status === 200 && $response->success) {
if ($status === 200 && $response->success) {
return Respond::setMessage('countries')
->toJson()
->withSuccess($response->data);
}
return Respond::toJson()
->setMessage('countries')
->withSuccess($response->data);
}
abort(400);
abort(400);
```
##### RespondHelper Instantiation

Expand All @@ -39,20 +39,21 @@ private RespondHelper $respond;
private array $data;
private bool $success;
public function __construct(RespondHelper $respond) {
$this->respond = $respond;
public function __construct(RespondHelper $respond)
{
$this->respond = $respond;
}
.
.
.
$respond = $this
->respond
->setMessage('countries')
->toJson();
->respond
->setMessage('countries')
->toJson();
if ($this->success) {
return $respond->withSuccess($data)
if ($this->success)
{
return $respond->withSuccess($data)
}
return $respond->withErrors();
Expand Down Expand Up @@ -139,7 +140,7 @@ Record not found error. The default response status code is 404.

##### Respond with not authenticated
```
Not authorized reponse. The default response status code is 401.
Not authenticated reponse. The default response status code is 401.
@return array withNotAuthenticated($message = null)
```
Expand All @@ -156,15 +157,15 @@ Not authorized reponse. The default response status code is 403.
```
@return array
[
'response' => [
'success' => true,
'message' => 'message',
'data' => [],
'errors' => [],
],
'status' => 200,
];
[
'response' => [
'success' => true,
'message' => 'message',
'data' => [],
'errors' => [],
],
'status' => 200,
];
```
## Testing

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 11 additions & 10 deletions src/Respond.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Support\Facades\Facade;

class Respond extends Facade {

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() {
return 'respond';
}
class Respond extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'respond';
}
}
79 changes: 79 additions & 0 deletions src/RespondBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Nnjeim\Respond;

use Illuminate\Http\JsonResponse;

abstract class RespondBuilder
{
protected array $attributes = [];

/**
* @param string $key
* @return mixed
*/
public function __get(string $key)
{
return $this->attributes[$key] ?? null;
}

/**
* @param $key
* @param $value
*/
public function __set($key, $value)
{
$this->attributes[$key] = $value;
}

/**
* @return array|JsonResponse
*/
public function respond()
{
$this->formatResponse();

['response' => $response, 'status' => $status] = $this->attributes;

return ($this->json === null) ? compact('response', 'status') : response()->json($response, $status);
}

private function formatResponse(): void
{
$response = [
'success' => false,
'data' => [],
'errors' => [],
];

if ($this->success !== null) {
$response = array_merge($response, [
'success' => $this->success,
]);
}

if ($this->message !== null) {
$response = array_merge([
'message' => $this->message,
], $response);
}

if ($this->data !== null) {
$response = array_merge($response, [
'data' => $this->data
]);
}

if ($this->errors !== null) {
$response = array_merge($response, [
'errors' => $this->errors
]);
}

if ($this->meta !== null) {
$response = array_merge($response, $this->meta);
}

$this->response = $response;
}
}
77 changes: 0 additions & 77 deletions src/RespondFactory.php

This file was deleted.

Loading

0 comments on commit ec491ec

Please sign in to comment.