From 3c9d1a591a6e22dd5d2cc7eac45d47e7a7d2d7e2 Mon Sep 17 00:00:00 2001 From: fito Date: Thu, 28 Apr 2022 02:01:53 -0500 Subject: [PATCH 1/5] =?UTF-8?q?Refactorizando=20la=20generaci=C3=B3n=20del?= =?UTF-8?q?=20checksum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CheckSum.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/CheckSum.php b/src/CheckSum.php index 5bc4207..8675967 100644 --- a/src/CheckSum.php +++ b/src/CheckSum.php @@ -6,25 +6,26 @@ final class CheckSum { - private const DICTIONARY = [0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, '&' => 24, 'O' => 25, 'P' => 26, 'Q' => 27, 'R' => 28, 'S' => 29, 'T' => 30, 'U' => 31, 'V' => 32, 'W' => 33, 'X' => 34, 'Y' => 35, 'Z' => 36, ' ' => 37, '#' => 38]; + private const DICTIONARY = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ #'; public function calculate(string $rfc): string { // 'Ñ' translated to '#' because 'Ñ' is multibyte 0xC3 0xB1 - $chars = str_split(str_replace('Ñ', '#', $rfc), 1); - array_pop($chars); // remove predefined checksum + $chars = str_split(str_replace('Ñ', '#', $rfc)); $length = count($chars); - $sum = (11 === $length) ? 481 : 0; // 481 para morales, 0 para físicas - $j = $length + 1; + array_pop($chars); // remove predefined checksum + $sum = (12 === $length) ? 481 : 0; // 481 para morales, 0 para físicas foreach ($chars as $i => $char) { - $sum += self::DICTIONARY[$char] * ($j - $i); + $posChar = strpos(self::DICTIONARY, $char); + $factor = $length - $i; + $sum += $posChar * $factor; } - $digit = strval(11 - $sum % 11); - if ('11' === $digit) { - $digit = '0'; - } elseif ('10' === $digit) { - $digit = 'A'; + $mod11 = $sum % 11; + if ($mod11 === 0) { + return '0'; + } else if($mod11 === 1) { + return 'A'; } - return $digit; + return strval(11 - $mod11); } } From 3e472956613ebc2517cd3f59e25783ca23cfb2a3 Mon Sep 17 00:00:00 2001 From: fito Date: Thu, 28 Apr 2022 10:59:03 -0500 Subject: [PATCH 2/5] Revisando comentarios del pull request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Se regreso el diccionario el cual se genera dinamicamente una unica vez Se revisó detalles del phpcs --- src/CheckSum.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/CheckSum.php b/src/CheckSum.php index 8675967..3607fc9 100644 --- a/src/CheckSum.php +++ b/src/CheckSum.php @@ -6,7 +6,21 @@ final class CheckSum { - private const DICTIONARY = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ #'; + private static $DICTIONARY = []; + + /** + * Se encarga de generar el diccionario a usar. + */ + public function __construct() + { + $dictionary = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ #'; + if (count(self::$DICTIONARY) > 0) { + return; + } + foreach (str_split($dictionary) as $index => $char) { + self::$DICTIONARY[$char] = $index; + } + } public function calculate(string $rfc): string { @@ -16,15 +30,15 @@ public function calculate(string $rfc): string array_pop($chars); // remove predefined checksum $sum = (12 === $length) ? 481 : 0; // 481 para morales, 0 para físicas foreach ($chars as $i => $char) { - $posChar = strpos(self::DICTIONARY, $char); + $posChar = self::$DICTIONARY[$char]; $factor = $length - $i; $sum += $posChar * $factor; } $mod11 = $sum % 11; if ($mod11 === 0) { - return '0'; - } else if($mod11 === 1) { - return 'A'; + return '0'; + } elseif ($mod11 === 1) { + return 'A'; } return strval(11 - $mod11); } From 53fba3b9f4c4e65c946bb5f58070b8a7d4c06982 Mon Sep 17 00:00:00 2001 From: fito Date: Thu, 28 Apr 2022 11:47:12 -0500 Subject: [PATCH 3/5] Revisando los comentarios de phpcs --- src/CheckSum.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/CheckSum.php b/src/CheckSum.php index 3607fc9..b8f3545 100644 --- a/src/CheckSum.php +++ b/src/CheckSum.php @@ -6,7 +6,7 @@ final class CheckSum { - private static $DICTIONARY = []; + private array $dictionary; /** * Se encarga de generar el diccionario a usar. @@ -14,12 +14,11 @@ final class CheckSum public function __construct() { $dictionary = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ #'; - if (count(self::$DICTIONARY) > 0) { - return; - } + $dic = []; foreach (str_split($dictionary) as $index => $char) { - self::$DICTIONARY[$char] = $index; + $dic = array_merge($dic, [$char => $index]); } + $this->dictionary = $dic; } public function calculate(string $rfc): string @@ -30,7 +29,7 @@ public function calculate(string $rfc): string array_pop($chars); // remove predefined checksum $sum = (12 === $length) ? 481 : 0; // 481 para morales, 0 para físicas foreach ($chars as $i => $char) { - $posChar = self::$DICTIONARY[$char]; + $posChar = intval($this->dictionary[$char]); $factor = $length - $i; $sum += $posChar * $factor; } From 9a261931325e7cc9512735054206ee6b253cb91b Mon Sep 17 00:00:00 2001 From: fito Date: Thu, 28 Apr 2022 12:28:18 -0500 Subject: [PATCH 4/5] =?UTF-8?q?Quitando=20la=20declaraci=C3=B3n=20del=20ti?= =?UTF-8?q?po=20array=20en=20el=20diccionario=20(pasando=20php7.3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CheckSum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CheckSum.php b/src/CheckSum.php index b8f3545..377f997 100644 --- a/src/CheckSum.php +++ b/src/CheckSum.php @@ -6,7 +6,7 @@ final class CheckSum { - private array $dictionary; + private $dictionary; /** * Se encarga de generar el diccionario a usar. From d410c4aaf77dc71dc01d16cef97bf2a676673077 Mon Sep 17 00:00:00 2001 From: fito Date: Thu, 28 Apr 2022 12:30:20 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Quitando=20la=20declaraci=C3=B3n=20del=20ti?= =?UTF-8?q?po=20array=20en=20el=20diccionario=20(pasando=20php7.3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CheckSum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CheckSum.php b/src/CheckSum.php index 377f997..194f708 100644 --- a/src/CheckSum.php +++ b/src/CheckSum.php @@ -6,7 +6,7 @@ final class CheckSum { - private $dictionary; + private $dictionary = []; /** * Se encarga de generar el diccionario a usar.