Initial release
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
<?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\Bridge\Doctrine\DBAL\Types;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use Doctrine\DBAL\Types\StringType;
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
|
||||
/**
|
||||
* Class RutType
|
||||
* Class RutType.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class RutType extends StringType
|
||||
@@ -26,7 +35,9 @@ class RutType extends StringType
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param AbstractPlatform $platform
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws ConversionException
|
||||
*/
|
||||
public function convertToDatabaseValue($value, AbstractPlatform $platform)
|
||||
@@ -45,16 +56,17 @@ class RutType extends StringType
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param AbstractPlatform $platform
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function convertToPHPValue($value, AbstractPlatform $platform)
|
||||
{
|
||||
$value = parent::convertToPHPValue($value, $platform);
|
||||
|
||||
if ($value === null || $value instanceof Rut) {
|
||||
if (null === $value || $value instanceof Rut) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return new Rut($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,75 @@
|
||||
<?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\Bridge\Symfony\Form;
|
||||
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Class RutType
|
||||
* @package MNC\ChileanRut\Bridge\Symfony\Form
|
||||
* Class RutType.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class RutType extends TextType
|
||||
class RutType extends AbstractType implements DataTransformerInterface
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->addModelTransformer($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolver $resolver
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'compound' => false,
|
||||
'data_class' => Rut::class,
|
||||
'empty_data' => function (FormInterface $form) {
|
||||
return new Rut($form->getData());
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBlockPrefix(): ?string
|
||||
{
|
||||
return 'rut';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function transform($value)
|
||||
{
|
||||
if ($value instanceof Rut) {
|
||||
return $value->format(Rut::FORMAT_READABLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed|Rut
|
||||
*/
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
return new Rut((string) $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
<?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\Bridge\Symfony\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
@@ -10,4 +18,4 @@ use Symfony\Component\Validator\Constraint;
|
||||
class IsValidRut extends Constraint
|
||||
{
|
||||
public $message = 'The rut "{{value}}" is not valid.';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
<?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\Bridge\Symfony\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Validator\RutValidator;
|
||||
use MNC\ChileanRut\Validator\SimpleRutValidator;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
@@ -11,8 +19,8 @@ use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* Class IsValidRutValidator
|
||||
* @package MNC\ChileanRut\Bridge\Symfony\Validator
|
||||
* Class IsValidRutValidator.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class IsValidRutValidator extends ConstraintValidator
|
||||
@@ -49,4 +57,4 @@ class IsValidRutValidator extends ConstraintValidator
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
<?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\Exception;
|
||||
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
|
||||
/**
|
||||
* Class InvalidRutException
|
||||
* @package MNC\ChileanRut\Rut
|
||||
* Class InvalidRutException.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class InvalidRutException extends \LogicException
|
||||
@@ -18,11 +26,15 @@ class InvalidRutException extends \LogicException
|
||||
|
||||
/**
|
||||
* InvalidRutException constructor.
|
||||
* @param Rut $rut
|
||||
*
|
||||
* @param Rut $rut
|
||||
* @param string|null $message
|
||||
*/
|
||||
public function __construct(Rut $rut)
|
||||
public function __construct(Rut $rut, string $message = null)
|
||||
{
|
||||
$message = sprintf('Rut %s is not a valid rut.', $rut->format(Rut::FORMAT_READABLE));
|
||||
if (null === $message) {
|
||||
$message = sprintf('Rut %s is not a valid rut.', $rut->format(Rut::FORMAT_READABLE));
|
||||
}
|
||||
$this->rut = $rut;
|
||||
parent::__construct($message);
|
||||
}
|
||||
@@ -34,4 +46,4 @@ class InvalidRutException extends \LogicException
|
||||
{
|
||||
return $this->rut;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace MNC\ChileanRut\Rut;
|
||||
/*
|
||||
* 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;
|
||||
|
||||
use MNC\ChileanRut\Validator\RutValidator;
|
||||
|
||||
/**
|
||||
* Class Rut
|
||||
* Class Rut.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class Rut
|
||||
@@ -13,6 +22,7 @@ class Rut
|
||||
public const FORMAT_HYPHENED = 0; // 14533535-5
|
||||
public const FORMAT_CLEAR = 1; // 145335355
|
||||
public const FORMAT_READABLE = 2; // 14.533.535-5
|
||||
public const FORMAT_HIDDEN = 3; // 17.***.***-5
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -25,8 +35,9 @@ class Rut
|
||||
|
||||
/**
|
||||
* Rut constructor.
|
||||
*
|
||||
* @param string $rut
|
||||
* @param RutValidator|null $validator if provided validates the Rut.
|
||||
* @param RutValidator|null $validator if provided validates the Rut
|
||||
*/
|
||||
public function __construct(string $rut, RutValidator $validator = null)
|
||||
{
|
||||
@@ -39,9 +50,18 @@ class Rut
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->format(self::FORMAT_READABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $correlative
|
||||
* @param string $verifierDigit
|
||||
*
|
||||
* @return Rut
|
||||
*/
|
||||
public static function fromParts(string $correlative, string $verifierDigit): Rut
|
||||
@@ -51,6 +71,7 @@ class Rut
|
||||
|
||||
/**
|
||||
* @param string $rut
|
||||
*
|
||||
* @return Rut
|
||||
*/
|
||||
public static function fromString(string $rut): Rut
|
||||
@@ -60,6 +81,7 @@ class Rut
|
||||
|
||||
/**
|
||||
* @param Rut $rut
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEqualTo(Rut $rut): bool
|
||||
@@ -68,31 +90,26 @@ class Rut
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
private function sanitize(string $value): string
|
||||
{
|
||||
$value = trim($value);
|
||||
$value = strtoupper($value);
|
||||
return str_replace(['.', ',', '-'], '', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $format One of the FORMAT_ constants.
|
||||
* Formats a Rut to a string.
|
||||
*
|
||||
* @param int $format one of the FORMAT_ constants
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function format(int $format = 0): string
|
||||
{
|
||||
switch ($format) {
|
||||
case self::FORMAT_HYPHENED:
|
||||
return $this->value . '-' . $this->dv;
|
||||
return $this->value.'-'.$this->dv;
|
||||
break;
|
||||
case self::FORMAT_CLEAR:
|
||||
return $this->value . $this->dv;
|
||||
return $this->value.$this->dv;
|
||||
break;
|
||||
case self::FORMAT_READABLE:
|
||||
return sprintf('%s-%s', number_format($this->value, 0, '', '.'), $this->dv);
|
||||
return $this->formatReadable();
|
||||
break;
|
||||
case self::FORMAT_HIDDEN:
|
||||
return $this->formatHidden();
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(
|
||||
@@ -106,6 +123,8 @@ class Rut
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correlative number of the Rut.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCorrelative(): string
|
||||
@@ -114,6 +133,8 @@ class Rut
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the verifier digit of the Rut.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVerifierDigit(): string
|
||||
@@ -122,10 +143,36 @@ class Rut
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes a Rut string.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
private function sanitize(string $value): string
|
||||
{
|
||||
return $this->format(self::FORMAT_READABLE);
|
||||
$value = trim($value);
|
||||
$value = strtoupper($value);
|
||||
|
||||
return str_replace(['.', ',', '-'], '', $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function formatReadable(): string
|
||||
{
|
||||
return sprintf('%s-%s', number_format($this->value, 0, '', '.'), $this->dv);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function formatHidden(): string
|
||||
{
|
||||
$readable = $this->formatReadable();
|
||||
$exploded = explode('.', $readable);
|
||||
|
||||
return sprintf('%s.***.***-%s', $exploded[0], $this->dv);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,29 @@
|
||||
<?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\Util;
|
||||
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
|
||||
/**
|
||||
* This class provides utils for a Rut correlative.
|
||||
*
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
class Correlative
|
||||
class CorrelativeUtils
|
||||
{
|
||||
/**
|
||||
* Finds the verifier digit of a correlative.
|
||||
*
|
||||
* @param string $correlative
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function findVerifierDigit(string $correlative): string
|
||||
@@ -22,21 +31,21 @@ class Correlative
|
||||
$x = 2;
|
||||
$s = 0;
|
||||
|
||||
for ($i = \strlen($correlative) - 1; $i >= 0; $i--) {
|
||||
for ($i = \strlen($correlative) - 1; $i >= 0; --$i) {
|
||||
if ($x > 7) {
|
||||
$x = 2;
|
||||
}
|
||||
$s += $correlative[$i] * $x;
|
||||
$x++;
|
||||
++$x;
|
||||
}
|
||||
|
||||
$dv = 11 - ($s % 11);
|
||||
|
||||
if ($dv === 10) {
|
||||
if (10 === $dv) {
|
||||
$dv = 'K';
|
||||
}
|
||||
|
||||
if ($dv === 11) {
|
||||
if (11 === $dv) {
|
||||
$dv = '0';
|
||||
}
|
||||
|
||||
@@ -47,10 +56,25 @@ class Correlative
|
||||
* Instantiates a valid Rut object just providing a correlative.
|
||||
*
|
||||
* @param string $correlative
|
||||
*
|
||||
* @return Rut
|
||||
*/
|
||||
public static function createValidRutOnlyFromCorrelative(string $correlative): Rut
|
||||
{
|
||||
return Rut::fromParts($correlative, static::findVerifierDigit($correlative));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-generates an algorithmically valid Rut, because why not.
|
||||
*
|
||||
* @return Rut
|
||||
*
|
||||
* @throws \Exception on insufficient entropy on correlative generation
|
||||
*/
|
||||
public static function autoGenerateValidRut(): Rut
|
||||
{
|
||||
$correlative = \random_int(1000000, 40000000);
|
||||
|
||||
return static::createValidRutOnlyFromCorrelative($correlative);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
<?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\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
|
||||
/**
|
||||
* A ChainRutValidator
|
||||
* A ChainRutValidator.
|
||||
*
|
||||
* Use this implementation when you want to validate a Rut against multiple
|
||||
* validators. Add the validators in order by calling addValidator().
|
||||
@@ -29,18 +37,23 @@ class ChainRutValidator implements RutValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a RutValidator instance to the validation chain.
|
||||
*
|
||||
* @param RutValidator $validator
|
||||
*
|
||||
* @return ChainRutValidator
|
||||
*/
|
||||
public function addValidator(RutValidator $validator): ChainRutValidator
|
||||
public function append(RutValidator $validator): ChainRutValidator
|
||||
{
|
||||
$this->validators[] = $validator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rut $rut
|
||||
* @throws InvalidRutException on invalid Rut.
|
||||
*
|
||||
* @throws InvalidRutException on invalid Rut
|
||||
*/
|
||||
public function validate(Rut $rut): void
|
||||
{
|
||||
@@ -48,4 +61,4 @@ class ChainRutValidator implements RutValidator
|
||||
$validator->validate($rut);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
<?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\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Rut;
|
||||
|
||||
/**
|
||||
* This is the base contract for a Rut validator.
|
||||
@@ -15,7 +23,6 @@ use MNC\ChileanRut\Rut\Rut;
|
||||
* You could create a HTTPRutValidator that performs a request to validate that a
|
||||
* Rut exists against a Rest Api or a third party service.
|
||||
*
|
||||
* @package MNC\ChileanRut\Validator
|
||||
* @author Matías Navarro Carter <mnavarro@option.cl>
|
||||
*/
|
||||
interface RutValidator
|
||||
@@ -29,7 +36,8 @@ interface RutValidator
|
||||
* error according to their business rules.
|
||||
*
|
||||
* @param Rut $rut
|
||||
* @throws InvalidRutException on invalid Rut.
|
||||
*
|
||||
* @throws InvalidRutException on invalid Rut
|
||||
*/
|
||||
public function validate(Rut $rut): void;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
<?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\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut\Rut;
|
||||
use MNC\ChileanRut\Util\Correlative;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Util\CorrelativeUtils;
|
||||
|
||||
/**
|
||||
* Validates the Rut using the Module 11 algorithm.
|
||||
@@ -15,13 +23,15 @@ class SimpleRutValidator implements RutValidator
|
||||
{
|
||||
/**
|
||||
* @param Rut $rut
|
||||
*
|
||||
* @throws InvalidRutException
|
||||
*/
|
||||
public function validate(Rut $rut): void
|
||||
{
|
||||
$digit = Correlative::findVerifierDigit($rut->getCorrelative());
|
||||
$digit = CorrelativeUtils::findVerifierDigit($rut->getCorrelative());
|
||||
|
||||
if ($digit !== $rut->getVerifierDigit()) {
|
||||
throw new InvalidRutException($rut);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user