Last active
July 19, 2018 08:09
-
-
Save dusta/5ba30070b6a8444cefa0ff5165b36990 to your computer and use it in GitHub Desktop.
Biblioteka sprawdzająca czy są wymagane posty oraz czy nie są one puste
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Biblioteka sprawdzająca czy są wymagane posty oraz czy nie są one puste | |
* kajtek | |
*/ | |
/** | |
* @author Sławomir Kaleta <[email protected]> | |
*/ | |
class ThatForm | |
{ | |
/** | |
* Undocumented function | |
* Usage: \ThatForm::requiredVariables(array('name', 'description'), $_POST); | |
* | |
* @param array $keys | |
* @param boolean $array | |
* @param [type] $v | |
* @return void | |
*/ | |
public static function requiredVariables($keys = array(), $array = false, $v = null) | |
{ | |
if ($array == false) { | |
$array = $_POST; | |
} | |
$debug = false; | |
$errorReturn = null; | |
if (ini_get('display_errors') == 'on') { | |
$debug = true; | |
} | |
$error = array( | |
'isset' => array(), | |
'empty' => array() | |
); | |
foreach ($keys as $key) { | |
if (!isset($array[$key])) { | |
$error['isset'][] = $key; | |
} | |
if (empty($array[$key])) { | |
$error['empty'][] = $key; | |
} | |
} | |
if ((isset($error['isset']) and !empty($error['isset'])) or (isset($error['empty']) and !empty($error['empty']))) { | |
if ($debug == true) { | |
if (isset($error['isset']) and !empty($error['isset'])) { | |
$errorReturn .= 'Missing:'; | |
foreach ($error['isset'] as $key => $value) { | |
$errorReturn .= $value . ','; | |
} | |
} | |
$errorReturn = rtrim($errorReturn, ','); | |
if (isset($error['empty']) and !empty($error['empty'])) { | |
$errorReturn .= ' Empty:'; | |
foreach ($error['empty'] as $key => $value) { | |
$errorReturn .= $value . ','; | |
} | |
} | |
$errorReturn = rtrim($errorReturn, ','); | |
} | |
} | |
if ($errorReturn !== null) { | |
return array('return' => false, 'response' => 'Nie wypełniono wszystkich pól.' . ($debug == true ? $errorReturn : '')); | |
} | |
if (isset($v) and empty($v) and $v != true) { | |
return array('return' => false, 'response' => 'Niepoprawne dane'); | |
} | |
return array('return' => true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment