Skip to content

Commit

Permalink
Merge pull request #2 from LFSoftware/dextermb-patch-1
Browse files Browse the repository at this point in the history
Update response helpers
  • Loading branch information
jaredkove authored Jun 29, 2018
2 parents be324a8 + 6099090 commit d0bad9f
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 44 deletions.
168 changes: 124 additions & 44 deletions src/LangleyFoxall/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
@@ -1,78 +1,158 @@
<?php

namespace LangleyFoxall\Helpers;

use Illuminate\Http\Request;

class ApiResponse
class Response
{
/** @var int $status */
protected $status;
/** @var Request $request */
private $request;

/** @var bool $success */
protected $success;
/** @var string $type */
private $type;

/** @var array|null|string $error */
protected $error;
/** @var string $message */
private $message;

/** @var array|null $data */
protected $data;
/** @var array $data */
private $data;

/** @var array|null $meta */
protected $meta;
/** @var array $meta */
private $meta;

/** @var int $status */
private $status;

/** @var string $uri */
private $uri;

/**
* ApiResponse constructor.
* Response constructor.
*
* @param bool $success
* @param array|null|string $error
* @param array|null $data
* @param array|null $meta
* @param int $status
* @param Request $request
*/
public function __construct(bool $success = true, $error = null, $data = null, $meta = null, int $status = 200)
public function __construct(Request $request)
{
$this->status = $status;
$this->success = $success;
$this->error = $error;
$this->request = $request;
}

/**
* @param string $message
* @param array $data
* @param array $meta
* @param int $status
* @return $this
*/
public function success(string $message = null, array $data = null, array $meta = null, int $status = 200)
{
$this->type = 'success';
$this->message = $message;
$this->data = $data;
$this->meta = $meta;
$this->status = $status;

return $this;
}

/**
* @param array|null $data
* @param array|null $meta
* @param int $status
* @return ApiResponse
* @param mixed $message
* @param int $status
* @return $this
*/
public function error($message = null, int $status = 400)
{
$this->message = $message;
$this->status = $status;

return $this;
}

public function type(string $type)
{
$this->type = $type;

return $this;
}

/**
* @param string $message
* @return $this
*/
public static function success($data = null, $meta = null, int $status = 200)
public function message(string $message)
{
$response = new self;
$this->message = $message;

$response->status = $status;
$response->data = $data;
$response->meta = $meta;
return $this;
}

return $response;
/**
* @param array $data
* @return $this
*/
public function data(array $data = null)
{
$this->data = $data;

return $this;
}

/**
* @param array $meta
* @return $this
*/
public function meta(array $meta = null)
{
$this->meta = $meta;

return $this;
}

/**
* @param mixed $error
* @param int $status
* @return ApiResponse
* @return $this
*/
public static function error($error = null, int $status = 400)
public function status(int $status)
{
$response = new self;
$this->status = $status;

$response->status = $status;
$response->success = false;
$response->error = $error;
return $this;
}

return $response;
/**
* @param string $uri
* @return $this
*/
public function redirect(string $uri)
{
$this->uri = $uri;

return $this;
}

public function json()
/**
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function end()
{
return response()->json(get_object_vars($this))
->setStatusCode($this->status);
}}
$type = $this->type;
$message = $this->message;
$status = $this->status;
$data = $this->data;
$meta = $this->meta;

if ($this->request->expectsJson()) {
if ($type === 'error') {
return ApiResponse::error($message, $status)->json();
}

return ApiResponse::success($data, $meta, $status)->json();
}


if ($this->uri) {
return redirect($this->uri);
}

return back()->with($type, $message);
}
}
158 changes: 158 additions & 0 deletions src/LangleyFoxall/Helpers/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
namespace LangleyFoxall\Helpers;

use Illuminate\Http\Request;

class Response
{
/** @var Request $request */
private $request;

/** @var string $type */
private $type;

/** @var string $message */
private $message;

/** @var array $data */
private $data;

/** @var array $meta */
private $meta;

/** @var int $status */
private $status;

/** @var string $uri */
private $uri;

/**
* Response constructor.
*
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}

/**
* @param string $message
* @param array $data
* @param array $meta
* @param int $status
* @return $this
*/
public function success(string $message = null, array $data = null, array $meta = null, int $status = 200)
{
$this->type = 'success';
$this->message = $message;
$this->data = $data;
$this->meta = $meta;
$this->status = $status;

return $this;
}

/**
* @param mixed $message
* @param int $status
* @return $this
*/
public function error($message = null, int $status = 400)
{
$this->message = $message;
$this->status = $status;

return $this;
}

public function type(string $type)
{
$this->type = $type;

return $this;
}

/**
* @param string $message
* @return $this
*/
public function message(string $message)
{
$this->message = $message;

return $this;
}

/**
* @param array $data
* @return $this
*/
public function data(array $data = null)
{
$this->data = $data;

return $this;
}

/**
* @param array $meta
* @return $this
*/
public function meta(array $meta = null)
{
$this->meta = $meta;

return $this;
}

/**
* @param int $status
* @return $this
*/
public function status(int $status)
{
$this->status = $status;

return $this;
}

/**
* @param string $uri
* @return $this
*/
public function redirect(string $uri)
{
$this->uri = $uri;

return $this;
}

/**
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function end()
{
$type = $this->type;
$message = $this->message;
$status = $this->status;
$data = $this->data;
$meta = $this->meta;

if ($this->request->expectsJson()) {
if ($type === 'error') {
return ApiResponse::error($message, $status)->json();
}

return ApiResponse::success($data, $meta, $status)->json();
}


if ($this->uri) {
return redirect($this->uri);
}

return back()->with($type, $message);
}
}

0 comments on commit d0bad9f

Please sign in to comment.