2
2
3
3
namespace Validvid \Validation ;
4
4
5
- use Illuminate \Container \Container ;
5
+ use Illuminate \Validation ;
6
+ use Illuminate \Translation ;
7
+ use Illuminate \Validation \Factory ;
6
8
use Illuminate \Filesystem \Filesystem ;
7
9
use Illuminate \Translation \FileLoader ;
8
10
use Illuminate \Translation \Translator ;
9
- use Illuminate \Validation \Factory ;
10
11
11
12
class Validator
12
13
{
13
- public function make (array $ data , array $ rules )
14
- {
15
- try {
16
- $ loader = new FileLoader (new Filesystem , 'lang ' );
17
- $ translator = new Translator ($ loader , 'en ' );
18
- $ validation = new Factory ($ translator , new Container );
19
-
20
- return $ validation ->make ($ data , $ rules );
21
- } catch (\Exception $ e ) {
22
- return $ e ->getMessage ();
23
- }
24
- }
14
+ public $ lang ;
15
+ public $ group ;
16
+ public $ factory ;
17
+ public $ namespace ;
18
+ public $ basePath ;
19
+ public static $ translator ;
20
+
21
+ public function __construct ($ namespace = 'lang ' , $ lang = 'en ' , $ group = 'validation ' )
22
+ {
23
+ $ this ->lang = $ lang ;
24
+ $ this ->group = $ group ;
25
+ $ this ->namespace = $ namespace ;
26
+ $ this ->basePath = $ this ->getTranslationsRootPath ();
27
+ $ this ->factory = new Factory ($ this ->loadTranslator ());
28
+ }
29
+
30
+ public function translationsRootPath (string $ path = '' )
31
+ {
32
+ if (!empty ($ path )) {
33
+ $ this ->basePath = $ path ;
34
+ $ this ->reloadValidatorFactory ();
35
+ }
36
+ return $ this ;
37
+ }
38
+
39
+ private function reloadValidatorFactory ()
40
+ {
41
+ $ this ->factory = new Factory ($ this ->loadTranslator ());
42
+ return $ this ;
43
+ }
44
+
45
+ public function getTranslationsRootPath () : string
46
+ {
47
+ return dirname (__FILE__ ) . '/ ' ;
48
+ }
49
+
50
+ public function loadTranslator () : Translator
51
+ {
52
+ $ loader = new FileLoader (new Filesystem (), $ this ->basePath . $ this ->namespace );
53
+ $ loader ->addNamespace ($ this ->namespace , $ this ->basePath . $ this ->namespace );
54
+ $ loader ->load ($ this ->lang , $ this ->group , $ this ->namespace );
55
+ return static ::$ translator = new Translator ($ loader , $ this ->lang );
56
+ }
57
+
58
+ public function __call ($ method , $ args )
59
+ {
60
+ return call_user_func_array ([$ this ->factory , $ method ], $ args );
61
+ }
25
62
}
0 commit comments