Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75cb3f41e0 | |||
| b1435d39dc | |||
| 16270fedf8 | |||
| 49bba32a34 | |||
| ce684e0ad1 | |||
| 9b3bc83d0d | |||
| 5dbca21828 | |||
| 78163a800e | |||
| bf7d1ec84f | |||
| 49a060f9ab | |||
| a61e2a847c | |||
| 75e1f7979c | |||
| c8d150d752 | |||
| b7ee83e82a | |||
| d923c788f9 |
@@ -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(['\"', '\/', '"', '\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,48 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mdesouza\Utils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Show variables and exit or continue.
|
|
||||||
*/
|
|
||||||
class ShowVars {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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>";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+34
-12
@@ -35,6 +35,20 @@ class TwigRender {
|
|||||||
*/
|
*/
|
||||||
private $data;
|
private $data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template object.
|
||||||
|
*
|
||||||
|
* @var \Twig\Environment
|
||||||
|
*/
|
||||||
|
private $twig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loader object.
|
||||||
|
*
|
||||||
|
* @var \Twig\Loader\FilesystemLoader
|
||||||
|
*/
|
||||||
|
private $loader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
@@ -42,6 +56,18 @@ class TwigRender {
|
|||||||
$this->template = $template;
|
$this->template = $template;
|
||||||
$this->templatesPath = $templates_path;
|
$this->templatesPath = $templates_path;
|
||||||
$this->data = $data;
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,12 +79,11 @@ class TwigRender {
|
|||||||
$html = "";
|
$html = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$loader = new FilesystemLoader([]);
|
$this->twig->addExtension(new DebugExtension());
|
||||||
$loader = $this->addPaths($loader);
|
$this->twig->addExtension(new MFDESTwigExtension());
|
||||||
$twig = new Environment($loader, ['debug' => TRUE]);
|
$this->twig->addExtension(new MDTwigExtension());
|
||||||
$twig->addExtension(new DebugExtension());
|
$this->addPaths();
|
||||||
$twig->addExtension(new MFDESTwigExtension());
|
$html = $this->twig->render($this->template, ['data' => $this->data]);
|
||||||
$html = $twig->render($this->template, ['data' => $this->data]);
|
|
||||||
}
|
}
|
||||||
catch (LoaderError $e) {
|
catch (LoaderError $e) {
|
||||||
$html .= "<br/><pre>" . print_r($e->getMessage());
|
$html .= "<br/><pre>" . print_r($e->getMessage());
|
||||||
@@ -79,11 +104,10 @@ class TwigRender {
|
|||||||
/**
|
/**
|
||||||
* Recursively add all directories under the templates base path.
|
* Recursively add all directories under the templates base path.
|
||||||
*/
|
*/
|
||||||
private function addPaths(FilesystemLoader $loader) : FilesystemLoader {
|
private function addPaths() {
|
||||||
|
|
||||||
// Add base path.
|
// Add base path.
|
||||||
$loader->addPath($this->templatesPath);
|
$this->loader->addPath($this->templatesPath);
|
||||||
|
|
||||||
// Create a recursive iterator to find all subdirectories.
|
// Create a recursive iterator to find all subdirectories.
|
||||||
$iterator = new \RecursiveIteratorIterator(
|
$iterator = new \RecursiveIteratorIterator(
|
||||||
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
|
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
|
||||||
@@ -93,11 +117,9 @@ class TwigRender {
|
|||||||
// Add each sub-dir under the base path.
|
// Add each sub-dir under the base path.
|
||||||
foreach ($iterator as $info) {
|
foreach ($iterator as $info) {
|
||||||
if ($info->isDir()) {
|
if ($info->isDir()) {
|
||||||
$loader->addPath($info->getPathname());
|
$this->loader->addPath($info->getPathname());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $loader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+195
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user