Skip to content

Commit 5dde595

Browse files
author
Sandu Luca
committed
Initial commit
0 parents  commit 5dde595

File tree

6 files changed

+322
-0
lines changed

6 files changed

+322
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Validation
2+
3+
Standalone library to use Illuminate\\Validation package outside the Laravel framework.
4+
5+
6+
## Install Validation:
7+
- execute
8+
```bash
9+
composer require ggbear/validation
10+
```
11+
## Usage
12+
```php
13+
14+
require_once __DIR__ . '\..\vendor\autoload.php';
15+
/** @var Factory $validatorFactory */
16+
$validatorFactory = new ValidatorFactory();
17+
18+
$number = '123456789012a';
19+
$id = 123;
20+
$string = 'string value';
21+
$channel = 'UNKNOWN';
22+
23+
$input = [
24+
'number' => $number,
25+
'id' => $id,
26+
'string' => $string,
27+
'channel' => $channel
28+
];
29+
$rules = [
30+
'number' => 'required|integer|digits_between:11,12',
31+
'id' => 'required|integer',
32+
'string' => 'required|string',
33+
'channel' => 'required|string|in:WEB,API,SOCKET',
34+
];
35+
$validator = $validatorFactory->make($input, $rules);
36+
```
37+
This will return an instance of `Illuminate\Validation\Validator::class`.
38+
39+
<br>
40+
<br>
41+
42+
```php
43+
if ($validator->fails()) {
44+
print_r($validator->errors()->toArray());
45+
} else {
46+
echo 'Validaton passed.' . PHP_EOL;
47+
}
48+
```
49+
You can learn more about the *Laravel Validator* in the [official documentation website](https://laravel.com/docs/7.x/validation).
50+
51+
### About validation rules
52+
- https://laravel.com/docs/7.x/validation#available-validation-rules
53+
54+
###

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "ggbear/validation",
3+
"description": "Standalone library to use Illuminate\\Validation package outside the Laravel framework.",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Sandu Luca",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"illuminate/validation": "^7.21"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"GGBear\\Validation\\": "src/"
17+
}
18+
}
19+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Validation Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines contain the default error messages used by
11+
| the validator class. Some of these rules have multiple versions such
12+
| as the size rules. Feel free to tweak each of these messages here.
13+
|
14+
*/
15+
16+
'accepted' => 'The :attribute must be accepted.',
17+
'active_url' => 'The :attribute is not a valid URL.',
18+
'after' => 'The :attribute must be a date after :date.',
19+
'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
20+
'alpha' => 'The :attribute may only contain letters.',
21+
'alpha_dash' => 'The :attribute may only contain letters, numbers, dashes and underscores.',
22+
'alpha_num' => 'The :attribute may only contain letters and numbers.',
23+
'array' => 'The :attribute must be an array.',
24+
'before' => 'The :attribute must be a date before :date.',
25+
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
26+
'between' => [
27+
'numeric' => 'The :attribute must be between :min and :max.',
28+
'file' => 'The :attribute must be between :min and :max kilobytes.',
29+
'string' => 'The :attribute must be between :min and :max characters.',
30+
'array' => 'The :attribute must have between :min and :max items.',
31+
],
32+
'boolean' => 'The :attribute field must be true or false.',
33+
'confirmed' => 'The :attribute confirmation does not match.',
34+
'date' => 'The :attribute is not a valid date.',
35+
'date_equals' => 'The :attribute must be a date equal to :date.',
36+
'date_format' => 'The :attribute does not match the format :format.',
37+
'different' => 'The :attribute and :other must be different.',
38+
'digits' => 'The :attribute must be :digits digits.',
39+
'digits_between' => 'The :attribute must be between :min and :max digits.',
40+
'dimensions' => 'The :attribute has invalid image dimensions.',
41+
'distinct' => 'The :attribute field has a duplicate value.',
42+
'email' => 'The :attribute must be a valid email address.',
43+
'ends_with' => 'The :attribute must end with one of the following: :values',
44+
'exists' => 'The selected :attribute is invalid.',
45+
'file' => 'The :attribute must be a file.',
46+
'filled' => 'The :attribute field must have a value.',
47+
'gt' => [
48+
'numeric' => 'The :attribute must be greater than :value.',
49+
'file' => 'The :attribute must be greater than :value kilobytes.',
50+
'string' => 'The :attribute must be greater than :value characters.',
51+
'array' => 'The :attribute must have more than :value items.',
52+
],
53+
'gte' => [
54+
'numeric' => 'The :attribute must be greater than or equal :value.',
55+
'file' => 'The :attribute must be greater than or equal :value kilobytes.',
56+
'string' => 'The :attribute must be greater than or equal :value characters.',
57+
'array' => 'The :attribute must have :value items or more.',
58+
],
59+
'image' => 'The :attribute must be an image.',
60+
'in' => 'The selected :attribute is invalid.',
61+
'in_array' => 'The :attribute field does not exist in :other.',
62+
'integer' => 'The :attribute must be an integer.',
63+
'ip' => 'The :attribute must be a valid IP address.',
64+
'ipv4' => 'The :attribute must be a valid IPv4 address.',
65+
'ipv6' => 'The :attribute must be a valid IPv6 address.',
66+
'json' => 'The :attribute must be a valid JSON string.',
67+
'lt' => [
68+
'numeric' => 'The :attribute must be less than :value.',
69+
'file' => 'The :attribute must be less than :value kilobytes.',
70+
'string' => 'The :attribute must be less than :value characters.',
71+
'array' => 'The :attribute must have less than :value items.',
72+
],
73+
'lte' => [
74+
'numeric' => 'The :attribute must be less than or equal :value.',
75+
'file' => 'The :attribute must be less than or equal :value kilobytes.',
76+
'string' => 'The :attribute must be less than or equal :value characters.',
77+
'array' => 'The :attribute must not have more than :value items.',
78+
],
79+
'max' => [
80+
'numeric' => 'The :attribute may not be greater than :max.',
81+
'file' => 'The :attribute may not be greater than :max kilobytes.',
82+
'string' => 'The :attribute may not be greater than :max characters.',
83+
'array' => 'The :attribute may not have more than :max items.',
84+
],
85+
'mimes' => 'The :attribute must be a file of type: :values.',
86+
'mimetypes' => 'The :attribute must be a file of type: :values.',
87+
'min' => [
88+
'numeric' => 'The :attribute must be at least :min.',
89+
'file' => 'The :attribute must be at least :min kilobytes.',
90+
'string' => 'The :attribute must be at least :min characters.',
91+
'array' => 'The :attribute must have at least :min items.',
92+
],
93+
'not_in' => 'The selected :attribute is invalid.',
94+
'not_regex' => 'The :attribute format is invalid.',
95+
'numeric' => 'The :attribute must be a number.',
96+
'password' => 'The password is incorrect.',
97+
'present' => 'The :attribute field must be present.',
98+
'regex' => 'The :attribute format is invalid.',
99+
'required' => 'The :attribute field is required.',
100+
'required_if' => 'The :attribute field is required when :other is :value.',
101+
'required_unless' => 'The :attribute field is required unless :other is in :values.',
102+
'required_with' => 'The :attribute field is required when :values is present.',
103+
'required_with_all' => 'The :attribute field is required when :values are present.',
104+
'required_without' => 'The :attribute field is required when :values is not present.',
105+
'required_without_all' => 'The :attribute field is required when none of :values are present.',
106+
'same' => 'The :attribute and :other must match.',
107+
'size' => [
108+
'numeric' => 'The :attribute must be :size.',
109+
'file' => 'The :attribute must be :size kilobytes.',
110+
'string' => 'The :attribute must be :size characters.',
111+
'array' => 'The :attribute must contain :size items.',
112+
],
113+
'starts_with' => 'The :attribute must start with one of the following: :values',
114+
'string' => 'The :attribute must be a string.',
115+
'timezone' => 'The :attribute must be a valid zone.',
116+
'unique' => 'The :attribute has already been taken.',
117+
'uploaded' => 'The :attribute failed to upload.',
118+
'url' => 'The :attribute format is invalid.',
119+
'uuid' => 'The :attribute must be a valid UUID.',
120+
121+
/*
122+
|--------------------------------------------------------------------------
123+
| Custom Validation Language Lines
124+
|--------------------------------------------------------------------------
125+
|
126+
| Here you may specify custom validation messages for attributes using the
127+
| convention "attribute.rule" to name the lines. This makes it quick to
128+
| specify a specific custom language line for a given attribute rule.
129+
|
130+
*/
131+
132+
'custom' => [
133+
'attribute-name' => [
134+
'rule-name' => 'custom-message',
135+
],
136+
],
137+
138+
/*
139+
|--------------------------------------------------------------------------
140+
| Custom Validation Attributes
141+
|--------------------------------------------------------------------------
142+
|
143+
| The following language lines are used to swap our attribute placeholder
144+
| with something more reader friendly such as "E-Mail Address" instead
145+
| of "email". This simply helps us make our message more expressive.
146+
|
147+
*/
148+
149+
'attributes' => [],
150+
151+
];

