Witam, oto moja pierwsza klasa ktora zrobilem dla mojego malego projektu postanowilem go udostepnic aby dowiedziec sie co mam tutaj poprawic, oraz czekam na jakies rady, w OOP siedze od kolo 4/5 tygodni. Dzieki.
ValidationInput.php
<?php
/**
* By Artur Kupczak, This class can be used in every project, please don't remove this line.
*/
class ValidationInput{
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Looping inside of parameters given in the form, and then passing data to class validateRequest
//
// @param array $validation Value passed from requested validation
public function validateInput
($validation = array()){ foreach($validation as $field => $requests){
foreach($requests as $validationName => $validationInfo) {
$this->validateRequest($validationName,$validationInfo,$field);
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Looping inside of parameters given in the form
//
// @param string $validationName
// @param string $validationInfo
// @param string $field
//
// Case possible options:
// require booleon - If booleon true has been given then field is required otherwise the error will be displayed
// preg_match String (Choice between)-
// Alphabetic - Allow Numbers and Letters
// Numeric - Allow Numbers Only
// Letters - Allow letters Only
//
// minLength Numbers - The minimum length of the field
// maxLength Numbers - The maximum length of the field
// matchWith String - String given is a name of field that the field need to compare, For example password/passwordRepeat
// filter_validate String (Choice between)-
// Email - Compare field with email and return error if it's not a email
//
//
private function validateRequest($validationName, $validationInfo, $field)
{
switch ($validationName) {
case 'require':
$validationInfo == true && empty(Input
::get($field)) ? ErrorHandler::getInstance()->addError("Field " . $field . " can't be empty")
: false;
break;
case 'preg_match':
switch($validationInfo){
case 'alphabetic':
!empty(Input
::get($field)) && preg_match("/[^A-z0-9]/", Input
::get($field)) == 1 ? ErrorHandler::getInstance()->addError("Field " . $field . " contains prohibited characters")
: false;
break;
case 'numeric':
!empty(Input
::get($field)) && preg_match("/[^0-9]/", Input
::get($field)) == 1 ? ErrorHandler::getInstance()->addError("Field " . $field . " can contains numbers only")
: false;
break;
case 'letters':
!empty(Input
::get($field)) && preg_match("/[^A-z]/", Input
::get($field)) == 1 ? ErrorHandler::getInstance()->addError("Field " . $field . " can contains letters only")
: false;
break;
}
break;
case 'minLength':
!empty(Input
::get($field)) && strlen(Input
::get($field)) < $validationInfo && strlen(Input
::get($field)) >= 1 ? ErrorHandler::getInstance()->addError("" . $field . " field need to be minimum " . $validationInfo . " characters")
: false;
break;
case 'maxLength':
!empty(Input
::get($field)) && strlen(Input
::get($field)) > $validationInfo ? ErrorHandler::getInstance()->addError("This field is longer then " . $validationInfo . " characters")
: false;
break;
case "matchWith":
Input::get($field) != Input::get($validationInfo)
? ErrorHandler::getInstance()->addError($field . " field doesn't match " . $validationInfo . " field")
: false;
break;
case "filter_validate";
switch ($validationInfo) {
case "email":
!empty(Input
::get($field)) && !filter_var
(Input
::get($field), FILTER_VALIDATE_EMAIL
) ? ErrorHandler::getInstance()->addError("This is not a valid E-mail")
: false;
break;
}
break;
}
}
public static function getInstance
() {
if(self::$instance === null)
{
self::$instance = new self();
}
return self::$instance;
}
}
Input.php (Nie jest wymagany):
class Input
{
public static function get
($item) {
if (isset($_POST[$item])) { return escape($_POST[$item]);
}
if (isset($_GET[$item])) { return escape($_GET[$item]);
}
return '';
}
}
ErrorHandeler.php
class ErrorHandler
{
public static $errorBooleon = false;
public function addError($newError)
{
self::$errorBooleon = true;
self::$errors[] = $newError;
}
public static function checkIfErrors
() {
return self::$errorBooleon;
}
public static function returnErrors
() {
return self::$errors;
}
public static function getInstance
() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
}
Przykladowe uzycie :
if(Input::get("register") ){
ValidationInput::getInstance()->validateInput(
"username" => array( //username to name inputu 'require' => true,
'minLength' => 3,
'maxLength' => 15
)
)
)
if(ErrorHandler::checkIfErrors() == false ){
}
if (ErrorHandler::checkIfErrors()) {
foreach (ErrorHandler::returnErrors() as $error) {
ErrorHandler::$errorBooleon = false;
}
}
<input type="text" name="username"/> <input type="submit" name="register"/>