From 9877455aa5ebe21221bcae07192d3cfe5a6985a6 Mon Sep 17 00:00:00 2001 From: Francisco Luz Date: Sat, 21 Apr 2018 21:44:11 -0400 Subject: [PATCH 1/4] Created DocumentoAbstract.php --- src/DocumentoAbstract.php | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/DocumentoAbstract.php diff --git a/src/DocumentoAbstract.php b/src/DocumentoAbstract.php new file mode 100644 index 0000000..99922b9 --- /dev/null +++ b/src/DocumentoAbstract.php @@ -0,0 +1,58 @@ +setValue($value); + } + + + abstract public function isValid(); + abstract public function format(); + + /** + * Get class name without namespace + * + * @return string + */ + public function getClassName() + { + return substr(strrchr(get_class($this), '\\'), 1); + } + + /** + * Get the raw value + * + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * Set the clean value + * + * @return self + */ + public function setValue($value) + { + $this->value = (string) preg_replace('/[^0-9]/', '', $value); + return $this; + } +} From e76c8d1d12ff2a42c537bc429ccb6764c653c90a Mon Sep 17 00:00:00 2001 From: Francisco Luz Date: Sat, 21 Apr 2018 21:45:43 -0400 Subject: [PATCH 2/4] Upteded CNPJ.php --- src/CNPJ.php | 41 ++--------------------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/src/CNPJ.php b/src/CNPJ.php index aa5f112..700badf 100644 --- a/src/CNPJ.php +++ b/src/CNPJ.php @@ -2,21 +2,14 @@ namespace Bissolli\ValidadorCpfCnpj; -class CNPJ +class CNPJ extends DocumentoAbstract { - /** - * Value to be validated - * - * @var string - */ - public $value; - /** * Invalid numbers * * @var string */ - public $blacklist = [ + protected $blacklist = [ '00000000000000', '11111111111111', '22222222222222', @@ -29,16 +22,6 @@ class CNPJ '99999999999999' ]; - /** - * Create a new ValidaDocumento instance - * - * @param string $value - */ - public function __construct($value = null) - { - $this->value = (string) preg_replace('/[^0-9]/', '', $value); - } - /** * Check if it is a valid CNPJ number * @@ -93,24 +76,4 @@ public function format() return $result; } - - /** - * Get class name without namespace - * - * @return string - */ - public function getClassName() - { - return 'CNPJ'; - } - - /** - * Get the raw value - * - * @return string - */ - public function getValue() - { - return $this->value; - } } From 34cf4bb56dd128be0f00bf09fe5f23f024c4dbfd Mon Sep 17 00:00:00 2001 From: Francisco Luz Date: Sat, 21 Apr 2018 21:46:42 -0400 Subject: [PATCH 3/4] Updated CPF.php --- src/CPF.php | 41 ++--------------------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/src/CPF.php b/src/CPF.php index 83c5259..419e945 100644 --- a/src/CPF.php +++ b/src/CPF.php @@ -2,21 +2,14 @@ namespace Bissolli\ValidadorCpfCnpj; -class CPF +class CPF extends DocumentoAbstract { - /** - * Value to be validated - * - * @var string - */ - public $value; - /** * Invalid numbers * * @var string */ - private $blacklist = [ + protected $blacklist = [ '00000000000', '11111111111', '22222222222', @@ -29,16 +22,6 @@ class CPF '99999999999' ]; - /** - * Create a new ValidaDocumento instance - * - * @param string $value - */ - public function __construct($value = null) - { - $this->value = (string) preg_replace('/[^0-9]/', '', $value); - } - /** * Check if it is a valid CPF number * @@ -90,24 +73,4 @@ public function format() return $result; } - - /** - * Get class name without namespace - * - * @return string - */ - public function getClassName() - { - return 'CPF'; - } - - /** - * Get the raw value - * - * @return string - */ - public function getValue() - { - return $this->value; - } } From 7d37692ae6d92859a09792e18dcc57fe4110fd0e Mon Sep 17 00:00:00 2001 From: Francisco Luz Date: Sat, 21 Apr 2018 21:47:27 -0400 Subject: [PATCH 4/4] Updated Documento.php --- src/Documento.php | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Documento.php b/src/Documento.php index 3c0ad5a..5cd54e1 100644 --- a/src/Documento.php +++ b/src/Documento.php @@ -2,7 +2,7 @@ namespace Bissolli\ValidadorCpfCnpj; -class Documento +class Documento extends DocumentoAbstract { /** * Value to be validated @@ -11,21 +11,6 @@ class Documento */ public $obj; - /** - * Create a new Documento instance - * - * @param string $value - */ - public function __construct($value = null) - { - $value = (string) preg_replace('/[^0-9]/', '', $value); - - if (strlen($value) === 11) - $this->obj = new CPF($value); - else - $this->obj = new CNPJ($value); - } - /** * Get document type * @@ -68,4 +53,21 @@ public function getValue() { return $this->obj->getValue(); } + + /** + * Set the clean value + * + * @return self + */ + public function setValue($value) + { + $value = (string) preg_replace('/[^0-9]/', '', $value); + + if (strlen($value) === 11) + $this->obj = new CPF($value); + else + $this->obj = new CNPJ($value); + + return $this; + } }