Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
harryWonder committed Jun 30, 2020
0 parents commit 2795658
Show file tree
Hide file tree
Showing 90 changed files with 15,979 additions and 0 deletions.
88 changes: 88 additions & 0 deletions App/Controller/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace App;

class Controller {
protected function validation($payloads, $initialData)
{
$i = -1;
$response = [];
foreach($payloads as $data => $validator) {
$i++;
if ($validator == 'required') {
if ($data == null || $data = '' || !isset($data)) {
array_push($response, [
index => $i,
message => 'The %%PLACE_HOLDER%% field is required'
]);
}
}

if ($validator == 'string') {
if (preg_match('/[^A-Za-z]/', $data)) {
array_push($response, [
index => $i,
message => 'Sorry %%PLACE_HOLDER%% expects an Alphabet.'
]);
}
}

if ($validator == 'numeric') {
if (preg_match('/[^0-9_]/', $data)) {
array_push($response, [
index => $i,
message => 'Sorry %%PLACE_HOLDER%% expects a Number.'
]);
}
}

if ($validator == 'boolean') {
if (strtolower(gettype($data)) !== 'boolean') {
array_push($response, [
index => $i,
message => 'Sorry %%PLACE_HOLDER%% expects a Boolean.'
]);
}
}

if (stristr($validator, ':')) {
$operationName = explode(':', $validator)[0];
$operationChecks = (int) explode(':', $validator)[1];

if (strtolower($operationName) == 'min' && $operationChecks > strlen($data)) {
array_push($response, [
index => $i,
message => 'Sorry %%PLACE_HOLDER%% is supposed to be less than ' . strlen($data)
]);
}


if (strtolower($operationName) == 'max' && $operationChecks < strlen($data)) {
array_push($response, [
index => $i,
message => 'Sorry %%PLACE_HOLDER%% is supposed to be greather than ' . strlen($data)
]);
}


if (strtolower($operationName) == 'between') {
$operationChecksTwo = (int) explode(':', $validator)[2];
array_push($response, [
index => $i,
message => 'Sorry %%PLACE_HOLDER%% is supposed to be between ' . $operationChecks . ' and ' . $operationChecksTwo
]);
}

}

}

if (count($response) < 0) {
$validationErrors = new \stdClass();
$validationErrors->status = false;
$validationErrors->data = [];

return $validationErrors;
}
}
}
?>
53 changes: 53 additions & 0 deletions App/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace App;
use App\UserModel;

class UserController {
public function __construct()
{
//We can define our middlewares here....
}

public static function createNewUser($request, $response, $service)
{
$data = json_decode($request->body());
$sapi_type = php_sapi_name();
$Response = [];

if ($data->firstName == '' || $data->firstName == null) {
array_push($Response, [
'status' => 400,
'message' => 'Sorry, The First Name field is required.',
'data' => []
]);
}

if (preg_match('/[^A-Za-z]/', $data->firstName)) {
array_push($Response, [
'status' => 400,
'message' => 'Sorry, Only Alphabets are allowed.',
'data' => []
]);
}



if (substr($sapi_type, 0, 3) == 'cgi') {
header("Status: 400 Not Found");
$response->json($Response);
return;
} else {
header("HTTP/1.1 404 Not Found");
$response->json($Response);
return;
}
}

public static function index($request, $response, $service)
{
echo "<pre>";
print_r($request);
return 'Hello World!';
}
}
?>
6 changes: 6 additions & 0 deletions App/Middleware/JwtMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace App;
class JwtMiddleware {

}
?>
80 changes: 80 additions & 0 deletions App/Model/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace App;

class Model {
protected $dbHost = 'localhost';
protected $dbName = 'php_mini_rest_api';
protected $dbUser = 'root';
protected $dbPass = '';
protected $dbConn;
protected $stmt;

protected function _construct()
{
// Create a DSN...
$Dsn = "mysql:host=" . $this->dbHost . ";dbname=" . $this->dbName;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);

try {
$this->dbConn = new PDO($Dsn, $dbUser, $dbPass, $options);
} catch(Exception $e) {
$Response = array(
status => 500,
data => [],
message => $e->getMessage()
);
return $Response;
}
}

protected function query($query)
{
$this->stmt = $this->dbConn->prepare($query);
return true;
}

protected function bindParams($param, $value, $type = null)
{
if ($type == null) {
switch(true) {
case is_int:
$type = PDO::PARAM_INT;
break;
case is_bool:
$type = PDO::PARAM_BOOL;
break;
case is_null:
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
break;
}
}

$this->stmt->bindValue($param, $value, $type);
return;
}

protected function execute()
{
$this->stmt->execute();
return true;
}

protected function fetch()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

protected function fetchAll()
{
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
?>
28 changes: 28 additions & 0 deletions App/Model/UserModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace App;
use App\Model;

class UserModel extends Model {
public static function createUser($payload)
{
$Sql = "INSERT INTO `db_users` (firstName, lastName, email, password, token, secret, created_at, updated_at) VALUES (:firstName, :lastName, :email, :password, :token, :secret, :created_at, :updated_at)";
Parent::query($Sql);
// Bind Params...
Parent::bindParams('firstName', $payload['firstName']);
Parent::bindParams('lastName', $payload['lastName']);
Parent::bindParams('email', $payload['email']);
Parent::bindParams('password', $payload['password']);
Parent::bindParams('token', $payload['token']);
Parent::bindParams('secret', $payload['secret']);
Parent::bindParams('created_at', $payload['created_at']);
Parent::bindParams('updated_at', $payload['updated_at']);

$newUser = Parent::execute();
if ($newUser) {
return true;
}

return false;
}
}
?>
12 changes: 12 additions & 0 deletions App/routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace App;
use App\UserController;

$Klein = new \Klein\Klein();

$Klein->respond('POST', '/api/v1/user', [ new UserController(), 'createNewUser' ]);
$Klein->respond('GET', '/hello-world', [ new UserController(), 'index' ]);

$Klein->dispatch();

?>
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"require": {
"klein/klein": "^2.1",
"firebase/php-jwt": "^5.2"
},
"autoload": {
"psr-4": {
"App\\": "App/"
},
"classmap": [
"App/Controller",
"App/Middleware",
"App/Model"
]
}
}
Loading

0 comments on commit 2795658

Please sign in to comment.