Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faker #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
Expand All @@ -17,24 +17,25 @@
],
"license": "MIT",
"require": {
"php": ">=5.4.0"
"php": ">=7.3.0",
"laravel/framework": ">=8.0"
},
"require-dev": {
"codeception/codeception": "^2.1",
"codeception/specify": "^0.4.3"
},
"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"
}
}
}
Expand Down
106 changes: 60 additions & 46 deletions src/Laravel/ChileanBundleServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,67 @@
<?php namespace Freshwork\ChileanBundle\Laravel;

use App;
use Validator;
use Faker\Factory;
use Faker\Provider\Base;
use Freshwork\ChileanBundle\Rut;
use Illuminate\Support\ServiceProvider;
use Validator;

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()
{
}

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 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 [];
}
}