-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanCodeModel.php
executable file
·150 lines (127 loc) · 3.67 KB
/
CleanCodeModel.php
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require_once 'CleanCodeClass.php';
class CleanCodeModel extends CleanCodeClass
{
const ALL = '/^[\w\W]{min,max}$/i';
const HTML = '/[<>\w]{min,max}/';
const NAME = '/^[a-záàâãçéèêëíìóôõöúüÁÀÂÃÇÉÈÊËÍÌÓÒÔÕÖÚÙÜ ]{min,max}$/i';
const LOGIN = '/^[a-z0-9\-_\.]{min,max}$/';
const URI = '/^[a-z0-9\-\/_]{min,max}$/i';
const URL = '/^(http|https)+(:\/\/)+(www\.|)+([\w\.\/\?\=\-\_]{min,max})$/';
const EMAIL = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i';
const DATE = '/^\d{4}\-\d{1,2}\-\d{1,2}$/';
const TIME = '/^\d{1,2}\:\d{1,2}/';
const FULLDATE = '/^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}/';
const PASSWORD = '/^[\w\@\$\#]{min,max}$/';
const NUM = '/^\d{min,max}$/';
const DOUBLE = '/^[\d\.]{min,max}$/';
const FILE = '/^([\w_\-\.]{min,max})+(\.\w{2,5})$/';
const CPF = '/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/';
const CNPJ = '/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/';
const TEL_BR = '/^\(\d{2}\) \d{4,5}\-\d{4}$/';
const GENDER = '/^[FM]$/i';
const IPv4 = '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/';
const BOOLEAN = '/^[0-1]$/';
protected $error = '';
protected $success = '';
public static function formatRegex($regex, $min, $max = '')
{
return str_replace('{min,max}', '{'.$min.','.$max.'}', $regex);
}
protected function parseURI($string)
{
$string = htmlentities($string);
$invalid = array('/\s/', '/acute;/', '/cedil;/', '/grave;/', '/circ;/', '/tilde;/', '/[^\-\_\w]/');
return preg_replace($invalid, array('_', ''), strtolower($string));
}
private function formatValueByRegex($value, $regex)
{
switch ($regex)
{
case self::NUM:
return (int) $value;
break;
case self::PASSWORD:
return md5($value);
break;
case self::LOGIN:
case self::EMAIL:
return strtolower($value);
break;
case self::NAME:
return ucwords(strtolower($value));
break;
case self::HTML:
return preg_replace('/^<\?.*\?>$/', '', $value);
break;
default:
return $value;
}
}
protected function formatValue($value, $regex)
{
return $this->formatValueByRegex($regex == self::HTML? $value : strip_tags($value), $regex);
}
protected function validateData($value, $regex, $min = 1, $max = '')
{
return preg_match(self::formatRegex($regex, $min, $max), $value);
}
protected function validateRange($number, $min, $max)
{
return $number >= $min && $number <= $max;
}
public function checkErrors()
{
return $this->error == '';
}
public function getError()
{
return $this->error;
}
public function setError($message)
{
$this->error = $message;
}
protected function setErrorByField($name)
{
$this->setError("Invalid $name!");
}
public function getSuccess()
{
return $this->success;
}
public function setSuccess($message)
{
$this->success = $message;
}
protected function execute($operation, $successMessage, $errorMessage)
{
if($operation)
{
$this->setSuccess($successMessage);
return true;
}
else if($this->checkErrors())
{
$this->setError($errorMessage);
return false;
}
else
{
return false;
}
}
public static function parseFloat($number)
{
return str_replace(array('.', ','), array('', '.'), $number);
}
public static function parseMoney($number)
{
return number_format($number, 2, ',', '.');
}
public static function parseNumber($string)
{
return preg_replace('/\D/', '', $string);
}
}
?>