src/ValidatorFactory.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace GGBear\Validation;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use Illuminate\Translation\FileLoader;
7+
use Illuminate\Translation\Translator;
8+
use Illuminate\Validation\Factory;
9+
10+
class ValidatorFactory
11+
{
12+
public $dirLang;
13+
public $filenameMessages;
14+
/**
15+
* @var Factory
16+
*/
17+
public $factory;
18+
public $dirErrorMessages;
19+
// Translations root directory
20+
public $basePath;
21+
public static $translator;
22+
23+
public function __construct($dirLang = 'en', $dirErrorMessages = 'ErrorMessages', $filenameMessages = 'validation')
24+
{
25+
$this->dirLang = $dirLang;
26+
$this->filenameMessages = $filenameMessages;
27+
$this->dirErrorMessages = $dirErrorMessages;
28+
$this->basePath = $this->getTranslationsRootPath();
29+
$this->factory = new Factory($this->loadTranslator());
30+
}
31+
32+
public function translationsRootPath(string $path = '')
33+
{
34+
if (!empty($path)) {
35+
$this->basePath = $path;
36+
$this->reloadValidatorFactory();
37+
}
38+
return $this;
39+
}
40+
41+
private function reloadValidatorFactory()
42+
{
43+
$this->factory = new Factory($this->loadTranslator());
44+
return $this;
45+
}
46+
47+
public function getTranslationsRootPath(): string
48+
{
49+
return dirname(__FILE__) . '/';
50+
}
51+
52+
public function loadTranslator(): Translator
53+
{
54+
$loader = new FileLoader(new Filesystem(), $this->basePath . $this->dirErrorMessages);
55+
$loader->addNamespace($this->dirErrorMessages, $this->basePath . $this->dirErrorMessages);
56+
$loader->load($this->dirLang, $this->filenameMessages, $this->dirErrorMessages);
57+
return static::$translator = new Translator($loader, $this->dirLang);
58+
}
59+
60+
public function __call($method, $args)
61+
{
62+
return $this->factory->$method(...$args);
63+
}
64+
}

test/test.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use GGBear\Validation\ValidatorFactory;
4+
use Illuminate\Validation\Factory;
5+
6+
require_once __DIR__ . '\..\vendor\autoload.php';
7+
8+
9+
10+
/** @var Factory */
11+
$validatorFactory = new ValidatorFactory();
12+
13+
$msisdn = '123456789012a';
14+
$serviceId = 123;
15+
$channel = null;
16+
$input = [
17+
'msisdn' => $msisdn,
18+
'service_id' => $serviceId,
19+
'channel' => $channel
20+
];
21+
$rules = [
22+
'msisdn' => 'required|integer|digits_between:11,12',
23+
'service_id' => 'required|integer',
24+
'channel' => 'required|string',
25+
];
26+
27+
$validator = $validatorFactory->make($input, $rules);
28+
if ($validator->fails()) {
29+
print_r($validator->errors()->toArray());
30+
} else {
31+
echo 'Validaton passed.' . PHP_EOL;
32+
}

0 commit comments

Comments
 (0)