Skip to content

Commit

Permalink
Mais classes utilitárias adicionadas
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Elias committed Feb 7, 2019
1 parent 3ee1659 commit 91a9577
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 27 deletions.
119 changes: 119 additions & 0 deletions src/IfThenPay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Pekit;

class IfThenPay
{
static function format_number ($number)
{
$verifySepDecimal = number_format (99, 2);

$valorTmp = $number;

$sepDecimal = substr ($verifySepDecimal, 2, 1);

$hasSepDecimal = true;

$i = (strlen ($valorTmp) - 1);

for ($i; $i != 0; $i -= 1) {
if (substr ($valorTmp, $i, 1) == "." || substr ($valorTmp, $i, 1) == ",") {
$hasSepDecimal = true;
$valorTmp = trim (substr ($valorTmp, 0, $i)) . "@" . trim (substr ($valorTmp, 1 + $i));
break;
}
}

if ($hasSepDecimal != true) {
$valorTmp = number_format ($valorTmp, 2);

$i = (strlen ($valorTmp) - 1);

for ($i; $i != 1; $i--) {
if (substr ($valorTmp, $i, 1) == "." || substr ($valorTmp, $i, 1) == ",") {
$hasSepDecimal = true;
$valorTmp = trim (substr ($valorTmp, 0, $i)) . "@" . trim (substr ($valorTmp, 1 + $i));
break;
}
}
}

for ($i = 1; $i != (strlen ($valorTmp) - 1); $i++) {
if (substr ($valorTmp, $i, 1) == "." || substr ($valorTmp, $i, 1) == "," || substr ($valorTmp, $i, 1) == " ") {
$valorTmp = trim (substr ($valorTmp, 0, $i)) . trim (substr ($valorTmp, 1 + $i));
break;
}
}

if (strlen (strstr ($valorTmp, '@')) > 0) {
$valorTmp = trim (substr ($valorTmp, 0, strpos ($valorTmp, '@'))) . trim ($sepDecimal) .
trim (substr ($valorTmp, strpos ($valorTmp, '@') + 1));
}

return $valorTmp;
}

static function GenerateMbRef ($ent_id, $subent_id, $order_id, $order_value)
{
$chk_val = 0;
$msg = "";
$order_id = "0000" . $order_id;

if (strlen ($ent_id) < 5)
$msg .= "Lamentamos mas tem de indicar uma entidade válida";
else if (strlen ($ent_id) > 5)
$msg .= "Lamentamos mas tem de indicar uma entidade válida";

if (strlen ($subent_id) == 0)
$msg .= "Lamentamos mas tem de indicar uma subentidade válida";

$order_value = sprintf ("%01.2f", $order_value);

$order_value = self::format_number ($order_value);

if ($order_value < 1)
$msg .= "Lamentamos mas é impossível gerar uma referência MB para valores inferiores a 1 Euro";

if ($order_value >= 1000000)
$msg .= "Pagamento fraccionado por exceder o valor limite para pagamentos no sistema Multibanco";

if (strlen ($subent_id) == 1) {
//Apenas sao considerados os 6 caracteres mais a direita do order_id
$order_id = substr ($order_id, (strlen ($order_id) - 6), strlen ($order_id));
$chk_str = sprintf ('%05u%01u%06u%08u', $ent_id, $subent_id, $order_id, round ($order_value * 100));
}
else if (strlen ($subent_id) == 2) {
//Apenas sao considerados os 5 caracteres mais a direita do order_id
$order_id = substr ($order_id, (strlen ($order_id) - 5), strlen ($order_id));
$chk_str = sprintf ('%05u%02u%05u%08u', $ent_id, $subent_id, $order_id, round ($order_value * 100));
}
else {
//Apenas sao considerados os 4 caracteres mais a direita do order_id
$order_id = substr ($order_id, (strlen ($order_id) - 4), strlen ($order_id));
$chk_str = sprintf ('%05u%03u%04u%08u', $ent_id, $subent_id, $order_id, round ($order_value * 100));
}

//cálculo dos check digits

$chk_array = [3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51];

for ($i = 0; $i < 20; $i++) {
$chk_int = substr ($chk_str, 19 - $i, 1);
$chk_val += ($chk_int % 10) * $chk_array[$i];
}

$chk_val %= 97;

$chk_digits = sprintf ('%02u', 98 - $chk_val);

if ($msg)
return ['message' => $msg];

$data_mb = [
'entidade' => $ent_id,
'referencia' => substr ($chk_str, 5, 3) . substr ($chk_str, 8, 3) . substr ($chk_str, 11, 1) . $chk_digits,
'valor' => number_format ($order_value, 2, ',', ' ')
];
return $data_mb;
}
}
118 changes: 118 additions & 0 deletions src/UUID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Pekit;

