Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HichemTab-tech committed Sep 9, 2023
0 parents commit f47e90f
Show file tree
Hide file tree
Showing 28 changed files with 954 additions and 0 deletions.
18 changes: 18 additions & 0 deletions composer.json
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": {}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions index.php
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();
25 changes: 25 additions & 0 deletions src/ReqEase.php
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();
}
}
69 changes: 69 additions & 0 deletions src/Responses/BaseResponse.php
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);
}
}
85 changes: 85 additions & 0 deletions src/Responses/BaseResponseBuilder.php
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;
}
}
14 changes: 14 additions & 0 deletions src/Responses/Components/ActionType.php
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";
}
38 changes: 38 additions & 0 deletions src/Responses/Components/BaseActionButton.php
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 src/Responses/Components/Buttons/CallFunctionActionButton.php
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
];
}
}
21 changes: 21 additions & 0 deletions src/Responses/Components/Buttons/CancelActionButton.php
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 [];
}
}
21 changes: 21 additions & 0 deletions src/Responses/Components/Buttons/CloseActionButton.php
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 [];
}
}
Loading

0 comments on commit f47e90f

Please sign in to comment.