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; - } } 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; - } } 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; + } } 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; + } +}