class UUID
{
public static function v3($namespace, $name)
{
if (!self::is_valid($namespace)) return false;

// Get hexadecimal components of namespace
$nhex = str_replace(array('-', '{', '}'), '', $namespace);

// Binary Value
$nstr = '';

// Convert Namespace UUID to bits
for ($i = 0; $i < strlen($nhex); $i += 2)
{
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
}

// Calculate hash value
$hash = md5($nstr . $name);

return sprintf('%08s-%04s-%04x-%04x-%12s',

// 32 bits for "time_low"
substr($hash, 0, 8),

// 16 bits for "time_mid"
substr($hash, 8, 4),

// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 3
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,

// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

// 48 bits for "node"
substr($hash, 20, 12)
);
}

public static function v4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),

// 16 bits for "time_mid"
mt_rand(0, 0xffff),

// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,

// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,

// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}

public static function v5($namespace, $name)
{
if (!self::is_valid($namespace)) return false;

// Get hexadecimal components of namespace
$nhex = str_replace(array('-', '{', '}'), '', $namespace);

// Binary Value
$nstr = '';

// Convert Namespace UUID to bits
for ($i = 0; $i < strlen($nhex); $i += 2)
{
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
}

// Calculate hash value
$hash = sha1($nstr . $name);

return sprintf('%08s-%04s-%04x-%04x-%12s',

// 32 bits for "time_low"
substr($hash, 0, 8),

// 16 bits for "time_mid"
substr($hash, 8, 4),

// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 5
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,

// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

// 48 bits for "node"
substr($hash, 20, 12)
);
}

public static function is_valid($uuid)
{
return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
}
}
40 changes: 13 additions & 27 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,6 @@

class Utilities
{
/**
* Gerar UUID
*
* @param $data
* @return string
*/
public static function uuid($data = "")
{
if (!$data)
$data = openssl_random_pseudo_bytes(16);

assert(strlen($data) == 16);

$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

/**
* A prettier dd
*
Expand All @@ -49,18 +30,21 @@ public static function validateNIF($nif, $ignoreFirst = true)

if (!is_numeric($nif) || strlen($nif) != 9)
return false;
else {
else
{
$nifSplit = str_split($nif);
//O primeiro digíto tem de ser 1, 2, 5, 6, 8 ou 9
//Ou não, se optarmos por ignorar esta "regra"
if (
in_array($nifSplit[0], array(1, 2, 5, 6, 7, 8, 9))
||
$ignoreFirst
) {
)
{
//Calculamos o dígito de controlo
$checkDigit = 0;
for ($i = 0; $i < 8; $i++) {
for ($i = 0; $i < 8; $i++)
{
$checkDigit += $nifSplit[$i] * (10 - $i - 1);
}
$checkDigit = 11 - ($checkDigit % 11);
Expand All @@ -71,7 +55,8 @@ public static function validateNIF($nif, $ignoreFirst = true)
return true;
else
return false;
} else
}
else
return false;
}
}
Expand Down Expand Up @@ -102,8 +87,8 @@ public static function writeInEnv($key, $value)
if ($value) $strValue = 'true';
else $strValue = 'false';

$fileString = preg_replace($pattern,"$1$strValue",$string);
file_put_contents($envFile,$fileString);
$fileString = preg_replace($pattern, "$1$strValue", $string);
file_put_contents($envFile, $fileString);
}

/**
Expand All @@ -115,9 +100,10 @@ public static function writeInEnv($key, $value)
* @param string $f
* @return float
*/
public static function truncateValue($val, $f="0")
public static function truncateValue($val, $f = "0")
{
if(($p = strpos($val, '.')) !== false) {
if (($p = strpos($val, '.')) !== false)
{
$val = floatval(substr($val, 0, $p + 1 + $f));
}
return $val;
Expand Down

0 comments on commit 91a9577

Please sign in to comment.