-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.class.php
More file actions
108 lines (89 loc) · 2.82 KB
/
Validate.class.php
File metadata and controls
108 lines (89 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Helper.Validate
*
* @category codeHive Extras
* @package Helper
* @author Tamer Zorba <tamer.zorba@purecis.com>
* @copyright Copyright (c) 2013 - 2016, Purecis, Inc.
* @license http://package.hive.live/Helper.Validate/license MIT License
* @link http://package.hive.live/Helper.Validate
* @version Release: 3.0
*/
namespace App\Helper;
use \App\System\Module;
use App\Helper\Validate\Controller\ValidateController as VC;
class Validate extends Module{
private $errors = [];
private static $messages = [];
public function __invoke()
{
$args = func_get_args();
if(sizeof($args) < 2){
return;
}
$request = $args[0];
$checker = $args[1];
call_user_func_array([$this, 'check'], $args);
return $this;
}
public function check()
{
$args = func_get_args();
$request = is_object($args[0]) ? get_object_vars($args[0]) : $args[0];
$checkable = $args[1];
$errors = [];
// update custom messages
if(isset($args[2])){
self::$messages = $args[2];
}
foreach($checkable as $input => $cases){
$cases = explode('|', $cases);
if(sizeof($cases) == 0){
continue;
}
foreach($cases as $case){
$arguments = explode(':', $case);
$type = array_shift($arguments);
$validate = VC::is(isset($request[$input]) ? $request[$input] : null, $type, $arguments, $input);
// allow empty type
if($type == 'empty'){
if($validate->status){
break;
}else{
continue;
}
}
if(!$validate->status){
// check for input if exists or initialize it
if(!isset($errors[$input])){
$errors[$input] = [];
}
// check for custom message
$key = $input . '.' . $type;
$message = isset(self::$messages[$key]) ? self::$messages[$key] : $validate->error;
// adding errors to input object
array_push($errors[$input], $message);
}
}
}
$this->errors = $errors;
return $this;
}
public function messages()
{
self::$messages = array_merge(self::$messages, func_get_arg(0));
return $this;
}
public function orFail()
{
if(sizeof($this->errors)){
$this->response($this->errors)->code(422)->kill();
}
return $this;
}
public function errors()
{
return $this->errors;
}
}