-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
946c97a
commit 15bf164
Showing
5 changed files
with
209 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace PixPhp; | ||
|
||
class PixKey | ||
{ | ||
public static function validateKey($key) | ||
{ | ||
// Trim spaces and special characters | ||
$key = trim($key); | ||
|
||
// Check if the key is a CPF | ||
if (preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $key)) { | ||
return 'CPF'; | ||
} | ||
|
||
// Check if the key is a CNPJ | ||
if (preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $key)) { | ||
return 'CNPJ'; | ||
} | ||
|
||
// Check if the key is an email | ||
if (filter_var($key, FILTER_VALIDATE_EMAIL)) { | ||
return 'EMAIL'; | ||
} | ||
|
||
// Check if the key is a phone number | ||
if (preg_match('/^\+55 \d{2} \d{5}-\d{4}$/', $key)) { | ||
return 'PHONE'; | ||
} | ||
|
||
// Check if the key is a random key | ||
if (preg_match('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', $key)) { | ||
return 'RANDOM'; | ||
} | ||
|
||
// If the key does not match any known format | ||
throw new \InvalidArgumentException('Invalid PIX key.'); | ||
} | ||
|
||
public static function formatKey($key) | ||
{ | ||
$type = self::validateKey($key); | ||
|
||
// Format the key according to its type | ||
switch ($type) { | ||
case 'CPF': | ||
return preg_replace('/[^\d]/', '', $key); | ||
case 'CNPJ': | ||
return preg_replace('/[^\d]/', '', $key); | ||
case 'EMAIL': | ||
return strtolower(trim($key)); | ||
case 'PHONE': | ||
return preg_replace('/[^+\d]/', '', $key); | ||
case 'RANDOM': | ||
return trim($key); | ||
default: | ||
throw new \InvalidArgumentException('Invalid PIX key.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PixPhp\PixKey; | ||
|
||
class PixKeyTest extends TestCase | ||
{ | ||
public function testValidCpf() | ||
{ | ||
$cpf = '123.456.789-09'; | ||
$this->assertEquals('CPF', PixKey::validateKey($cpf)); | ||
$this->assertEquals('12345678909', PixKey::formatKey($cpf)); | ||
} | ||
|
||
public function testValidCnpj() | ||
{ | ||
$cnpj = '12.345.678/0001-95'; | ||
$this->assertEquals('CNPJ', PixKey::validateKey($cnpj)); | ||
$this->assertEquals('12345678000195', PixKey::formatKey($cnpj)); | ||
} | ||
|
||
public function testValidEmail() | ||
{ | ||
$email = 'example@test.com'; | ||
$this->assertEquals('EMAIL', PixKey::validateKey($email)); | ||
$this->assertEquals('example@test.com', PixKey::formatKey($email)); | ||
} | ||
|
||
public function testValidPhone() | ||
{ | ||
$phone = '+55 11 91234-5678'; | ||
$this->assertEquals('PHONE', PixKey::validateKey($phone)); | ||
$this->assertEquals('+5511912345678', PixKey::formatKey($phone)); | ||
} | ||
|
||
public function testInvalidKey() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$invalidKey = 'invalid_key'; | ||
PixKey::validateKey($invalidKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters