Skip to content

Commit

Permalink
Merge pull request #5 from drupalista-br/patch-1
Browse files Browse the repository at this point in the history
Criado set method para $value property e criado Abstract Class
  • Loading branch information
bissolli authored Apr 23, 2018
2 parents 911ba86 + 7d37692 commit 6f1c824
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 94 deletions.
41 changes: 2 additions & 39 deletions src/CNPJ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
}
41 changes: 2 additions & 39 deletions src/CPF.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
}
34 changes: 18 additions & 16 deletions src/Documento.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bissolli\ValidadorCpfCnpj;

class Documento
class Documento extends DocumentoAbstract
{
/**
* Value to be validated
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
}
58 changes: 58 additions & 0 deletions src/DocumentoAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Bissolli\ValidadorCpfCnpj;

abstract class DocumentoAbstract
{
/**
* Value to be validated
*
* @var string
*/
protected $value;

/**
* Create a new ValidaDocumento instance
*
* @param string $value
*/
public function __construct($value = null)
{
if ($value) $this->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;
}
}

0 comments on commit 6f1c824

Please sign in to comment.