21 Commits

Author SHA1 Message Date
mdesouza 75cb3f41e0 Merge pull request 'Release-v1.0.8' (#9) from release-v1.0.8 into master
Reviewed-on: #9
2026-04-20 09:08:48 -04:00
mdesouza b1435d39dc Add twig filter for brazilian date formatting. 2026-04-20 09:04:30 -04:00
mdesouza 16270fedf8 Merge pull request 'Release-v1.0.7' (#8) from release-v1.0.7 into master
Reviewed-on: #8
2025-10-07 14:48:42 -04:00
mdesouza 49bba32a34 Release-v1.0.7 2025-10-07 14:44:16 -04:00
mdesouza ce684e0ad1 Improve global variables access. 2025-10-07 14:41:51 -04:00
mdesouza 9b3bc83d0d Merge pull request 'Release-v1.0.6' (#7) from release-v1.0.6 into master
Reviewed-on: #7
2025-10-07 10:49:59 -04:00
mdesouza 5dbca21828 Release-v1.0.6 2025-10-07 10:46:35 -04:00
mdesouza 78163a800e Add globals to template. 2025-10-07 10:45:16 -04:00
mdesouza bf7d1ec84f Merge pull request 'Release-v1.0.5' (#6) from release-v1.0.5 into master
Reviewed-on: #6
2025-09-25 20:12:36 -04:00
mdesouza 49a060f9ab Release-v1.0.5 2025-09-25 20:11:17 -04:00
mdesouza a61e2a847c Add more functionality. 2025-09-25 20:09:58 -04:00
mdesouza 75e1f7979c Merge pull request 'Release-v1.0.4' (#5) from release-v1.0.4 into master
Reviewed-on: #5
2025-09-25 16:03:06 -04:00
mdesouza c8d150d752 Release-v1.0.4 2025-09-25 16:02:21 -04:00
mdesouza b7ee83e82a Code improvements. 2025-09-25 16:01:02 -04:00
mdesouza d923c788f9 Merge pull request 'Release-v1.0.3' (#4) from release-v1.0.3 into master
Reviewed-on: #4
2025-09-25 11:52:31 -04:00
mdesouza 000ed4c093 Release-v1.0.3 2025-09-25 11:52:03 -04:00
mdesouza 2dc9c8df1a Code improvements. 2025-09-25 11:47:43 -04:00
mdesouza 60f3508b36 Merge pull request 'Release-v1.0.2' (#3) from release-v1.0.2 into master
Reviewed-on: #3
2025-09-24 16:25:21 -04:00
mdesouza bd8c4f42eb Release-v1.0.2 2025-09-24 16:24:24 -04:00
mdesouza 7b807c015f fix paths. 2025-09-24 16:22:53 -04:00
mdesouza c6caa9e418 add some more utilities. 2025-09-24 16:07:50 -04:00
7 changed files with 522 additions and 17 deletions
+3 -1
View File
@@ -1,3 +1,5 @@
# mdesouza
Utils classes
Utils classes
Release-v1.0.7
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace Mdesouza\Utils;
/**
* @file
* Implementation of cookies.
*/
/**
* Class implementation for cookies.
*/
class Cookie {
/**
* Content of $_COOKIE array.
*
* @var array
*/
private $cookie = [];
/**
* Constructor.
*/
public function __construct() {
$this->cookie = $_COOKIE;
}
/**
* Checks if a cookie exists.
*/
public function cookieExists($name) {
return isset($this->cookie[$name]);
}
/**
* Get value of a cookie.
*/
public function getCookie($name) : string {
return $this->cookieExists($name) ? $this->cookie[$name] : "";
}
/**
* Set a cookie.
*/
public function setCookie(array $options) {
setcookie(
$options['name'],
$options['value'],
$options['expires'],
$options['path'],
$options['domain'],
$options['secure'],
$options['httponly']
);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Mdesouza\Utils\DB;
namespace Mdesouza\Utils;
/**
* @file
+203
View File
@@ -0,0 +1,203 @@
<?php
namespace Mdesouza\Utils;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* A class to extend Twig Filters.
*/
class MDTwigExtension extends AbstractExtension {
/**
* Regular expression for a brazilian phone number.
*
* @var string
*/
private $phoneRegex = '/^([\d]{2})([\d]{1})?([\d]{4})([\d]{4})$/';
/**
* Regular expression for a Brazilian zip code.
*
* @var string
*/
private $cepRegex = '/^([\d]{5})([\d]{3})?$/';
/**
* Regular expression for CPF.
*
* @var string
*/
private $cpfRegex = '/^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$/';
/**
* Regular expression for CNPJ.
*
* @var string
*/
private $cnpjRegex = '/^([\d]{2})([\d]{3})([\d]{3})([\d]{4})([\d]{2})$/';
/**
* Get the available filters for this extension.
*/
public function getFilters() {
return [
new TwigFilter('phone', [$this, 'formatPhone']),
new TwigFilter('cep', [$this, 'formatCEP']),
new TwigFilter('cpf', [$this, 'formatCPF']),
new TwigFilter('cnpj', [$this, 'formatCNPJ']),
new TwigFilter('real', [$this, 'formatReal']),
new TwigFilter('unicode', [$this, 'returnUnicode']),
new TwigFilter('dateshort', [$this, 'formatDateShort']),
];
}
/**
* Get the availeble functions for this extension.
*/
public function getFunctions() {
return [
new TwigFunction('version', [$this, 'getVersionNumber']),
];
}
/**
* Get version of the software from db.
*
* @param string $currentVersion
* The current version.
*
* @return string
* The latest version, according to the db.
*/
public function getVersionNumber($currentVersion) {
if ($_SESSION['devel'] == 1) {
$version = exec('git rev-parse --abbrev-ref HEAD');
}
else {
$version = $_SESSION['version'];
}
return $version;
}
/**
* Format a phone number - Brazilian phone number.
*
* @param string $number
* The number to be formated.
*
* @return string
* The formatted number.
*/
public function formatPhone(string $number): string {
return preg_replace($this->phoneRegex, '($1) $2 $3-$4', $number);
}
/**
* Format a brazilian zip code.
*
* @param string $cep
* The value to be formatted.
*
* @return string
* The formatted value.
*/
public function formatCep(string $cep): string {
return preg_replace($this->cepRegex, '$1-$2', $cep);
}
/**
* Format a CPF.
*
* @param string $cpf
* The value to be formatted.
*
* @return string
* The formatted value (will mask part of the CPF).
*/
public function formatCpf(string $cpf): string {
return preg_replace($this->cpfRegex, '$1.$2.$3-$4', $cpf);
}
/**
* Format CNPJ.
*
* @param string $cnpj
* The value to be formated.
*
* @return string
* The formatted value
*/
public function formatCnpj(string $cnpj): string {
return preg_replace($this->cnpjRegex, '$1.$2.$3/$4-$5', $cnpj);
}
/**
* Format number as Brazilian currency.
*
* @param string $number
* The number to be formatted.
*
* @return string
* The formatted number.
*/
public function formatReal(string $number): string {
$a = new \NumberFormatter("pt-BR", \NumberFormatter::DECIMAL);
$a->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2);
return $a->format((float) $number);
}
/**
* Return a converted unicode to HTML.
*
* @param string $value
* The number to be formatted.
*
* @return string
* The formatted value.
*/
public function returnUnicode(string $value): string {
$value = str_replace("u0", "\\u0", $value);
$unicode = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
$encodings = [
"UCS-2",
"UCS-2BE",
"UTF-16",
"UTF-16BE",
"ISO-8859-1",
"ISO-8859-4",
"ISO-8859-9",
"ISO-8859-10",
"ISO-8859-14",
"ISO-8859-15",
"byte2be",
"HTML-ENTITIES",
"8bit",
"Windows-1252",
];
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', $encodings);
}, str_replace(['\"', '\/', '&quot;', '\n'], ['"', '/', '\'', "\n"], $value));
return $unicode;
}
/**
* Format and translate date to portuguese.
* 2026-04-01 => 01 Apr. 2026
*/
public function formatDateShort(string $value) : string {
$df = new \IntlDateFormatter(
'pt_BR',
\IntlDateFormatter::FULL,
\IntlDateFormatter::NONE
);
$df->setPattern('d-LLL-yy');
$d = $df->format(new \DateTime($value));
return strtoupper(str_replace(".", "", $d));
}
}
+1 -3
View File
@@ -1,14 +1,12 @@
<?php
namespace Mdesouza\Utils\DB;
namespace Mdesouza\Utils;
/**
* @file
* A database class around the PDO object.
*/
include_once 'db.inc.php';
/**
* A mysql class around the PDO object.
*/
+62 -12
View File
@@ -21,10 +21,12 @@ class TwigRender {
*/
private $template;
/**
* Templates paths
*/
private $templates_path;
/**
* Templates paths.
*
* @var string
*/
private $templatesPath;
/**
* Template data.
@@ -33,13 +35,39 @@ class TwigRender {
*/
private $data;
/**
* Template object.
*
* @var \Twig\Environment
*/
private $twig;
/**
* Loader object.
*
* @var \Twig\Loader\FilesystemLoader
*/
private $loader;
/**
* Constructor.
*/
public function __construct(string $template, array $templates_path, array $data = []) {
public function __construct(string $template, string $templates_path, array $data = []) {
$this->template = $template;
$this->templates_path = $templates_path;
$this->templatesPath = $templates_path;
$this->data = $data;
$this->loader = new \Twig\Loader\FilesystemLoader([]);
$this->twig = new Environment($this->loader, ['debug' => TRUE]);
}
/**
* Add global variable(s) to template.
*
* @return string
*/
public function addGlobals($name, $value) : self {
$this->twig->addGlobal($name, $value);
return $this;
}
/**
@@ -47,14 +75,15 @@ class TwigRender {
*/
public function render() : string {
$data['host'] = $_SERVER['HTTP_HOST'];
$this->data['host'] = $_SERVER['HTTP_HOST'];
$html = "";
try {
$loader = new FilesystemLoader($this->templates_path);
$twig = new Environment($loader, ['debug' => TRUE]);
$twig->addExtension(new DebugExtension());
$twig->addExtension(new MFDESTwigExtension());
$html = $twig->render($this->template, ['data' => $this->data]);
$this->twig->addExtension(new DebugExtension());
$this->twig->addExtension(new MFDESTwigExtension());
$this->twig->addExtension(new MDTwigExtension());
$this->addPaths();
$html = $this->twig->render($this->template, ['data' => $this->data]);
}
catch (LoaderError $e) {
$html .= "<br/><pre>" . print_r($e->getMessage());
@@ -72,4 +101,25 @@ class TwigRender {
return $html;
}
/**
* Recursively add all directories under the templates base path.
*/
private function addPaths() {
// Add base path.
$this->loader->addPath($this->templatesPath);
// Create a recursive iterator to find all subdirectories.
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
// Add each sub-dir under the base path.
foreach ($iterator as $info) {
if ($info->isDir()) {
$this->loader->addPath($info->getPathname());
}
}
}
}
+195
View File
@@ -0,0 +1,195 @@
<?php
namespace Mdesouza\Utils;
/**
* Show variables and exit or continue.
*/
class Utils {
/**
* Print vars and exit.
*/
public static function printAndExit($data, $trace = TRUE) {
$bt = debug_backtrace();
echo "<pre>";
if (TRUE == $trace) {
echo "Trace: " . PHP_EOL;
foreach ($bt as $atrace) {
echo "$atrace[file]:$atrace[line]" . PHP_EOL;
}
echo PHP_EOL;
}
print_r($data);
echo "</pre>";
die("");
}
/**
* Print vars and continue.
*/
public static function printAndContinue($data, $trace = TRUE) {
$bt = debug_backtrace();
echo "<pre>";
if (TRUE == $trace) {
echo "Trace: " . PHP_EOL;
foreach ($bt as $atrace) {
echo "$atrace[file]:$atrace[line]" . PHP_EOL;
}
echo PHP_EOL;
}
print_r($data);
echo "</pre>";
}
/**
* Convert date to yyyy-mm-dd format.
*
* @param string $input
* Date to be converted.
*
* @return string
* Converted date.
*/
public static function dateToMysql($input): string {
if ($input == "") {
return "0000-00-00";
}
return date("Y-m-d", strtotime($input));
}
/**
* Validate an email address.
*
* @param string $email
* Email to be validted.
*
* @return bool
* TRUE if the email is valid; FALSE otherwise.
*/
public static function validateEmail($email) : bool {
if ("" != $email) {
$email = filter_var((string) $email, FILTER_VALIDATE_EMAIL);
if (FALSE == $email) {
return FALSE;
}
}
return TRUE;
}
/**
* Trim all items inside an array.
*
* @param array $array
* Array to be trimmed.
*
* @return array
* The trimmed array.
*/
public static function trimAll($array) : array {
$array_r = [];
foreach ($array as $key => $value) {
$value = str_replace("\"", "", $value);
$value = preg_replace('/(\s)+/', '$1', $value);
$value = trim($value);
if (is_numeric($key)) {
if (trim($value != "")) {
$array_r[] = $value;
}
}
else {
$array_r[$key] = $value;
}
}
return $array_r;
}
/**
* Remove code/html/javascript, double quotes and other invalid inputs.
*
* @param string $input
* String to be sanitized.
*
* @return string
* Sanitized string.
*/
public static function sanitizeInput($input): string {
$input = strip_tags($input);
$input = str_replace('"', '', $input);
return $input;
}
/**
* Remove bad characters from currency strings.
*
* @param string $input
* String to sanitize.
*
* @return string
* Sanitized string.
*/
public static function sanitizeCurrency($input): string {
$input = str_replace([".", '$'], "", $input);
$input = str_replace(",", ".", $input);
if ($input == "") {
$input = "0.00";
}
return $input;
}
/**
* Remove bad characters from number strings.
*
* @param string $input
* String to sanitize.
*
* @return string
* Sanitized string.
*/
public static function sanitizeNumber($input): string {
$input = str_replace(",", ".", $input);
if ($input == "") {
$input = "0";
}
return $input;
}
/**
* Multi dimension array search.
*
* @param string $needle
* The value being searched.
* @param array $haystack
* The array being searched.
* @param mixed $column
* The column being searched.
*
* @return mixed
* The index of the item, if found, or FALSE.
*/
public static function mdaSearch($needle, $haystack, $column): mixed {
$idx = '';
$found = FALSE;
foreach ($haystack as $key => $value) {
if (isset($value[$column]) && $value[$column] == $needle) {
$idx = $key;
$found = TRUE;
break;
}
}
if ($found) {
return $idx;
}
return FALSE;
}
}