You've already forked symfony-validator
test
This commit is contained in:
2
ValidatorBundle/.gitignore
vendored
Normal file
2
ValidatorBundle/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
vendor
|
||||
.idea
|
||||
24
ValidatorBundle/DependencyInjection/Configuration.php
Normal file
24
ValidatorBundle/DependencyInjection/Configuration.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\ValidatorBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class Configuration implements ConfigurationInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Generates the configuration tree builder.
|
||||
*
|
||||
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$treeBuilder->root('validator');
|
||||
return ($treeBuilder);
|
||||
}
|
||||
}
|
||||
34
ValidatorBundle/DependencyInjection/ValidatorExtension.php
Normal file
34
ValidatorBundle/DependencyInjection/ValidatorExtension.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\ValidatorBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Extension\Extension;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class ValidatorExtension extends Extension
|
||||
{
|
||||
|
||||
/**
|
||||
* Loads a specific configuration.
|
||||
*
|
||||
* @throws \InvalidArgumentException When provided tag is not defined in this extension
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Ressources/config'));
|
||||
$loader->load('services.yaml');
|
||||
$configuration = $this->getConfiguration($configs, $container);
|
||||
$config = $this->processConfiguration($configuration, $configs);
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
public function getAlias()
|
||||
{
|
||||
return parent::getAlias(); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
21
ValidatorBundle/LICENSE
Normal file
21
ValidatorBundle/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
DON'T BE A DICK PUBLIC LICENSE
|
||||
|
||||
Version 1, December 2009
|
||||
|
||||
Copyright (C) 2009 Philip Sturgeon email@philsturgeon.co.uk
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
|
||||
|
||||
DON'T BE A DICK PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
Do whatever you like with the original work, just don't be a dick.
|
||||
|
||||
Being a dick includes - but is not limited to - the following instances:
|
||||
|
||||
1a. Outright copyright infringement - Don't just copy this and change the name.
|
||||
1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
|
||||
1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
|
||||
|
||||
If you become rich through modifications, related works/services, or supporting the original work, share the love. Only a dick would make loads off this work and not buy the original works creator(s) a pint.
|
||||
|
||||
Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.
|
||||
7
ValidatorBundle/Ressources/config/services.yaml
Normal file
7
ValidatorBundle/Ressources/config/services.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
services:
|
||||
bluesquare.validator:
|
||||
class: Bluesquare\ValidatorBundle\Services\Validator
|
||||
autowire: true
|
||||
public: true
|
||||
arguments: ['@request_stack', '@translator.data_collector']
|
||||
Bluesquare\ValidatorBundle\Services\Validator: '@bluesquare.validator'
|
||||
441
ValidatorBundle/Services/Validator.php
Normal file
441
ValidatorBundle/Services/Validator.php
Normal file
@@ -0,0 +1,441 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\ValidatorBundle\Services;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class Validator
|
||||
{
|
||||
protected $request;
|
||||
protected $context;
|
||||
protected $errors;
|
||||
protected $entity;
|
||||
protected $values;
|
||||
protected $rules;
|
||||
protected $validated;
|
||||
protected $translator;
|
||||
|
||||
public function __construct(RequestStack $requestStack, TranslatorInterface $translator)
|
||||
{
|
||||
$this->values = [];
|
||||
$this->errors = [];
|
||||
$this->rules = [];
|
||||
$this->validated = false;
|
||||
|
||||
$this->translator = $translator;
|
||||
$this->request = $requestStack->getCurrentRequest();
|
||||
|
||||
$session = $this->request->getSession()->getFlashBag()->get("Bluesquare:ValidatorBundle");
|
||||
|
||||
if(is_array($session) && isset($session[0])) {
|
||||
if(isset($session[0]['context'])) $this->context = $session[0]['context'];
|
||||
if(isset($session[0]['errors']) && is_array($session[0]['errors'])) $this->errors = $session[0]['errors'];
|
||||
if(isset($session[0]['values']) && is_array($session[0]['values'])) $this->values = $session[0]['values'];
|
||||
$this->validated = true;
|
||||
}
|
||||
|
||||
if ($this->post())
|
||||
{
|
||||
$values = [];
|
||||
foreach (array_merge($_GET, $_POST) as $field => $value) $values[$field] = $value;
|
||||
$json = @json_decode(file_get_contents('php://input'), true);
|
||||
if (is_array($json)) $values = array_merge($values, $json);
|
||||
$this->values = array_merge($this->values, $values);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public function context($context)
|
||||
{
|
||||
$this->context = $context;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function entity($entity)
|
||||
{
|
||||
$this->entity = $entity;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->values[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function error($field, $error)
|
||||
{
|
||||
if (!isset($this->errors[$field]))
|
||||
$this->errors[$field] = [];
|
||||
|
||||
$this->errors[$field][] = $error;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public function json($code = 400, $data = [])
|
||||
{
|
||||
$data = array_merge([
|
||||
'message' => 'Please check your input',
|
||||
'error' => 'validator.form-error',
|
||||
'errors' => array_map(function ($errors) {
|
||||
return "validator.".$errors[0].(!is_null($this->context) ? ':'.$this->context : '');
|
||||
}, $this->errors)
|
||||
], $data);
|
||||
|
||||
return new JsonResponse($data, $code);
|
||||
}
|
||||
|
||||
public function keep()
|
||||
{
|
||||
$data = [
|
||||
'errors' => $this->errors,
|
||||
'values' => $this->values,
|
||||
'context' => $this->context
|
||||
];
|
||||
$this->request->getSession()->getFlashBag()->add('Bluesquare:ValidatorBundle', $data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function validated()
|
||||
{
|
||||
return $this->validated;
|
||||
}
|
||||
|
||||
public function failed()
|
||||
{
|
||||
return (count($this->errors) > 0);
|
||||
}
|
||||
|
||||
public function errors()
|
||||
{
|
||||
$translator = $this->translator;
|
||||
$context = $this->context;
|
||||
|
||||
return array_map(function ($errors) use ($translator, $context) {
|
||||
$error = $errors[0];
|
||||
$message = null;
|
||||
if (!is_null($context)) {
|
||||
$message = $translator->trans("$context.$error", [], "validator");
|
||||
}
|
||||
if (is_null($message) || $message == "$context.$error") {
|
||||
$message = $translator->trans("$error", [], "validator");
|
||||
}
|
||||
return $message;
|
||||
}, $this->errors);
|
||||
}
|
||||
|
||||
public function message()
|
||||
{
|
||||
$result = $this->failed() ? 'form-error' : 'form-success';
|
||||
$message = null;
|
||||
if (!is_null($this->context)) {
|
||||
$message = $this->translator->trans("$this->context.$result", [], "validator");
|
||||
}
|
||||
if (is_null($message) || $message == "$this->context.$result") {
|
||||
$message = $this->translator->trans("$result", [], "validator");
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function has($field)
|
||||
{
|
||||
return !is_null($this->get($field));
|
||||
}
|
||||
|
||||
public function value($field, $default = null)
|
||||
{
|
||||
$value = isset($this->values[$field]) ? trim($this->values[$field]) : $default;
|
||||
if (empty($value)) $value = $default;
|
||||
|
||||
if (is_null($value) && !is_null($this->entity)) {
|
||||
$method = "get".$this->camelize($field);
|
||||
if (method_exists($this->entity, $method))
|
||||
$value = $this->entity->$method();
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function get($field, $default = null)
|
||||
{
|
||||
$value = isset($this->values[$field]) ? trim($this->values[$field]) : $default;
|
||||
if (empty($value)) $value = $default;
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function checked($field)
|
||||
{
|
||||
return !is_null($this->get($field)) || $this->get($field) != '0' || $this->get($field) != 0;
|
||||
}
|
||||
|
||||
public function getFile($name)
|
||||
{
|
||||
return $this->request->files->get($name);
|
||||
}
|
||||
|
||||
public function hasFile($name)
|
||||
{
|
||||
$file = $this->getFile($name);
|
||||
return !is_null($file) && !(is_array($file) || $file instanceof Traversable);
|
||||
}
|
||||
|
||||
public function hasFiles($name)
|
||||
{
|
||||
$files = $this->getFile($name);
|
||||
return !is_null($files) && (is_array($files) || $files instanceof Traversable);
|
||||
}
|
||||
|
||||
public function inject()
|
||||
{
|
||||
$args = func_get_args();
|
||||
if (count($args) === 0) return false;
|
||||
if (is_object($args[0])) $entity = array_shift($args);
|
||||
else $entity = $this->entity;
|
||||
if (is_null($entity)) return false;
|
||||
|
||||
foreach ($args as $field) {
|
||||
$method = "set".$this->camelize($field);
|
||||
if (method_exists($entity, $method)) $entity->$method($this->value($field));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public function post()
|
||||
{
|
||||
return in_array(strtolower($this->request->getMethod()), ['delete', 'put', 'post', 'patch']);
|
||||
}
|
||||
|
||||
public function check()
|
||||
{
|
||||
return $this->validate();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
protected function validate()
|
||||
{
|
||||
foreach ($this->rules as $field => $rules)
|
||||
{
|
||||
foreach ($rules as $rule) $this->test($field, $rule);
|
||||
}
|
||||
|
||||
$this->validated = true;
|
||||
|
||||
return (count($this->errors) == 0);
|
||||
}
|
||||
|
||||
public function test($field, $rule)
|
||||
{
|
||||
$name = $rule['rule'];
|
||||
$data = $rule['data'];
|
||||
$value = $this->get($field);
|
||||
$success = true;
|
||||
|
||||
switch ($name)
|
||||
{
|
||||
case 'required-file':
|
||||
$success = $this->hasFile($field);
|
||||
break;
|
||||
case 'required-files':
|
||||
$success = $this->hasFiles($field);
|
||||
break;
|
||||
case 'required':
|
||||
$success = !(is_null($value));
|
||||
break;
|
||||
case 'integer':
|
||||
$success = (filter_var($value, FILTER_VALIDATE_INT) ? true : false);
|
||||
break;
|
||||
case 'float':
|
||||
$success = (filter_var($value, FILTER_VALIDATE_FLOAT) ? true : false);
|
||||
break;
|
||||
case 'boolean':
|
||||
$success = (filter_var($value, FILTER_VALIDATE_BOOLEAN) ? true : false);
|
||||
break;
|
||||
case 'email':
|
||||
$success = (filter_var($value, FILTER_VALIDATE_EMAIL) ? true : false);
|
||||
break;
|
||||
case 'phone':
|
||||
$_pattern = "/^\+?\d{7,15}$/";
|
||||
$success = (!(strlen($value) == 10 && ctype_digit($value)) && !preg_match($_pattern, $value)) ? false : true;
|
||||
break;
|
||||
case 'zipcode':
|
||||
$success = (!(strlen($value) == 5 && ctype_digit($value))) ? false : true;
|
||||
break;
|
||||
case 'alphanumeric':
|
||||
$success = ctype_alnum($value);
|
||||
break;
|
||||
case 'date':
|
||||
$success = preg_match('/^([0-9]{2}\/[0-9]{2}\/[0-9]{4})|([0-9]{4}-[0-9]{2}-[0-9]{2})$/', $value);
|
||||
break;
|
||||
case 'datetime':
|
||||
$_pattern = "[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}";
|
||||
$success = (preg_matchs($_pattern, $value)) ? true : false;
|
||||
break;
|
||||
case 'url':
|
||||
$_pattern = "%^((?:(?:https?|ftp)://))?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$%iuS";
|
||||
$success = preg_match($_pattern, $value);
|
||||
break;
|
||||
case 'inArray':
|
||||
$success = in_array($value, $data['values']);
|
||||
break;
|
||||
case 'min':
|
||||
$success = min($value, $data['length']) == $value ? true : false;
|
||||
break;
|
||||
case 'max':
|
||||
$success = max($value, $data['length']) == $value ? true : false;
|
||||
break;
|
||||
case 'minLength':
|
||||
$success = strlen($value) < $data['min-length'] ? true : false;
|
||||
break;
|
||||
case 'maxLength':
|
||||
$success = strlen($value) > $data['min-length'] ? true : false;
|
||||
break;
|
||||
// (éwé c'est un switch)
|
||||
}
|
||||
|
||||
if (!$success) {
|
||||
$this->error($field, $name);
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function rule($field, $rule, $data = [])
|
||||
{
|
||||
if (!isset($this->rules[$field]))
|
||||
$this->rules[$field] = [];
|
||||
|
||||
$this->rules[$field][] = ['rule' => $rule, 'data' => $data];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Rules: multiple
|
||||
|
||||
public function required()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'required');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function integer()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'integer');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function float()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'float');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function boolean()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'boolean');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function email()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'email');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function phone()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'phone');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function zipcode()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'zipcode');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function alphanumeric()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'alphanumeric');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function date()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'date');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function datetime()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'datetime');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function url()
|
||||
{
|
||||
foreach (func_get_args() as $field) $this->rule($field, 'url');
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Rules: single
|
||||
|
||||
public function requiredFile($name)
|
||||
{
|
||||
$this->rule($name, 'required-file');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function requiredFiles($name)
|
||||
{
|
||||
$this->rule($name, 'required-files');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function min($field, $length)
|
||||
{
|
||||
$this->rule($field, 'min', ['length' => $length]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function max($field, $length)
|
||||
{
|
||||
$this->rule($field, 'max', ['length' => $length]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function minLength($field, $length)
|
||||
{
|
||||
$this->rule($field, 'min-length', ['length' => $length]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function maxLength($field, $length)
|
||||
{
|
||||
$this->rule($field, 'max-length', ['length' => $length]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function inArray($field, $values)
|
||||
{
|
||||
$this->rule($field, 'inArray', ['values' => $values]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
protected function camelize($string)
|
||||
{
|
||||
$string = implode('_', explode('-', $string));
|
||||
$words = array_map('ucfirst', explode('_', $string));
|
||||
return implode('', $words);
|
||||
}
|
||||
}
|
||||
22
ValidatorBundle/ValidatorBundle.php
Normal file
22
ValidatorBundle/ValidatorBundle.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\ValidatorBundle;
|
||||
|
||||
use Bluesquare\ValidatorBundle\DependencyInjection\ValidatorExtension;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class ValidatorBundle extends Bundle
|
||||
{
|
||||
public function build(ContainerBuilder $container)
|
||||
{
|
||||
parent::build($container);
|
||||
}
|
||||
|
||||
public function getContainerExtension()
|
||||
{
|
||||
if (null === $this->extension)
|
||||
$this->extension = new ValidatorExtension();
|
||||
return $this->extension;
|
||||
}
|
||||
}
|
||||
21
ValidatorBundle/composer.json
Normal file
21
ValidatorBundle/composer.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "bluesquare-packages/symfony-validator",
|
||||
"description": "Validator created by Bluesquare Computing",
|
||||
"keywords": ["template", "composer", "package"],
|
||||
"license": "proprietary",
|
||||
"authors": [
|
||||
{
|
||||
"name": "RENOU Maxime",
|
||||
"email": "maxime@bluesquare.io"
|
||||
}
|
||||
],
|
||||
"type": "symfony-bundle",
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Bluesquare\\": "/"
|
||||
}
|
||||
}
|
||||
}
|
||||
19
ValidatorBundle/composer.lock
generated
Normal file
19
ValidatorBundle/composer.lock
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "692cf76646b7b8c0cf2b57040ee21139",
|
||||
"packages": [],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"platform-dev": []
|
||||
}
|
||||
Reference in New Issue
Block a user