From 4cfd01b7a15c6ff543af42706dac844be738ae27 Mon Sep 17 00:00:00 2001 From: Luca Alcalde Date: Wed, 18 Nov 2020 18:54:59 -0300 Subject: [PATCH] Faker --- composer.json | 15 +-- src/Laravel/ChileanBundleServiceProvider.php | 106 +++++++++++-------- 2 files changed, 68 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index b4f896d..43e751a 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "freshwork/chilean-bundle", - "description": "A PHP composer package with Chilean validations, common variables, etc. (RUT, IVA, ETC). Ready for Laravel 5. Grande chile ctm :)", + "name": "pittacusw/chilean-bundle", + "description": "A PHP composer package with Chilean validations, common variables, etc. (RUT, IVA, ETC). Ready for Laravel 8.", "authors": [ { - "name": "Gonzalo De Spirito | Freshwork Studio Chile", + "name": "Freshwork Studio Chile", "email": "gonzalo@freshworkstudio.com" } ], @@ -17,7 +17,8 @@ ], "license": "MIT", "require": { - "php": ">=5.4.0" + "php": ">=7.3.0", + "laravel/framework": ">=8.0" }, "require-dev": { "codeception/codeception": "^2.1", @@ -25,16 +26,16 @@ }, "autoload": { "psr-4": { - "Freshwork\\ChileanBundle\\": "src/" + "PittacusW\\ChileanBundle\\": "src/" } }, "extra": { "laravel": { "providers": [ - "Freshwork\\ChileanBundle\\Laravel\\ChileanBundleServiceProvider" + "PittacusW\\ChileanBundle\\Laravel\\ChileanBundleServiceProvider" ], "aliases": { - "Rut": "Freshwork\\ChileanBundle\\Facades\\Rut" + "Rut": "PittacusW\\ChileanBundle\\Facades\\Rut" } } } diff --git a/src/Laravel/ChileanBundleServiceProvider.php b/src/Laravel/ChileanBundleServiceProvider.php index ff36bde..89b371a 100644 --- a/src/Laravel/ChileanBundleServiceProvider.php +++ b/src/Laravel/ChileanBundleServiceProvider.php @@ -1,53 +1,67 @@ addReplacer('cl_rut', function ($message, $attribute, $rule, $parameters) { - return str_replace(':attribute', $attribute, $message == 'validation.cl_rut' - ? 'El atributo :attribute no es válido.' - : $message); - }); - - return Rut::parse($value)->quiet()->validate(); - }); - - app()->bind('rut', function () { - return new Rut; - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array(); - } +class ChileanBundleServiceProvider extends ServiceProvider { + + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = FALSE; + + /** + * Register the service provider. + * + * @return void + */ + public function register() { + $this->app->singleton('Faker', function($app) { + $faker = Factory::create(); + $newClass = new class($faker) extends Base { + public function rut() { + return Rut::set(rand(1000000, 25000000)) + ->fix() + ->format(Rut::FORMAT_ESCAPED); + } + }; + + $faker->addProvider($newClass); + + return $faker; + }); + } + + public function boot() { + Validator::extend('cl_rut', function($attribute, $value, $parameters, $validator) { + $validator->addReplacer('cl_rut', function($message, $attribute, $rule, $parameters) { + return str_replace(':attribute', $attribute, $message == 'validation.cl_rut' + ? 'El atributo :attribute no es válido.' + : $message); + }); + + return Rut::parse($value) + ->quiet() + ->validate(); + }); + + app()->bind('rut', function() { + return new Rut; + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() { + return []; + } }