39 lines
1.0 KiB
PHP
39 lines
1.0 KiB
PHP
<?php
|
|
namespace Tests\Extension\Faker\Provider;
|
|
|
|
use Faker\Provider\Base;
|
|
|
|
class Rut extends Base
|
|
{
|
|
public function rut(bool $withDigito = true, bool $withDotsAndSlash = true): string
|
|
{
|
|
$base = self::numberBetween(1000000, 99999999);
|
|
$rut = $base;
|
|
if ($withDotsAndSlash) {
|
|
$rut = number_format($rut, 0, ',', '.');
|
|
}
|
|
if ($withDigito) {
|
|
$digito = $this->getDigito($base);
|
|
if ($withDotsAndSlash) {
|
|
return "{$digito}-{$rut}";
|
|
}
|
|
return "{$digito}{$rut}";
|
|
}
|
|
return $rut;
|
|
}
|
|
public function digitoVerificador(string $rut): bool|string
|
|
{
|
|
if ( !preg_match("/^[0-9.]+/",$rut)) return false;
|
|
$rut = str_replace('.','',$rut);
|
|
return $this->getDigito($rut);
|
|
}
|
|
|
|
protected function getDigito(string $rut): string
|
|
{
|
|
$M=0;$S=1;
|
|
for(;$rut;$rut=floor($rut/10))
|
|
$S=($S+$rut%10*(9-$M++%6))%11;
|
|
return $S?$S-1:'K';
|
|
}
|
|
}
|