-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f47e90f
Showing
28 changed files
with
954 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "hichemtab-tech/reqease-php", | ||
"version": "1.0.0", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"HichemtabTech\\ReqeasePhp\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "HichemTab", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": {} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use HichemtabTech\ReqeasePhp\ReqEase; | ||
use HichemtabTech\ReqeasePhp\Responses\Components\ButtonsBuilder; | ||
use HichemtabTech\ReqeasePhp\Responses\Message\MessageContent; | ||
use HichemtabTech\ReqeasePhp\Responses\Message\MessageResponseType; | ||
|
||
$response = ReqEase::createMessageResponse() | ||
->setContent( | ||
new MessageContent( | ||
'Hello BOSS!', | ||
'Hello BOSS! How are you doing today?' | ||
) | ||
) | ||
->setButtons( | ||
ButtonsBuilder::addCancelButton("Cancel") | ||
->addRedirectButton('https://google.com') | ||
) | ||
->setType(MessageResponseType::MODAL_BIG) | ||
->setHttpCode(200) | ||
->setColor('green') | ||
->build(); | ||
$response->send(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php /** @noinspection PhpUnused */ | ||
|
||
namespace HichemtabTech\ReqeasePhp; | ||
|
||
use HichemtabTech\ReqeasePhp\Responses\Custom\CustomResponseBuilder; | ||
use HichemtabTech\ReqeasePhp\Responses\FieldsErrors\FieldsErrorResponseBuilder; | ||
use HichemtabTech\ReqeasePhp\Responses\Message\MessageResponseBuilder; | ||
|
||
class ReqEase | ||
{ | ||
public static function createMessageResponse(): MessageResponseBuilder | ||
{ | ||
return new MessageResponseBuilder(); | ||
} | ||
|
||
public static function createFieldsErrorResponse(): FieldsErrorResponseBuilder | ||
{ | ||
return new FieldsErrorResponseBuilder(); | ||
} | ||
|
||
public static function createCustomResponse(): CustomResponseBuilder | ||
{ | ||
return new CustomResponseBuilder(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses; | ||
|
||
/** | ||
* @method static self setColor(string $color) | ||
* @method static self setHttpCode(int $httpCode) | ||
* @method static self setTimestamp(int $timestamp) | ||
* @method static self setExtraData(array $extraData) | ||
* @method static string getColor() | ||
* @method static int getHttpCode() | ||
* @method static int getTimestamp() | ||
* @method static string getVersion() | ||
* @method static string getEnvironment() | ||
* @method static array|null getExtraData() | ||
* | ||
* @method self setColor(string $color) | ||
* @method self setHttpCode(int $httpCode) | ||
* @method self setTimestamp(int $timestamp) | ||
* @method self setExtraData(array $extraData) | ||
* @method string getColor() | ||
* @method int getHttpCode() | ||
* @method int getTimestamp() | ||
* @method string getVersion() | ||
* @method string getEnvironment() | ||
* @method array|null getExtraData() | ||
*/ | ||
trait BaseResponse | ||
{ | ||
private static ?self $instance = null; | ||
protected readonly string $version; | ||
public readonly string $environment; | ||
protected ?int $timestamp = null; | ||
protected ?int $httpCode = null; | ||
protected ?string $color = null; | ||
protected ?array $extraData = null; | ||
|
||
public function __construct(?string $version, ?string $environment, ?string $timestamp, ?string $httpCode, ?string $color, ?array $extraData) | ||
{ | ||
$this->version = $version; | ||
$this->environment = $environment; | ||
$this->timestamp = $timestamp; | ||
$this->httpCode = $httpCode; | ||
$this->color = $color; | ||
$this->extraData = $extraData; | ||
} | ||
|
||
protected function jsonSerializeEssential(): array | ||
{ | ||
return array_merge([ | ||
"version" => $this->version, | ||
"environment" => $this->environment, | ||
"timestamp" => $this->timestamp, | ||
"httpCode" => $this->httpCode, | ||
"color" => $this->color, | ||
], $this->extraData??[]); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->jsonSerializeEssential(); | ||
} | ||
|
||
public function send(): void | ||
{ | ||
header('Content-Type: application/json'); | ||
echo json_encode($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses; | ||
|
||
use BadMethodCallException; | ||
|
||
/** | ||
* @method static self setColor(string $color) | ||
* @method static self setHttpCode(int $httpCode) | ||
* @method static self setTimestamp(int $timestamp) | ||
* @method static self setExtraData(array $extraData) | ||
* @method static string getColor() | ||
* @method static int getHttpCode() | ||
* @method static int getTimestamp() | ||
* @method static string getVersion() | ||
* @method static string getEnvironment() | ||
* @method static array|null getExtraData() | ||
* | ||
* | ||
* @method self setColor(string $color) | ||
* @method self setHttpCode(int $httpCode) | ||
* @method self setTimestamp(int $timestamp) | ||
* @method self setExtraData(array $extraData) | ||
* @method string getColor() | ||
* @method int getHttpCode() | ||
* @method int getTimestamp() | ||
* @method string getVersion() | ||
* @method string getEnvironment() | ||
* @method array|null getExtraData() | ||
*/ | ||
trait BaseResponseBuilder | ||
{ | ||
private static ?self $instance = null; | ||
protected readonly string $version; | ||
public readonly string $environment; | ||
protected ?int $timestamp = null; | ||
protected ?int $httpCode = null; | ||
protected ?string $color = null; | ||
protected ?array $extraData = null; | ||
|
||
public static function __callStatic($method, $args) { | ||
if (!static::$instance) { | ||
static::$instance = new static(); | ||
} | ||
return call_user_func_array([static::$instance, $method], $args); | ||
} | ||
|
||
public function __call(string $method, array $arguments) | ||
{ | ||
if (method_exists($this, $method)) { | ||
if (in_array($method, ["setField", "getField"])) { | ||
throw new BadMethodCallException("Method $method does not exist."); | ||
} | ||
return call_user_func_array([static::$instance, $method], $arguments); | ||
} else { | ||
if (str_starts_with($method, "set")) { | ||
return $this->setField(lcfirst(substr($method, 3)), $arguments[0]??null); | ||
} | ||
elseif (str_starts_with($method, "get")) { | ||
return $this->getField(strtolower(substr($method, 3))); | ||
} | ||
throw new BadMethodCallException("Method $method does not exist."); | ||
} | ||
} | ||
|
||
public function __construct() | ||
{ | ||
$composerJson = json_decode(file_get_contents(__DIR__ . '/../../composer.json'), true); | ||
$this->environment = "PHP"; | ||
$this->version = $composerJson['version']; | ||
$this->timestamp = time(); | ||
$this->httpCode = 200; | ||
} | ||
|
||
private function setField(string $field, mixed $value): self | ||
{ | ||
$this->$field = $value; | ||
return $this; | ||
} | ||
|
||
private function getField(string $field): mixed | ||
{ | ||
return $this->$field; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses\Components; | ||
|
||
enum ActionType | ||
{ | ||
const CLOSE = 'close'; | ||
const REDIRECT = "redirect"; | ||
const REFRESH = "refresh"; | ||
const CONFIRM = "confirm"; | ||
const CANCEL = "cancel"; | ||
const RETRY = "retry"; | ||
const CALL_FUNCTION = "call-function"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses\Components; | ||
|
||
use JsonSerializable; | ||
|
||
abstract class BaseActionButton implements JsonSerializable | ||
{ | ||
protected ActionType|string $actionType; | ||
protected ?string $text; | ||
protected ?string $color; | ||
|
||
/** | ||
* @param string|null $text | ||
* @param string|null $color | ||
*/ | ||
public function __construct(string $text = null, string $color = null) | ||
{ | ||
$this->text = $text; | ||
$this->color = $color; | ||
} | ||
|
||
abstract protected function getButtonData(): array; | ||
|
||
protected function getBaseButtonData(): array | ||
{ | ||
return [ | ||
'actionType' => $this->actionType, | ||
'text' => $this->text, | ||
'color' => $this->color, | ||
]; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return array_merge($this->getBaseButtonData(), $this->getButtonData()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Responses/Components/Buttons/CallFunctionActionButton.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses\Components\Buttons; | ||
|
||
use HichemtabTech\ReqeasePhp\Responses\Components\ActionType; | ||
use HichemtabTech\ReqeasePhp\Responses\Components\BaseActionButton; | ||
|
||
class CallFunctionActionButton extends BaseActionButton | ||
{ | ||
protected ActionType|string $actionType = ActionType::CALL_FUNCTION; | ||
private string $functionName; | ||
|
||
/** | ||
* @param string $functionName | ||
* @param string|null $text | ||
* @param string|null $color | ||
*/ | ||
public function __construct(string $functionName, ?string $text = null, ?string $color = null) | ||
{ | ||
parent::__construct($text, $color); | ||
$this->functionName = $functionName; | ||
} | ||
|
||
protected function getButtonData(): array | ||
{ | ||
return [ | ||
'functionName' => $this->functionName | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses\Components\Buttons; | ||
|
||
use HichemtabTech\ReqeasePhp\Responses\Components\ActionType; | ||
use HichemtabTech\ReqeasePhp\Responses\Components\BaseActionButton; | ||
|
||
class CancelActionButton extends BaseActionButton | ||
{ | ||
protected ActionType|string $actionType = ActionType::CANCEL; | ||
|
||
public function __construct(string $text = "Cancel", string $color = null) | ||
{ | ||
parent::__construct($text, $color); | ||
} | ||
|
||
protected function getButtonData(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace HichemtabTech\ReqeasePhp\Responses\Components\Buttons; | ||
|
||
use HichemtabTech\ReqeasePhp\Responses\Components\ActionType; | ||
use HichemtabTech\ReqeasePhp\Responses\Components\BaseActionButton; | ||
|
||
class CloseActionButton extends BaseActionButton | ||
{ | ||
protected ActionType|string $actionType = ActionType::CLOSE; | ||
|
||
public function __construct(string $text = "Close", string $color = null) | ||
{ | ||
parent::__construct($text, $color); | ||
} | ||
|
||
protected function getButtonData(): array | ||
{ | ||
return []; | ||
} | ||
} |
Oops, something went wrong.