Skip to content

Commit

Permalink
Merge pull request #59 from kivudesign/version_boss
Browse files Browse the repository at this point in the history
[ENH] ORM: review and rewrite all procedure.
  • Loading branch information
bim-g authored Jun 22, 2022
2 parents 97af74c + 50bc3be commit 9f3670d
Show file tree
Hide file tree
Showing 35 changed files with 664 additions and 186 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added assets/img/favicon.ico
Binary file not shown.
Binary file added assets/img/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/wepesi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions global/constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
const TIMEZONE = 'Africa/Kigali';

//define default domain
$server_name=$_SERVER['SERVER_NAME'];
$protocol=$_SERVER['REQUEST_SCHEME'];
$server_name=$_SERVER['SERVER_NAME']??"wepesi.com";
$protocol=$_SERVER['REQUEST_SCHEME']??"http";

define("DEFAULT_DOMAIN","$protocol://$server_name");
define("APP_DOMAIN",$server_name);
4 changes: 2 additions & 2 deletions lang/fr/language.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
$language=[
"Welcom"=>"Bienvenu",
"Welcom to Wepesi"=>"Bienvenu chez Wepesi",
"Welcome"=>"Bienvenu",
"Welcome to Wepesi"=>"Bienvenu chez Wepesi",
"you can find the simple example here"=>"Vous pouvez trouvez un exemple ici",
"A Simple Php MVC platform to develop quickly a php application"=>"Une plateforme Simple Php MVC pour développer rapidement une application web",
"Home"=>"Acceuill",
Expand Down
16 changes: 8 additions & 8 deletions src/Core/Bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

class Bundles{
static function insertCSS(string $file){
if(is_file("layout/style/" . $file . ".css")){
print '<link rel="stylesheet" type="text/css" href="'.WEB_ROOT.'layout/style/'.$file.'.css"/>'."\n";
if(is_file(ROOT."assets/css/" . $file . ".css")){
print '<link rel="stylesheet" type="text/css" href="'.WEB_ROOT.'assets/css/'.$file.'.css"/>'."\n";
}
}

static function insertJS(string $file){
if (is_file("layout/js/" . $file . ".js")) {
print '<script src="'.WEB_ROOT.'layout/js/'.$file.'.js"></script>';
if (is_file(ROOT."assets/js/" . $file . ".js")) {
print '<script src="'.WEB_ROOT.'assets/js/'.$file.'.js"></script>'."\n";
}
}
static function insertIMG($file){
if (is_file("layout/img/" . $file ) ) {
print '<img src="'.WEB_ROOT.'layout/img/'.$file.'" alt="">';
static function insertIMG($file_name){
if (is_file(ROOT."assets/img/" . $file_name ) ) {
print '<img src="'.WEB_ROOT.'assets/img/'.$file_name.'" alt="">';
}else{
print $file;
print $file_name;
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/Core/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
namespace Wepesi\Core;

class Escape{

static function encode(string $input){
static function encode(string $input)
{
$text = $input;
if ($input != strip_tags($input)) {
$text = htmlentities($input, ENT_QUOTES, 'UTF-8');
}
return $text;
}
static function decode(string $input){

static function decode(string $input)
{
return html_entity_decode($input, ENT_QUOTES, 'UTF-8');
}

static function randomString(int $length = 8)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
134 changes: 78 additions & 56 deletions src/Core/Orm/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,158 +4,180 @@
use PDOException;
use PDO;
use FFI\Exception;
use Wepesi\Core\Config;

class DB
class DB
{
private static $_instance = null;
private $_pdo,
$_query, $select_db_query,
$_error=false,
$_results = false,
$_lastid;
private $_action=null;
private $option=[
PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8",
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
] ;
private $_count;
private static ?DB $_instance;
private $queryResult;
private ?DBSelect $select_db_query;
private ?string $_error;
private ?array $_results;
private int $_lastid;
private PDO $pdoObject;
private array $option;
private int $_count;

private function __construct()
{
try {
$this->_pdo = new PDO("mysql:host=" . Config::get('mysql/host') . ";dbname=" . Config::get('mysql/db').";charset=utf8mb4", Config::get('mysql/username'), Config::get('mysql/password'),$this->option);
$this->initialisation();
$this->pdoObject = new PDO("mysql:host=" . Config::get('mysql/host') . ";dbname=" . Config::get('mysql/db').";charset=utf8mb4", Config::get('mysql/username'), Config::get('mysql/password'),$this->option);
} catch (PDOException $ex) {
die($ex->getMessage());
}
}
static function getInstance(){

/**
* Initialise all
*/
private function initialisation(){
$this->_results=[];
$this->option = [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$this->_count=0;
$this->_lastid=0;
self::$_instance=null;
}
static function getInstance(): ?DB
{
if(!isset(self::$_instance)){
self::$_instance=new DB();
}
return self::$_instance;
}
private function queryOperation(string $table_name,string $actions)

/**
* @param string $table_name
* @param string $actions
* @return QueryTransactions
* @throws \Exception
*/
private function queryOperation(string $table_name, string $actions)
{
if (strlen($table_name) < 1) {
throw new \Exception("table name should be a string");
}
$this->_action=$actions;
return new QueryTransactions($this->_pdo, $table_name, $actions);
return new QueryTransactions($this->pdoObject, $table_name, $actions);
}

/**
* @string :$table =>this is the name of the table where to get information
* this method allow to do a select field from a $table with all the conditions defined ;
* @throws \Exception
*/
function get(string $table_name)
function get(string $table_name): ?DBSelect
{
return $this->select_option($table_name);
}

/**
* @string :$table =>this is the name of the table where to get information
* this method allow to do a count the number of field on a $table with all the possible condition
* @throws \Exception
*/
function count(string $table_name)
function count(string $table_name): DBSelect
{
return $this->select_option($table_name, "count");
}

/**
* @string : $table=> this is the name of the table where to get information
* @string : @action=> this is the type of action tu do while want to do a request
* @throws \Exception
*/
private function select_option(string $table_name, string $action = null)
private function select_option(string $table_name, string $action = null): DBSelect
{
if (strlen($table_name) < 1) {
throw new \Exception("table name should be a string");
}
return $this->select_db_query = new DBSelect($this->_pdo, $table_name, $action);
return $this->queryResult = new DBSelect($this->pdoObject, $table_name, $action);
}

/**
* @param string $table : this is the name of the table where to get information
* @return QueryTransactions
* @throws \Exception
*
* this method will help create new row data
* @param string $table_name
* @return DBInsert
*/
function insert(string $table)
function insert(string $table_name): DBInsert
{
return $this->_query=$this->queryOperation($table, "insert");
return $this->queryResult = new DBInsert($this->pdoObject,$table_name);
}

/**
* @param string $table_name
* @return DBCreateMultiple
*/
function insertMultiple(string $table_name)
function insertMultiple(string $table_name): DBCreateMultiple
{
return $this->_query =new DBCreateMultiple($this->_pdo, $table_name);
return $this->queryResult =new DBCreateMultiple($this->pdoObject, $table_name);
}

/**
* @param string $table : this is the name of the table where to get information
* @return QueryTransactions
* @return DBDelete
* @throws \Exception
* this method will help delete row data information
*/
function delete(string $table)
function delete(string $table): DBDelete
{
return $this->queryOperation($table, "delete");
return $this->queryResult = new DBDelete($this->pdoObject,$table);
}
//

/**
* @param string $table : this is the name of the table where to get information
* @return QueryTransactions
* @return DBUpdate
* @throws \Exception
* this methode will help update row informations of a selected tables
*/
function update(string $table)
function update(string $table): DBUpdate
{
return $this->_query = $this->queryOperation($table, "update");
return $this->queryResult = new DBUpdate($this->pdoObject,$table);
}
//
function query($sql, array $params = []): DB
{
$q = new DBQeury($this->_pdo, $sql, $params);
$q = new DBQuery($this->pdoObject, $sql, $params);
$this->_results = $q->result();
$this->_count = $q->rowCount();
$this->_error = $q->getError();
$this->_lastid = $q->lastId();
return $this;
}
// return the last id after an insert operation query
function lastId()
function lastId(): ?int
{
return isset($this->_query) ? $this->_query->lastId() : $this->_lastid;
return isset($this->queryResult) ? $this->queryResult->lastId() : $this->_lastid;
}
/**
* return an error status when an error occur while doing an query
*/
function error()
{
/** check if it was a select operation and if an error occur then return it
*/
if(isset($this->select_db_query) ){
return $this->select_db_query->error();
}
/** check if if was an (insert, update, select) method was called, and return if an error occur
*/
else if(isset($this->_query)){
return $this->_query->error();
if(isset($this->queryResult)){
return $this->queryResult->error();
}else{
/** if it was written query, it will return the error, if it exist.
// otherwise, it will return false
/**
* if it was written query, it will return the error, if it exist.
* otherwise, it will return false
*/
return $this->_error;
}
}
function result()

/**
* @return array|null
*/
function result(): ?array
{
return $this->_results;
}

/**
* @return int
*/
function rowCount(){
return isset($this->_query) ? $this->_query->count() : $this->_count;
return isset($this->queryResult) ? $this->queryResult->count() : $this->_count;
}
}
43 changes: 33 additions & 10 deletions src/Core/Orm/DBCreateMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ function field(array $fields = [])
{
try{
if (count($fields) ) {
$myKeysFields=[];
if(isset($fields[0]) && is_array($fields[0])){
foreach ($fields[0] as $field=>$value){
array_push($myKeysFields, trim($field));
}
$prepared_field_keys ="(". implode(',', $myKeysFields).")" ;
$extracted_field_key= array_keys($fields[0]);

$prepared_field_keys ="(". implode(',', $extracted_field_key).")" ;
$expected_prepared_params="";
$y=0;
$value_prepared_params=[];
foreach ($fields as $elements){
foreach ($fields as $data_elements){
$prepared_params="";
$x=0;
foreach ($elements as $element=>$field){
array_push($value_prepared_params,$field);
foreach ($data_elements as $element=>$field){
$value_prepared_params[]=$field;
$prepared_params .= "? ";
if ($x < count($elements)-1) {
if ($x < count($data_elements)-1) {
$prepared_params .= ', ';
}
$x++;
Expand Down Expand Up @@ -67,12 +65,37 @@ function field(array $fields = [])
*/
private function query($sql, array $params = [])
{
$q = new DBQeury($this->_pdo, $sql, $params);
$q = new DBQuery($this->_pdo, $sql, $params);
$this->_error = $q->getError();
$this->_lastid = $q->lastId();
return $this;
}

private function checkFieldKeyExist(array $source_data){
$fieldKey=[];
$source_data=(isset($source_data[0]) && is_array($source_data[0]))?$source_data:[$source_data];
if(isset($source_data[0]) && is_array($source_data[0])){
$fieldKey=array_keys($source_data[0]);
}
$extracted_key=array_keys($source_data);
if(count($extracted_key)!=count($fieldKey)) return false;
foreach ($fieldKey as $key){
if(!isset($source_data[$key])) return false;
}
return $this->formatFieldData($fieldKey,$source_data);
}
private function formatFieldData(array $fieldKey,array $source_data): array
{
$new_formated_row=[];
foreach ($source_data as $rows){
$new_formated_data=[];
foreach ($fieldKey as $key){
$new_formated_data[$key]=$rows[$key];
}
$new_formated_row[]=$new_formated_data;
}
return $new_formated_row;
}
/**
* @return bool
* use this module to create new record
Expand Down
Loading

0 comments on commit 9f3670d

Please sign in to comment.