Version 2.0.0
This commit is contained in:
16
.travis.yml
Normal file
16
.travis.yml
Normal file
@@ -0,0 +1,16 @@
|
||||
language: php
|
||||
dist: precise
|
||||
php:
|
||||
- '7.1'
|
||||
- '7.2'
|
||||
- nightly
|
||||
sudo: false
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache
|
||||
|
||||
before_script:
|
||||
- travis_retry composer self-update
|
||||
- travis_retry composer install --no-interaction --prefer-dist
|
||||
|
||||
script: composer run test
|
||||
18
CHANGELOG.md
Normal file
18
CHANGELOG.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# CHANGELOG
|
||||
|
||||
##v1.0.0 (08.08.2018)
|
||||
- Create Initial Classes (Rut, Validator, ChainRutValidator)
|
||||
- Tested Rut
|
||||
- Tested SimpleRutValidator
|
||||
- Tested Symfony Form Type
|
||||
|
||||
##v2.0.0 (12.10.2018)
|
||||
This is a major version because api changes and class renaming took place.
|
||||
Also, all features were properly tested with CI/CD pipelines.
|
||||
|
||||
- Changed ChainRutValidator constructor signature. Now uses argument spreading.
|
||||
- Added tests for ChainRutValidator
|
||||
- SimpleRutValidator renamed to Module11RutValidator
|
||||
- Now class Rut always validates itself with the SimpleRutValidator if no validator
|
||||
is passed. This ensures object consistency.
|
||||
- Added test coverage reports, code quality and CI testing
|
||||
14
README.md
14
README.md
@@ -1,6 +1,10 @@
|
||||
Rut Chileno
|
||||
===========
|
||||
|
||||
[](https://travis-ci.org/mnavarrocarter/chilean-rut)
|
||||
[](https://codeclimate.com/github/mnavarrocarter/chilean-rut/maintainability)
|
||||
[](https://codeclimate.com/github/mnavarrocarter/chilean-rut/test_coverage)
|
||||
|
||||
Esta librería implementa una clase Rut como un *value object* inmutable, incluyendo
|
||||
una api de validación flexible y extendible.
|
||||
|
||||
@@ -48,7 +52,7 @@ $rut = new Rut('23.546.565-4', new SimpleRutValidator());
|
||||
```
|
||||
|
||||
### Validación Personalizada de Rut
|
||||
El `SimpleRutValidator` no es más que la implementación del validador clásico de Rut,
|
||||
El `Module11RutValidator` no es más que la implementación del validador clásico de Rut,
|
||||
el algoritmo de módulo 11. Esto verifica que un Rut es algoritmicamente correcto, pero
|
||||
no valida que es real.
|
||||
|
||||
@@ -114,10 +118,10 @@ use MNC\ChileanRut\Validator\ChainRutValidator;
|
||||
use MNC\ChileanRut\Validator\SimpleRutValidator;
|
||||
use App\Rut\DatabaseRutValidator;
|
||||
|
||||
$chainValidator = new ChainRutValidator();
|
||||
$chainValidator
|
||||
->append(new SimpleRutValidator())
|
||||
->append(new DatabaseRutValidator());
|
||||
$chainValidator = new ChainRutValidator(
|
||||
new SimpleRutValidator(),
|
||||
new DatabaseRutValidator()
|
||||
);
|
||||
|
||||
$rut = new Rut('14.245.245-2');
|
||||
|
||||
|
||||
@@ -30,5 +30,9 @@
|
||||
"psr-4": {
|
||||
"MNC\\ChileanRut\\Tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "@php vendor/bin/phpunit --verbose",
|
||||
"style": "@php vendor/bin/php-cs-fixer fix"
|
||||
}
|
||||
}
|
||||
|
||||
216
coverage.xml
Normal file
216
coverage.xml
Normal file
@@ -0,0 +1,216 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage generated="1539368191">
|
||||
<project timestamp="1539368191">
|
||||
<package name="MNC\ChileanRut\Bridge\Doctrine\DBAL\Types">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Bridge/Doctrine/DBAL/Types/RutType.php">
|
||||
<class name="MNC\ChileanRut\Bridge\Doctrine\DBAL\Types\RutType" namespace="MNC\ChileanRut\Bridge\Doctrine\DBAL\Types">
|
||||
<metrics complexity="7" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="18" coveredstatements="0" elements="21" coveredelements="0"/>
|
||||
</class>
|
||||
<line num="30" type="method" name="getName" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="31" type="stmt" count="0"/>
|
||||
<line num="32" type="stmt" count="0"/>
|
||||
<line num="33" type="stmt" count="0"/>
|
||||
<line num="43" type="method" name="convertToDatabaseValue" visibility="public" complexity="3" crap="12.00" count="0"/>
|
||||
<line num="44" type="stmt" count="0"/>
|
||||
<line num="45" type="stmt" count="0"/>
|
||||
<line num="46" type="stmt" count="0"/>
|
||||
<line num="47" type="stmt" count="0"/>
|
||||
<line num="49" type="stmt" count="0"/>
|
||||
<line num="50" type="stmt" count="0"/>
|
||||
<line num="51" type="stmt" count="0"/>
|
||||
<line num="53" type="stmt" count="0"/>
|
||||
<line num="54" type="stmt" count="0"/>
|
||||
<line num="62" type="method" name="convertToPHPValue" visibility="public" complexity="3" crap="12.00" count="0"/>
|
||||
<line num="63" type="stmt" count="0"/>
|
||||
<line num="64" type="stmt" count="0"/>
|
||||
<line num="65" type="stmt" count="0"/>
|
||||
<line num="66" type="stmt" count="0"/>
|
||||
<line num="68" type="stmt" count="0"/>
|
||||
<line num="69" type="stmt" count="0"/>
|
||||
<metrics loc="70" ncloc="41" classes="1" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="18" coveredstatements="0" elements="21" coveredelements="0"/>
|
||||
</file>
|
||||
</package>
|
||||
<package name="MNC\ChileanRut\Bridge\Symfony\Form">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Bridge/Symfony/Form/RutType.php">
|
||||
<class name="MNC\ChileanRut\Bridge\Symfony\Form\RutType" namespace="MNC\ChileanRut\Bridge\Symfony\Form">
|
||||
<metrics complexity="7" methods="5" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="12" coveredstatements="10" elements="17" coveredelements="13"/>
|
||||
</class>
|
||||
<line num="27" type="method" name="buildForm" visibility="public" complexity="1" crap="1" count="1"/>
|
||||
<line num="29" type="stmt" count="1"/>
|
||||
<line num="30" type="stmt" count="1"/>
|
||||
<line num="35" type="method" name="configureOptions" visibility="public" complexity="1" crap="1.01" count="1"/>
|
||||
<line num="37" type="stmt" count="1"/>
|
||||
<line num="38" type="stmt" count="1"/>
|
||||
<line num="41" type="stmt" count="0"/>
|
||||
<line num="42" type="stmt" count="1"/>
|
||||
<line num="44" type="stmt" count="1"/>
|
||||
<line num="49" type="method" name="getBlockPrefix" visibility="public" complexity="2" crap="2" count="1"/>
|
||||
<line num="51" type="stmt" count="1"/>
|
||||
<line num="59" type="method" name="transform" visibility="public" complexity="2" crap="2.15" count="1"/>
|
||||
<line num="61" type="stmt" count="1"/>
|
||||
<line num="62" type="stmt" count="0"/>
|
||||
<line num="64" type="stmt" count="1"/>
|
||||
<line num="71" type="method" name="reverseTransform" visibility="public" complexity="1" crap="1" count="1"/>
|
||||
<line num="73" type="stmt" count="1"/>
|
||||
<metrics loc="75" ncloc="47" classes="1" methods="5" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="12" coveredstatements="10" elements="17" coveredelements="13"/>
|
||||
</file>
|
||||
</package>
|
||||
<package name="MNC\ChileanRut\Bridge\Symfony\Validator">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Bridge/Symfony/Validator/IsValidRut.php">
|
||||
<class name="MNC\ChileanRut\Bridge\Symfony\Validator\IsValidRut" namespace="MNC\ChileanRut\Bridge\Symfony\Validator">
|
||||
<metrics complexity="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
|
||||
</class>
|
||||
<metrics loc="21" ncloc="11" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
|
||||
</file>
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Bridge/Symfony/Validator/IsValidRutValidator.php">
|
||||
<class name="MNC\ChileanRut\Bridge\Symfony\Validator\IsValidRutValidator" namespace="MNC\ChileanRut\Bridge\Symfony\Validator">
|
||||
<metrics complexity="6" methods="2" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="18" coveredstatements="0" elements="20" coveredelements="0"/>
|
||||
</class>
|
||||
<line num="38" type="method" name="__construct" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="39" type="stmt" count="0"/>
|
||||
<line num="40" type="stmt" count="0"/>
|
||||
<line num="41" type="stmt" count="0"/>
|
||||
<line num="47" type="method" name="validate" visibility="public" complexity="5" crap="30.00" count="0"/>
|
||||
<line num="48" type="stmt" count="0"/>
|
||||
<line num="49" type="stmt" count="0"/>
|
||||
<line num="50" type="stmt" count="0"/>
|
||||
<line num="51" type="stmt" count="0"/>
|
||||
<line num="53" type="stmt" count="0"/>
|
||||
<line num="54" type="stmt" count="0"/>
|
||||
<line num="55" type="stmt" count="0"/>
|
||||
<line num="57" type="stmt" count="0"/>
|
||||
<line num="58" type="stmt" count="0"/>
|
||||
<line num="59" type="stmt" count="0"/>
|
||||
<line num="60" type="stmt" count="0"/>
|
||||
<line num="61" type="stmt" count="0"/>
|
||||
<line num="62" type="stmt" count="0"/>
|
||||
<line num="63" type="stmt" count="0"/>
|
||||
<line num="64" type="stmt" count="0"/>
|
||||
<metrics loc="65" ncloc="41" classes="1" methods="2" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="18" coveredstatements="0" elements="20" coveredelements="0"/>
|
||||
</file>
|
||||
</package>
|
||||
<package name="MNC\ChileanRut\Exception">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Exception/InvalidRutException.php">
|
||||
<class name="MNC\ChileanRut\Exception\InvalidRutException" namespace="MNC\ChileanRut\Exception">
|
||||
<metrics complexity="3" methods="2" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="6" coveredstatements="5" elements="8" coveredelements="6"/>
|
||||
</class>
|
||||
<line num="33" type="method" name="__construct" visibility="public" complexity="2" crap="2" count="3"/>
|
||||
<line num="35" type="stmt" count="3"/>
|
||||
<line num="36" type="stmt" count="3"/>
|
||||
<line num="38" type="stmt" count="3"/>
|
||||
<line num="39" type="stmt" count="3"/>
|
||||
<line num="40" type="stmt" count="3"/>
|
||||
<line num="45" type="method" name="getRut" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="47" type="stmt" count="0"/>
|
||||
<metrics loc="49" ncloc="25" classes="1" methods="2" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="6" coveredstatements="5" elements="8" coveredelements="6"/>
|
||||
</file>
|
||||
</package>
|
||||
<package name="MNC\ChileanRut">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Rut.php">
|
||||
<class name="MNC\ChileanRut\Rut" namespace="MNC\ChileanRut">
|
||||
<metrics complexity="16" methods="11" coveredmethods="10" conditionals="0" coveredconditionals="0" statements="31" coveredstatements="30" elements="42" coveredelements="40"/>
|
||||
</class>
|
||||
<line num="45" type="method" name="__construct" visibility="public" complexity="2" crap="2" count="14"/>
|
||||
<line num="47" type="stmt" count="14"/>
|
||||
<line num="48" type="stmt" count="14"/>
|
||||
<line num="49" type="stmt" count="14"/>
|
||||
<line num="51" type="stmt" count="14"/>
|
||||
<line num="52" type="stmt" count="12"/>
|
||||
<line num="54" type="stmt" count="14"/>
|
||||
<line num="55" type="stmt" count="12"/>
|
||||
<line num="62" type="method" name="__toString" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="64" type="stmt" count="0"/>
|
||||
<line num="76" type="method" name="fromParts" visibility="public" complexity="1" crap="1" count="2"/>
|
||||
<line num="78" type="stmt" count="2"/>
|
||||
<line num="89" type="method" name="fromString" visibility="public" complexity="1" crap="1" count="1"/>
|
||||
<line num="91" type="stmt" count="1"/>
|
||||
<line num="101" type="method" name="isEqualTo" visibility="public" complexity="1" crap="1" count="2"/>
|
||||
<line num="103" type="stmt" count="2"/>
|
||||
<line num="113" type="method" name="format" visibility="public" complexity="5" crap="5" count="10"/>
|
||||
<line num="116" type="stmt" count="10"/>
|
||||
<line num="117" type="stmt" count="3"/>
|
||||
<line num="119" type="stmt" count="7"/>
|
||||
<line num="120" type="stmt" count="1"/>
|
||||
<line num="122" type="stmt" count="6"/>
|
||||
<line num="123" type="stmt" count="4"/>
|
||||
<line num="125" type="stmt" count="2"/>
|
||||
<line num="126" type="stmt" count="1"/>
|
||||
<line num="129" type="stmt" count="1"/>
|
||||
<line num="130" type="stmt" count="1"/>
|
||||
<line num="131" type="stmt" count="1"/>
|
||||
<line num="132" type="stmt" count="1"/>
|
||||
<line num="133" type="stmt" count="1"/>
|
||||
<line num="144" type="method" name="getCorrelative" visibility="public" complexity="1" crap="1" count="14"/>
|
||||
<line num="146" type="stmt" count="14"/>
|
||||
<line num="154" type="method" name="getVerifierDigit" visibility="public" complexity="1" crap="1" count="14"/>
|
||||
<line num="156" type="stmt" count="14"/>
|
||||
<line num="166" type="method" name="sanitize" visibility="private" complexity="1" crap="1" count="14"/>
|
||||
<line num="168" type="stmt" count="14"/>
|
||||
<line num="176" type="method" name="formatReadable" visibility="private" complexity="1" crap="1" count="5"/>
|
||||
<line num="178" type="stmt" count="5"/>
|
||||
<line num="186" type="method" name="formatHidden" visibility="private" complexity="1" crap="1" count="1"/>
|
||||
<line num="188" type="stmt" count="1"/>
|
||||
<line num="189" type="stmt" count="1"/>
|
||||
<line num="191" type="stmt" count="1"/>
|
||||
<metrics loc="193" ncloc="96" classes="1" methods="11" coveredmethods="10" conditionals="0" coveredconditionals="0" statements="31" coveredstatements="30" elements="42" coveredelements="40"/>
|
||||
</file>
|
||||
</package>
|
||||
<package name="MNC\ChileanRut\Util">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Util/CorrelativeUtils.php">
|
||||
<class name="MNC\ChileanRut\Util\CorrelativeUtils" namespace="MNC\ChileanRut\Util">
|
||||
<metrics complexity="7" methods="3" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="16" coveredstatements="14" elements="19" coveredelements="16"/>
|
||||
</class>
|
||||
<line num="29" type="method" name="findVerifierDigit" visibility="public" complexity="5" crap="5.09" count="14"/>
|
||||
<line num="31" type="stmt" count="14"/>
|
||||
<line num="32" type="stmt" count="14"/>
|
||||
<line num="34" type="stmt" count="14"/>
|
||||
<line num="35" type="stmt" count="14"/>
|
||||
<line num="36" type="stmt" count="14"/>
|
||||
<line num="38" type="stmt" count="14"/>
|
||||
<line num="39" type="stmt" count="14"/>
|
||||
<line num="42" type="stmt" count="14"/>
|
||||
<line num="44" type="stmt" count="14"/>
|
||||
<line num="45" type="stmt" count="0"/>
|
||||
<line num="48" type="stmt" count="14"/>
|
||||
<line num="49" type="stmt" count="0"/>
|
||||
<line num="52" type="stmt" count="14"/>
|
||||
<line num="62" type="method" name="createValidRutOnlyFromCorrelative" visibility="public" complexity="1" crap="1" count="2"/>
|
||||
<line num="64" type="stmt" count="2"/>
|
||||
<line num="72" type="method" name="autoGenerateValidRut" visibility="public" complexity="1" crap="1" count="2"/>
|
||||
<line num="74" type="stmt" count="2"/>
|
||||
<line num="76" type="stmt" count="2"/>
|
||||
<metrics loc="78" ncloc="47" classes="1" methods="3" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="16" coveredstatements="14" elements="19" coveredelements="16"/>
|
||||
</file>
|
||||
</package>
|
||||
<package name="MNC\ChileanRut\Validator">
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Validator/ChainRutValidator.php">
|
||||
<class name="MNC\ChileanRut\Validator\ChainRutValidator" namespace="MNC\ChileanRut\Validator">
|
||||
<metrics complexity="3" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="5" coveredstatements="5" elements="7" coveredelements="7"/>
|
||||
</class>
|
||||
<line num="36" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="2"/>
|
||||
<line num="38" type="stmt" count="2"/>
|
||||
<line num="39" type="stmt" count="2"/>
|
||||
<line num="46" type="method" name="validate" visibility="public" complexity="2" crap="2" count="2"/>
|
||||
<line num="48" type="stmt" count="2"/>
|
||||
<line num="49" type="stmt" count="2"/>
|
||||
<line num="51" type="stmt" count="1"/>
|
||||
<metrics loc="52" ncloc="24" classes="1" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="5" coveredstatements="5" elements="7" coveredelements="7"/>
|
||||
</file>
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Validator/Module11RutValidator.php">
|
||||
<class name="MNC\ChileanRut\Validator\Module11RutValidator" namespace="MNC\ChileanRut\Validator">
|
||||
<metrics complexity="2" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="4" coveredstatements="4" elements="5" coveredelements="5"/>
|
||||
</class>
|
||||
<line num="29" type="method" name="validate" visibility="public" complexity="2" crap="2" count="14"/>
|
||||
<line num="31" type="stmt" count="14"/>
|
||||
<line num="33" type="stmt" count="14"/>
|
||||
<line num="34" type="stmt" count="2"/>
|
||||
<line num="36" type="stmt" count="12"/>
|
||||
<metrics loc="37" ncloc="20" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="4" coveredstatements="4" elements="5" coveredelements="5"/>
|
||||
</file>
|
||||
</package>
|
||||
<file name="/home/mnavarro/code-hosted/github.com/mnavarrocarter/chilean-rut/src/Validator/RutValidator.php">
|
||||
<metrics loc="43" ncloc="12" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
|
||||
</file>
|
||||
<metrics files="10" loc="683" ncloc="364" classes="8" methods="29" coveredmethods="19" conditionals="0" coveredconditionals="0" statements="110" coveredstatements="68" elements="139" coveredelements="87"/>
|
||||
</project>
|
||||
</coverage>
|
||||
@@ -16,6 +16,10 @@
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<logging>
|
||||
<log type="coverage-clover" target="coverage.xml"/>
|
||||
</logging>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./src</directory>
|
||||
|
||||
@@ -47,10 +47,10 @@ class RutType extends StringType
|
||||
}
|
||||
|
||||
if ($value instanceof Rut) {
|
||||
return parent::convertToDatabaseValue($value->format(), $platform);
|
||||
return $value->format(Rut::FORMAT_HYPHENED);
|
||||
}
|
||||
|
||||
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'Rut']);
|
||||
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', Rut::class]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,8 +61,6 @@ class RutType extends StringType
|
||||
*/
|
||||
public function convertToPHPValue($value, AbstractPlatform $platform)
|
||||
{
|
||||
$value = parent::convertToPHPValue($value, $platform);
|
||||
|
||||
if (null === $value || $value instanceof Rut) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
@@ -17,5 +17,5 @@ use Symfony\Component\Validator\Constraint;
|
||||
*/
|
||||
class IsValidRut extends Constraint
|
||||
{
|
||||
public $message = 'The rut "{{value}}" is not valid.';
|
||||
public $message = '"{{value}}" is not a valid Rut.';
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ namespace MNC\ChileanRut\Bridge\Symfony\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use MNC\ChileanRut\Validator\RutValidator;
|
||||
use MNC\ChileanRut\Validator\SimpleRutValidator;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
@@ -30,9 +30,14 @@ class IsValidRutValidator extends ConstraintValidator
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* IsValidRutValidator constructor.
|
||||
*
|
||||
* @param RutValidator|null $validator
|
||||
*/
|
||||
public function __construct(RutValidator $validator = null)
|
||||
{
|
||||
$this->validator = $validator ?? new SimpleRutValidator();
|
||||
$this->validator = $validator ?? new Module11RutValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
43
src/Rut.php
43
src/Rut.php
@@ -10,10 +10,13 @@
|
||||
|
||||
namespace MNC\ChileanRut;
|
||||
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use MNC\ChileanRut\Validator\RutValidator;
|
||||
|
||||
/**
|
||||
* Class Rut.
|
||||
* Rut represents a the Chilean National ID Number.
|
||||
*
|
||||
* All residents of Chile are uniquely identified by one of these.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
@@ -45,12 +48,15 @@ class Rut
|
||||
$this->value = substr($sanitized, 0, -1);
|
||||
$this->dv = $sanitized[\strlen($sanitized) - 1];
|
||||
|
||||
if (null !== $validator) {
|
||||
$validator->validate($this);
|
||||
if (!$validator instanceof RutValidator) {
|
||||
$validator = new Module11RutValidator();
|
||||
}
|
||||
$validator->validate($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts the Rut object into a string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
@@ -59,27 +65,35 @@ class Rut
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $correlative
|
||||
* @param string $verifierDigit
|
||||
* Creates a new Rut instance from the correlative and the verifier digit.
|
||||
*
|
||||
* @param string $correlative
|
||||
* @param string $verifierDigit
|
||||
* @param RutValidator|null $validator
|
||||
*
|
||||
* @return Rut
|
||||
*/
|
||||
public static function fromParts(string $correlative, string $verifierDigit): Rut
|
||||
public static function fromParts(string $correlative, string $verifierDigit, RutValidator $validator = null): Rut
|
||||
{
|
||||
return new self($correlative.$verifierDigit);
|
||||
return new self($correlative.$verifierDigit, $validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $rut
|
||||
* Creates a new instance of Rut from a string.
|
||||
*
|
||||
* @param string $rut
|
||||
* @param RutValidator|null $validator
|
||||
*
|
||||
* @return Rut
|
||||
*/
|
||||
public static function fromString(string $rut): Rut
|
||||
public static function fromString(string $rut, RutValidator $validator = null): Rut
|
||||
{
|
||||
return new self($rut);
|
||||
return new self($rut, $validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares whether a Rut is equal to another or not.
|
||||
*
|
||||
* @param Rut $rut
|
||||
*
|
||||
* @return bool
|
||||
@@ -151,13 +165,12 @@ class Rut
|
||||
*/
|
||||
private function sanitize(string $value): string
|
||||
{
|
||||
$value = trim($value);
|
||||
$value = strtoupper($value);
|
||||
|
||||
return str_replace(['.', ',', '-'], '', $value);
|
||||
return str_replace(['.', ',', '-'], '', strtoupper(trim($value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to format the Rut as FORMAT_READABLE.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function formatReadable(): string
|
||||
@@ -166,6 +179,8 @@ class Rut
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to format the Rut as FORMAT_HIDDEN.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function formatHidden(): string
|
||||
|
||||
@@ -68,8 +68,6 @@ class CorrelativeUtils
|
||||
* Auto-generates an algorithmically valid Rut, because why not.
|
||||
*
|
||||
* @return Rut
|
||||
*
|
||||
* @throws \Exception on insufficient entropy on correlative generation
|
||||
*/
|
||||
public static function autoGenerateValidRut(): Rut
|
||||
{
|
||||
|
||||
@@ -30,24 +30,12 @@ class ChainRutValidator implements RutValidator
|
||||
|
||||
/**
|
||||
* ChainRutValidator constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->validators = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a RutValidator instance to the validation chain.
|
||||
*
|
||||
* @param RutValidator $validator
|
||||
*
|
||||
* @return ChainRutValidator
|
||||
* @param RutValidator[] $validators
|
||||
*/
|
||||
public function append(RutValidator $validator): ChainRutValidator
|
||||
public function __construct(RutValidator ...$validators)
|
||||
{
|
||||
$this->validators[] = $validator;
|
||||
|
||||
return $this;
|
||||
$this->validators = $validators;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ use MNC\ChileanRut\Util\CorrelativeUtils;
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class SimpleRutValidator implements RutValidator
|
||||
class Module11RutValidator implements RutValidator
|
||||
{
|
||||
/**
|
||||
* @param Rut $rut
|
||||
@@ -17,7 +17,7 @@ use MNC\ChileanRut\Rut;
|
||||
* This is the base contract for a Rut validator.
|
||||
*
|
||||
* You can implement any logic here that you can use to validate a Rut.
|
||||
* For example, the SimpleRutValidator only validates that a Rut is algorithmically
|
||||
* For example, the Module11RutValidator only validates that a Rut is algorithmically
|
||||
* correct, but not that it actually exists.
|
||||
*
|
||||
* You could create a HTTPRutValidator that performs a request to validate that a
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace MNC\ChileanRut\Tests\Rut;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Validator\SimpleRutValidator;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RutTest extends TestCase
|
||||
@@ -60,13 +60,13 @@ class RutTest extends TestCase
|
||||
{
|
||||
$this->expectException(InvalidRutException::class);
|
||||
|
||||
$validator = new SimpleRutValidator();
|
||||
$validator = new Module11RutValidator();
|
||||
$rut = new Rut('4444444-2', $validator);
|
||||
}
|
||||
|
||||
public function testThatIntegratedValidationDoesNotThrowExceptionOnValidRut()
|
||||
{
|
||||
$validator = new SimpleRutValidator();
|
||||
$validator = new Module11RutValidator();
|
||||
$rut = new Rut('16.894.365-2', $validator);
|
||||
|
||||
$this->assertInstanceOf(Rut::class, $rut);
|
||||
|
||||
56
tests/Validator/ChainRutValidatorTest.php
Normal file
56
tests/Validator/ChainRutValidatorTest.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
*
|
||||
* (c) Matías Navarro Carter <mnavarrocarter@gmail.com>
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace MNC\ChileanRut\Tests\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Util\CorrelativeUtils;
|
||||
use MNC\ChileanRut\Validator\ChainRutValidator;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use MNC\ChileanRut\Validator\RutValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ChainRutValidatorTest.
|
||||
*/
|
||||
class ChainRutValidatorTest extends TestCase
|
||||
{
|
||||
public function testThatChainValidatorFails(): void
|
||||
{
|
||||
$rut = CorrelativeUtils::autoGenerateValidRut();
|
||||
|
||||
$normalValidator = new Module11RutValidator();
|
||||
$mockValidator = $this->createMock(RutValidator::class);
|
||||
$mockValidator->expects($this->once())
|
||||
->method('validate')
|
||||
->willThrowException(new InvalidRutException($rut));
|
||||
|
||||
$chainValidator = new ChainRutValidator($normalValidator, $mockValidator);
|
||||
|
||||
$this->expectException(InvalidRutException::class);
|
||||
$chainValidator->validate($rut);
|
||||
}
|
||||
|
||||
public function testThatChainValidatorPasses(): void
|
||||
{
|
||||
$rut = CorrelativeUtils::autoGenerateValidRut();
|
||||
|
||||
$normalValidator = new Module11RutValidator();
|
||||
$mockValidator = $this->createMock(RutValidator::class);
|
||||
$mockValidator->expects($this->once())
|
||||
->method('validate')
|
||||
->willReturn(null);
|
||||
|
||||
$chainValidator = new ChainRutValidator($normalValidator, $mockValidator);
|
||||
|
||||
$chainValidator->validate($rut);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace MNC\ChileanRut\Tests\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Validator\SimpleRutValidator;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SimpleRutValidatorTest extends TestCase
|
||||
@@ -20,7 +20,7 @@ class SimpleRutValidatorTest extends TestCase
|
||||
public function testValidationPassesOnValidRut()
|
||||
{
|
||||
$rut = new Rut('16.894.365-2');
|
||||
$validator = new SimpleRutValidator();
|
||||
$validator = new Module11RutValidator();
|
||||
|
||||
$validator->validate($rut);
|
||||
|
||||
@@ -32,7 +32,7 @@ class SimpleRutValidatorTest extends TestCase
|
||||
$this->expectException(InvalidRutException::class);
|
||||
|
||||
$rut = new Rut('34.4534.353-1');
|
||||
$validator = new SimpleRutValidator();
|
||||
$validator = new Module11RutValidator();
|
||||
|
||||
$validator->validate($rut);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user