main
Robert 1 month ago
parent bcaf3310b2
commit 05aefcfdfc
  1. 211
      src/Framework/Arrays/CountriesCommon.php
  2. 5
      src/Framework/Assets.php
  3. 46
      src/Framework/Common.php
  4. 14
      src/Framework/Database/Model.php
  5. 51
      src/Framework/Trait/Database/RunSql.php
  6. 12
      src/Framework/Trait/Database/Validation.php

@ -0,0 +1,211 @@
<?php
declare(strict_types=1);
namespace IOcornerstone\Framework\Arrays;
class CountriesCommon {
/**
* Fetch countries
* @return array of countries
*/
public static function countriesArray(): array {
return [
'AFG' => 'Afghanistan',
'ALB' => 'Albania',
'ALG' => 'Algeria',
'AND' => 'Andorra',
'ANG' => 'Angola',
'ANT' => 'Antigua and Barbuda',
'ARG' => 'Argentina',
'ARM' => 'Armenia',
'AUS' => 'Australia',
'AUT' => 'Austria',
'AZE' => 'Azerbaijan',
'BAH' => 'Bahamas',
'BAH' => 'Bahrain',
'BAN' => 'Bangladesh',
'BAR' => 'Barbados',
'BEL' => 'Belgium',
'BEL' => 'Belize',
'BEN' => 'Benin',
'BHU' => 'Bhutan',
'BOL' => 'Bolivia',
'BIH' => 'Bosnia and Herzegovina',
'BOT' => 'Botswana',
'BRA' => 'Brazil',
'BRU' => 'Brunei',
'BUL' => 'Bulgaria',
'BUR' => 'Burkina Faso',
'BUR' => 'Burundi',
'CAV' => 'Cabo Verde',
'CAM' => 'Cambodia',
'CMR' => 'Cameroon',
'CAN' => 'Canada',
'CAF' => 'Central African Republic',
'CHA' => 'Chad',
'CHI' => 'Chile',
'CHN' => 'China',
'COL' => 'Colombia',
'COM' => 'Comoros',
'CGO' => 'Congo, Republic of the',
'COD' => 'Congo, Democratic Republic of the',
'CRI' => 'Costa Rica',
'HRV' => 'Croatia',
'CUB' => 'Cuba',
'CYP' => 'Cyprus',
'CZE' => 'Czech Republic',
'DEN' => 'Denmark',
'DJB' => 'Djibouti',
'DMA' => 'Dominica',
'DOM' => 'Dominican Republic',
'ECU' => 'Ecuador',
'EGY' => 'Egypt',
'SLV' => 'El Salvador',
'GEQ' => 'Equatorial Guinea',
'ERI' => 'Eritrea',
'EST' => 'Estonia',
'SWZ' => 'Eswatini',
'ETH' => 'Ethiopia',
'FIJ' => 'Fiji',
'FIN' => 'Finland',
'FRA' => 'France',
'GAB' => 'Gabon',
'GMB' => 'Gambia',
'GEO' => 'Georgia',
'DEU' => 'Germany',
'GHA' => 'Ghana',
'GRC' => 'Greece',
'GRD' => 'Grenada',
'GTM' => 'Guatemala',
'GIN' => 'Guinea',
'GNB' => 'Guinea-Bissau',
'GUY' => 'Guyana',
'HAI' => 'Haiti',
'HND' => 'Honduras',
'HUN' => 'Hungary',
'ISL' => 'Iceland',
'IND' => 'India',
'IDN' => 'Indonesia',
'IRN' => 'Iran',
'IRQ' => 'Iraq',
'IRL' => 'Ireland',
'ISR' => 'Israel',
'ITA' => 'Italy',
'JAM' => 'Jamaica',
'JPN' => 'Japan',
'JOR' => 'Jordan',
'KAZ' => 'Kazakhstan',
'KEN' => 'Kenya',
'KIR' => 'Kiribati',
'KOR' => 'Korea, North',
'KRS' => 'Korea, South',
'KOS' => 'Kosovo',
'KWT' => 'Kuwait',
'KGZ' => 'Kyrgyzstan',
'LAO' => 'Laos',
'LVA' => 'Latvia',
'LBN' => 'Lebanon',
'LSO' => 'Lesotho',
'LBR' => 'Liberia',
'LBY' => 'Libya',
'LIE' => 'Liechtenstein',
'LTU' => 'Lithuania',
'LUX' => 'Luxembourg',
'MDG' => 'Madagascar',
'MWI' => 'Malawi',
'MYS' => 'Malaysia',
'MDV' => 'Maldives',
'MLI' => 'Mali',
'MLT' => 'Malta',
'MHL' => 'Marshall Islands',
'MRT' => 'Mauritania',
'MUS' => 'Mauritius',
'MEX' => 'Mexico',
'FSM' => 'Micronesia',
'MDA' => 'Moldova',
'MCO' => 'Monaco',
'MNG' => 'Mongolia',
'MNE' => 'Montenegro',
'MAR' => 'Morocco',
'MOZ' => 'Mozambique',
'MYA' => 'Myanmar',
'NAM' => 'Namibia',
'NRU' => 'Nauru',
'NPL' => 'Nepal',
'NLD' => 'Netherlands',
'NZL' => 'New Zealand',
'NIC' => 'Nicaragua',
'NER' => 'Niger',
'NGA' => 'Nigeria',
'MKD' => 'North Macedonia',
'NOR' => 'Norway',
'OMN' => 'Oman',
'PAK' => 'Pakistan',
'PLW' => 'Palau',
'PAN' => 'Panama',
'PNG' => 'Papua New Guinea',
'PRY' => 'Paraguay',
'PER' => 'Peru',
'PHL' => 'Philippines',
'POL' => 'Poland',
'PRT' => 'Portugal',
'QAT' => 'Qatar',
'ROU' => 'Romania',
'RUS' => 'Russia',
'RWA' => 'Rwanda',
'KNA' => 'Saint Kitts and Nevis',
'LCA' => 'Saint Lucia',
'VCT' => 'Saint Vincent and the Grenadines',
'WSM' => 'Samoa',
'SMR' => 'San Marino',
'STP' => 'Sao Tome and Principe',
'SAU' => 'Saudi Arabia',
'SEN' => 'Senegal',
'SRB' => 'Serbia',
'SYC' => 'Seychelles',
'SLE' => 'Sierra Leone',
'SGP' => 'Singapore',
'SVK' => 'Slovakia',
'SVN' => 'Slovenia',
'SLB' => 'Solomon Islands',
'SOM' => 'Somalia',
'ZAF' => 'South Africa',
'SSD' => 'South Sudan',
'ESP' => 'Spain',
'LKA' => 'Sri Lanka',
'SDN' => 'Sudan',
'SUR' => 'Suriname',
'SWE' => 'Sweden',
'CHE' => 'Switzerland',
'SYR' => 'Syria',
'TAI' => 'Taiwan',
'TJK' => 'Tajikistan',
'TZA' => 'Tanzania',
'THA' => 'Thailand',
'TGO' => 'Togo',
'TON' => 'Tonga',
'TTO' => 'Trinidad and Tobago',
'TUN' => 'Tunisia',
'TUR' => 'Turkey',
'TKM' => 'Turkmenistan',
'TUV' => 'Tuvalu',
'UGA' => 'Uganda',
'UKR' => 'Ukraine',
'ARE' => 'United Arab Emirates',
'GBR' => 'United Kingdom',
'USA' => 'United States',
'URY' => 'Uruguay',
'UZB' => 'Uzbekistan',
'VAN' => 'Vanuatu',
'VAT' => 'Vatican City',
'VEN' => 'Venezuela',
'VNM' => 'Vietnam',
'YEM' => 'Yemen',
'ZMB' => 'Zambia',
'ZWE' => 'Zimbabwe',
];
}
}

