Skip to content

Commit

Permalink
Merge pull request #1 from PhpSlides/master
Browse files Browse the repository at this point in the history
Updated enum types
  • Loading branch information
dconco authored Aug 22, 2024
2 parents 3bff03b + d9342fa commit 42bcab3
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 64 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"email": "[email protected]",
"homepage": "https://dconco.github.io"
}],
"version": "0.0.1",
"require": {
"php": ">=8.2"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/ApiException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php


namespace PhpSlides\Exception;
namespace PhpSlides\Status\Exception;

use Exception;
use PhpSlides\Exception;

class ApiException extends Exception implements ExceptionInterface
{
Expand Down
7 changes: 3 additions & 4 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace PhpSlides\Exception;
namespace PhpSlides\Status\Exception;

use Throwable;
use PhpSlides\Interface\SlidesException;

interface ExceptionInterface extends Throwable
interface ExceptionInterface extends SlidesException
{

}
6 changes: 0 additions & 6 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

use DOMDocument;
use PhpSlides\StatusCode;
use PhpSlides\Enums\ResponseType;
use PhpSlides\Interface\ResponseInterface;

class Response implements ResponseInterface
{
const JSON = ResponseType::JSON;
const HTML = ResponseType::HTML;
const CSV = ResponseType::CSV;
const XML = ResponseType::XML;

/**
* The function `json` in PHP sets the response header to indicate JSON content and returns the
* JSON-encoded data along with the specified HTTP status code.
Expand Down
37 changes: 19 additions & 18 deletions src/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpSlides;

use PhpSlides\Http\Response;
use PhpSlides\Enums\ResponseType;
use PhpSlides\Interface\StatusInterface;

class Status implements StatusInterface
Expand All @@ -21,9 +22,9 @@ class Status implements StatusInterface
* `Response::HTML`, `Response::CSV`, or `Response::XML`. If the provided `response` value is not one
* of
*/
public function __construct (string $response = Response::JSON)
public function __construct (string $response = ResponseType::JSON)
{
$responseTypes = [ Response::JSON, Response::HTML, Response::CSV, Response::XML ];
$responseTypes = [ ResponseType::JSON, ResponseType::HTML, ResponseType::CSV, ResponseType::XML ];

if (!in_array($response, $responseTypes))
{
Expand Down Expand Up @@ -83,13 +84,13 @@ public function get (): string|array

switch ($this->response)
{
case (Response::JSON):
case (ResponseType::JSON):
return Response::json($data, $this->status);
case (Response::HTML):
case (ResponseType::HTML):
return Response::html($data, $this->status);
case (Response::XML):
case (ResponseType::XML):
return Response::xml($data, $this->status);
case (Response::CSV):
case (ResponseType::CSV):
return Response::csv($data, $this->status);
default:
return Response::array($data, $this->status);
Expand Down Expand Up @@ -180,7 +181,7 @@ public function setMessage (mixed $message): void
* This PHP function handles errors by formatting the error data based on the response type and status
* code.
*
* @param array data The `data` parameter in the `error` function can accept either an array or a
* @param string|array data The `data` parameter in the `error` function can accept either an array or a
* string. It is used to provide the error message or data that needs to be returned in the response.
* @param int status The `status` parameter in the `error` function is an optional integer parameter
* that represents the HTTP status code to be returned in the response. If not provided, the default
Expand All @@ -191,19 +192,19 @@ public function setMessage (mixed $message): void
* the value of the `` property. The response can be in JSON, HTML, XML, CSV, or as a plain
* array.
*/
public function error (array|string $data, int $status = StatusCode::INTERNAL_SERVER_ERROR): string|array
public function error (string|array $data, int $status = StatusCode::INTERNAL_SERVER_ERROR): string|array
{
$data = [ 'error' => $data ];

switch ($this->response)
{
case (Response::JSON):
case (ResponseType::JSON):
return Response::json($data, $status);
case (Response::HTML):
case (ResponseType::HTML):
return Response::html($data, $status);
case (Response::XML):
case (ResponseType::XML):
return Response::xml($data, $status);
case (Response::CSV):
case (ResponseType::CSV):
return Response::csv($data, $status);
default:
return Response::array($data, $status);
Expand All @@ -214,7 +215,7 @@ public function error (array|string $data, int $status = StatusCode::INTERNAL_SE
* This PHP function returns a success response in various formats based on the specified response
* type.
*
* @param array data The `data` parameter in the `success` function can be either an array or a
* @param string|array data The `data` parameter in the `success` function can be either an array or a
* string. It is wrapped in an array with the key 'success' before being returned based on the
* response type specified.
* @param int status The `status` parameter in the `success` function represents the HTTP status code
Expand All @@ -228,19 +229,19 @@ public function error (array|string $data, int $status = StatusCode::INTERNAL_SE
* type. The specific response format returned depends on the value of `->response` which can be
* JSON, HTML, XML,
*/
public function success (array|string $data, int $status = StatusCode::OK): string|array
public function success (string|array $data, int $status = StatusCode::OK): string|array
{
$data = [ 'success' => $data ];

switch ($this->response)
{
case (Response::JSON):
case (ResponseType::JSON):
return Response::json($data, $status);
case (Response::HTML):
case (ResponseType::HTML):
return Response::html($data, $status);
case (Response::XML):
case (ResponseType::XML):
return Response::xml($data, $status);
case (Response::CSV):
case (ResponseType::CSV):
return Response::csv($data, $status);
default:
return Response::array($data, $status);
Expand Down
117 changes: 90 additions & 27 deletions src/interface/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,104 @@
namespace PhpSlides\Interface;

use PhpSlides\StatusCode;
use PhpSlides\Enums\ResponseType;

interface ResponseInterface
{
const JSON = ResponseType::JSON;
const HTML = ResponseType::HTML;
const CSV = ResponseType::CSV;
const XML = ResponseType::XML;

/**
* Handle response to Json
* The function `json` in PHP sets the response header to indicate JSON content and returns the
* JSON-encoded data along with the specified HTTP status code.
*
* @param array data The `data` parameter is an array that contains the data you want to encode as
* JSON. This data will be converted to a JSON string using the `json_encode` function before being
* returned by the `json` method.
* @param int status The `status` parameter in the `json` function is used to specify the HTTP status
* code that will be returned in the response. It has a default value of `StatusCode::OK`, which
* typically corresponds to the HTTP status code `200 OK`. This parameter allows you to customize the
* status code based
*
* @return string The `json` method returns a JSON-encoded string representation of the provided data
* array.
*/
public static function json(
array $data = [],
int $status = StatusCode::OK,
public static function json (
array $data = [],
int $status = StatusCode::OK,
): string;

public static function html(
array $data = [],
int $status = StatusCode::OK,

/**
* The function generates an HTML unordered list based on the provided data array with key-value
* pairs.
*
* @param array data The `data` parameter in the `html` function is an array that contains key-value
* pairs. The keys are used as labels and the values are the corresponding data to be displayed in the
* HTML output.
* @param int status The `status` parameter in the `html` function represents the HTTP status code
* that will be set in the response header. It is of type integer and is set to a default value of
* `StatusCode::OK`. This parameter allows you to specify the HTTP status code to be returned in the
* response.
*
* @return string The `html` function is returning an HTML string generated using the provided data
* array. The function sets the content type to text/html and the HTTP response code based on the
* status parameter. It then creates a DOMDocument object, generates a list (<ul>) element, and
* populates it with list items (<li>) based on the key-value pairs in the data array. If a value is
* an array
*/
public static function html (
array $data = [],
int $status = StatusCode::OK,
): string;


public static function csv(
array $data = [],
int $status = StatusCode::OK,

/**
* The function generates a CSV string from an array of data and sets the appropriate HTTP headers.
*
* @param array data The `csv` function you provided is used to generate a CSV string from the given
* data array. The data array should be structured in a way that each key-value pair represents a row
* in the CSV.
* @param int status The `status` parameter in the `csv` function is used to specify the HTTP status
* code that will be set in the response header. In the provided code snippet, the default value for
* the `status` parameter is `StatusCode::OK`. This means that if no status is provided when calling
* the
*
* @return string A string containing CSV formatted data based on the input array provided.
*/
public static function csv (
array $data = [],
int $status = StatusCode::OK,
): string;

public static function xml(
array $data = [],
int $status = StatusCode::OK,

/**
* The function generates an XML document based on the provided data array and HTTP status code.
*
* @param array data The `data` parameter in the `xml` function is an array that contains the data you
* want to convert to XML format. Each key-value pair in the array will be converted into an XML
* element where the key becomes the element tag and the value becomes the element value.
* @param int status The `status` parameter in the `xml` function represents the HTTP status code that
* will be set in the response headers. It is of type integer and is set to a default value of
* `StatusCode::OK`. This parameter allows you to specify the HTTP status code for the response,
* indicating the success
*
* @return string The `xml` function returns an XML string generated based on the provided data array.
* The XML string represents the data in a structured format with elements and values as specified in
* the input array.
*/
public static function xml (
array $data = [],
int $status = StatusCode::OK,
): string;


/**
* This PHP function sets the HTTP response status code and content type before returning an array.
*
* @param array data The `data` parameter is an array that contains the data you want to return from
* the function. It has a default value of an empty array `[]`, which means if no data is provided
* when calling the function, it will return an empty array.
* @param int status The `status` parameter in the `array` function is used to specify the HTTP status
* code that will be returned in the response. In this case, the default value for the `status`
* parameter is `StatusCode::UNSUPPORTED_MEDIA_TYPE`.
*
* @return array An array is being returned, which is typecasted from the input data array.
*/
public static function array(
array $data = [],
int $status = StatusCode::UNSUPPORTED_MEDIA_TYPE,
array $data = [],
int $status = StatusCode::UNSUPPORTED_MEDIA_TYPE,
): array;
}
}
Loading

0 comments on commit 42bcab3

Please sign in to comment.