Version 2.0.0
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user