@ -201,12 +201,13 @@ class Assets
} else {
$more .= " media=\"all\"";
}
if (strtolower($scope) !== "cdn") {
$ver = self::projectPaths($file, $scope);
$wrap_file = self::wrapAsset($file, $scope);
if ($ver === false || $wrap_file === false) {
return "<!-- CSS: {$file} not Found -->";
}
}
return "<link rel=\"stylesheet\" href=\"{$wrap_file}{$ver}\" type=\"text/css\"{$more}/>\r\n";
}
@ -224,11 +225,13 @@ class Assets
}
}
if (strtolower($scope) !== "cdn") {
$ver = self::projectPaths($file, $scope);
$wrap_file = self::wrapAsset($file, $scope);
if ($ver === false || $wrap_file === false) {
return "<!-- Script: {$file} not Found -->";
}
}
return "<script src=\"{$wrap_file}{$ver}\"{$more}></script>\r\n";
//return "<script type=\"text/javascript\">js_loader(\"{$file}\");</script>";
}

@ -9,10 +9,51 @@ use IOcornerstone\Framework\{
Logger,
String\StringFacade as F,
Enum\ExitOnDump as endDump,
TimeZones,
};
final class Common
{
/**
* Walks through all variables given to it, checking for false,
* if false found it Failed, so save Failed....
* @param type $args
* @return array
*/
public static function hasSaved(...$args): array
{
if (self::isArrayWithData($args)) {
if (self::hasKeys($args)) {
foreach($args as $key => $value){
if ($value === false) {
return self::saveFailed();
}
}
} else {
foreach($args as $item){
if ($item === false) {
return self::saveFailed();
}
}
}
}
if (is_bool($args) && $args === false) {
return self::saveFailed();
}
return self::saved();
}
public static function saved(bool $worked = true): array {
$time = TimeZones::convertTimeZone(array('format' => 'g:i a'));
return ['time' => $time, 'saved' => $worked];
}
/**
* JSON API inform AJAX that save failed
*/
public static function saveFailed($msg = 'Save Failed!'): array {
return ['saved' => false, 'msg' => $msg];
}
/**
* Clear out from memory given variable by Reference!
@ -103,11 +144,12 @@ final class Common
return false; // Empty Data..., return false
}
public static function getDigitsAndLettersOnly(false|null|string $input): ?string {
public static function getAlphaNumbric(false|null|string $input, $allow_dashes = false): ?string {
if ($input === false || $input === null) {
return null;
}
return preg_replace('/[^a-zA-Z0-9]/', '', $input);
$allowed = ($allow_dashes) ? "_-" : "";
return preg_replace('/[^a-zA-Z0-9'.$allowed.']/', '', $input);
}
public static function safeFlipArray(?array $data): array

@ -11,14 +11,16 @@ declare(strict_types=1);
namespace IOcornerstone\Framework\Database;
use IOcornerstone\Framework\{
Trait\Database\RunSql,
Trait\Database\Validation,
TimeZones,
Common,
};
class Model {
use \IOCornerstone\Framework\Trait\Database\RunSql;
use \IOCornerstone\Framework\Trait\Database\Validation;
use RunSql;
use Validation;
const SUCCESSFUL_SAVE = 1;
const DUPLICATE_FOUND = 2;
@ -29,20 +31,19 @@ class Model {
private $members = [];
private $validationMembers = [];
private $missing = [];
private $errorMessage;
protected $errorMessage;
private $primaryKey = 'id';
private $dbSkiped = [];
public function saved(bool $worked = true): array {
$time = TimeZones::convertTimeZone(array('format' => 'g:i a'));
return ['time' => $time, 'saved' => $worked];
return Common::saved($worked);
}
/**
* JSON API inform AJAX that save failed
*/
public function saveFailed($msg = 'Save Failed!'): array {
return ['saved' => false, 'msg' => $msg];
return Common::saveFailed($msg);
}
public function dumpTableFields(string $table) {
@ -262,7 +263,6 @@ class Model {
/**
* Save
*
* @param object $lcoal_model Should be $this from the Model
*
* Insert if primary key not set
* Update if primary key set

@ -7,31 +7,43 @@ declare(strict_types = 1);
* @copyright (c) 2026, Robert Strutts
* @license MIT
*/
namespace IOCornerstone\Framework\Trait\Database;
use IOcornerstone\Framework\Common;
class RunSql
trait RunSql
{
public function run($sql, $bind = ""): int
{
public function run($sql, $bind=""): int {
$pdostmt = $this->pdo->prepare(trim($sql));
if (Common::getCount($bind) > 0) {
$exec = $pdostmt->execute($bind);
} else {
$exec = $pdostmt->execute();
}
if ($exec === false) {
return 0;
}
return $pdostmt->rowCount();
}
public function runSelect($sql) {
public function runSelect($sql): array
{
$pdostmt = $this->pdo->prepare(trim($sql));
$exec = $pdostmt->execute();
if ($exec === false) {
return [];
}
return $pdostmt->fetchAll(\PDO::FETCH_ASSOC);
}
private function describeTable(string $table) {
private function describeTable(string $table)
{
$safer_table = Common::getAlphaNumbric($table, allow_dashes: true);
$driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
$tickTable = (strrpos($table, "`") === false) ? "`{$table}`" : $table;
$tickTable = (strrpos($safer_table, "`") === false) ? "`{$safer_table}`" : $safer_table;
if ($driver == 'sqlite') {
$sql = "PRAGMA table_info({$tickTable});";
$key = "name";
@ -39,7 +51,7 @@ class RunSql
$sql = "DESCRIBE {$tickTable};";
$key = "Field";
} elseif ($driver == 'pgsql') {
$sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '{$table}';";
$sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '{$safer_table}';";
$key = "column_name";
} else {
return false;
@ -47,33 +59,46 @@ class RunSql
return ['sql' => $sql, 'key' => $key];
}
public function getFields(string $table) {
/**
* Uses the private helper method describeTable to get all fields in a Table
* @param string $table
* @return array
*/
public function getFields(string $table = ""): array
{
if (empty($table)) {
$table = $this->table ?? false;
}
if ($table === false) {
return [];
}
$describe = $this->describeTable($table);
if (false !== ($list = $this->runSelect($describe['sql']))) {
$fields = array();
$fields = [];
if (count($list) == 0) {
return array();
return [];
}
foreach ($list as $record) {
$fields[] = $record[$describe['key']];
}
return $fields;
}
return array();
return [];
}
private function filter(string $table, array $info): array {
protected function filter(string $table, array $info): array
{
$describe = $this->describeTable($table);
if ($describe === false) {
return array_keys($info);
}
if (false !== ($list = $this->runSelect($describe['sql']))) {
$fields = array();
$fields = [];
foreach ($list as $record) {
$fields[] = $record[$describe['key']];
}
return array_values(array_intersect($fields, array_keys($info)));
}
return array();
return [];
}
}

@ -12,7 +12,8 @@ namespace IOCornerstone\Framework\Trait\Database;
use IOcornerstone\Framework\{
Configure,
Common,
Registry
Registry,
Logger,
};
trait Validation
@ -198,11 +199,10 @@ trait Validation
* @param string $msg error message
*/
private function do_verr(string $msg): void {
$this->error_message = $msg;
$exists = Registry::get('di')->exists('log');
if ($exists && Configure::get('database', 'log_validation_errors') === true) {
$log = Registry::get('di')->getService('log', ['validation_errors']);
$log->write($msg);
if (Configure::get('database', 'log_validation_errors') === true) {
$log = new Logger();
$log->alert($msg);
}
$this->errorMessage = $msg;
}
}

Loading…
Cancel
Save