Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
Create validation factory in our own service provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
franzliedke committed Oct 30, 2014
1 parent f6196f1 commit 10c3945
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/FluxBB/Validator/ValidationServiceProvider.php
Expand Up @@ -2,8 +2,9 @@

namespace FluxBB\Validator;

use Illuminate\Contracts\Validation\Factory;
use Illuminate\Contracts\Validation\Factory as FactoryContract;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Factory;

class ValidationServiceProvider extends ServiceProvider
{
Expand All @@ -14,18 +15,34 @@ class ValidationServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->extend('Illuminate\Contracts\Validation\Factory', function (Factory $factory) {
$this->registerFactory();

$this->registerExtensions();
}

protected function registerFactory()
{
$this->app->singleton('validator', function () {
$translator = $this->app->make('Symfony\Component\Translation\TranslatorInterface');

return new Factory($translator, $this->app);
});
}

protected function registerExtensions()
{
$this->app->extend('Illuminate\Contracts\Validation\Factory', function (FactoryContract $factory) {
$factory->extend('username_not_guest', function ($attribute, $value) {
return strcasecmp($value, 'Guest') && strcasecmp($value, trans('fluxbb::common.guest'));
});

$factory->extend('username_not_reserved', function ($attribute, $value) {
return !str_contains($value, array('[', ']', '\'', '"'));
return !str_contains($value, ['[', ']', '\'', '"']);
});

$factory->extend('no_ip', function ($attribute, $value) {
return !preg_match('%[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}%', $value) &&
!preg_match('%((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([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1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))%', $value);
});

return $factory;
Expand Down

0 comments on commit 10c3945

Please sign in to comment.