Add more functionality.
This commit is contained in:
parent
75e1f7979c
commit
a61e2a847c
186
src/MDTwigExtension.php
Normal file
186
src/MDTwigExtension.php
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
<?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']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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>";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -68,6 +68,7 @@ class TwigRender {
|
|||||||
$twig = new Environment($loader, ['debug' => TRUE]);
|
$twig = new Environment($loader, ['debug' => TRUE]);
|
||||||
$twig->addExtension(new DebugExtension());
|
$twig->addExtension(new DebugExtension());
|
||||||
$twig->addExtension(new MFDESTwigExtension());
|
$twig->addExtension(new MFDESTwigExtension());
|
||||||
|
$twig->addExtension(new MDTwigExtension());
|
||||||
$html = $twig->render($this->template, [$this->tname => $this->data]);
|
$html = $twig->render($this->template, [$this->tname => $this->data]);
|
||||||
}
|
}
|
||||||
catch (LoaderError $e) {
|
catch (LoaderError $e) {
|
||||||
|
|||||||
195
src/Utils.php
Normal file
195
src/Utils.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user