Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
semiherdogan committed Jan 26, 2021
1 parent e2f789b commit 6dbce94
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Easily respond to api requests.

# Requirement
> Laravel >= 5.5
# Installation

You can install the package via composer:
Expand Down Expand Up @@ -70,6 +73,26 @@ Content-Type: application/json
}
```

### All methods that responder has.
#### send
- Parameter `null|mixed $data` (if set calls setData method behind the scenes)
#### setHttpStatusCode
- Parameter `int $statusCode`
#### setResponseMeta
- Parameter `array $responseMeta`
#### setResponseCode
- Parameter `int $responseCode`
#### setResponseMessage
- Parameter `string $responseMessage`
#### setData
- Parameter `mixed $data`
#### addExtraData
- Parameter `string $key`
- Parameter `mixed $value`
#### addHeader
- Parameter `string $key`
- Parameter `string|integer|numeric $value`

# Testing

// TODO:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
},
"autoload": {
"psr-4": {
"LaraToolbox\\responder\\": "src"
"LaraToolbox\\Responder\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"LaraToolbox\\responder\\Tests\\": "tests"
"LaraToolbox\\Responder\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -44,7 +44,7 @@
"extra": {
"laravel": {
"providers": [
"LaraToolbox\\responder\\PackageServiceProvider"
"LaraToolbox\\Responder\\PackageServiceProvider"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ResponderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function report()
public function render()
{
return responder()
->setResponseCode($this->responseCode)
->setResponseMeta($this->responseCode)
->setData($this->responseData)
->send();
}
Expand Down
8 changes: 0 additions & 8 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@

class PackageServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
//
}

/**
* Register the application services.
*/
Expand Down
43 changes: 18 additions & 25 deletions src/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public function send($data = null)
return $this->sendResponse();
}

/**
* @param int $httpStatus
* @return \LaraToolbox\Responder\Responder
*/
public function setStatusCode(int $httpStatus): self
public function setHttpStatusCode(int $httpStatus): self
{
$this->httpStatus = $httpStatus;

Expand All @@ -41,52 +37,49 @@ public function setStatusCode(int $httpStatus): self
*
* @see \LaraToolbox\Responder\ResponseCodes
*/
public function setResponseCode($response): self
public function setResponseMeta(array $response): self
{
$this->responseCode = $response['code'];
$this->responseMessage = $response['message'];
$this->setResponseCode($response['code']);
$this->setResponseMessage($response['message']);

return $this;
}

public function setResponseCode(int $code): self
{
$this->responseCode = $code;

return $this;
}

public function setResponseMessage(?string $message): self
{
$this->responseMessage = $message;

return $this;
}

/**
* @param mixed $data
* @return \LaraToolbox\Responder\Responder
*/
public function setData($data): self
{
$this->responseData = $data;

return $this;
}

/**
* @param $key
* @param $value
* @return \LaraToolbox\Responder\Responder
*/
public function addExtraData($key, $value): self
{
$this->extraData[$key] = $value;

return $this;
}

/**
* @param $key
* @param $value
* @return \LaraToolbox\Responder\Responder
*/
public function addHeader($key, $value): self
{
$this->headers[$key] = $value;

return $this;
}

/**
* @return \Illuminate\Http\JsonResponse
*/
private function sendResponse()
{
return response()
Expand Down

0 comments on commit 6dbce94

Please sign in to comment.