Created
February 5, 2018 23:05
-
-
Save ErlendEllingsen/fa79a3302df35057e2c4d00a3a30b36e to your computer and use it in GitHub Desktop.
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 | |
class InFields { | |
/** | |
* Fetches value from POST. | |
* $fields array with post fields to be fetched | |
* $strict if strict mode is enabled, the fields will be required (if not, they will be added, but have null as value) | |
* $stringFilter if the stringFilter is enabled, the variables are ran through filter_var with FILTER_SANITIZE_STRING | |
* | |
* @throws AppException (w/ id 'missing_field') on missing field. | |
*/ | |
public static function fetchMultiple($fields = [], $strict = true, $stringFilter = true) { | |
if (!is_array($fields)) return false; | |
$out = []; | |
foreach ($fields as $key => $value) { | |
if (!isset($_POST[$value])) { | |
if ($strict) throw new AppException('missing_field', 'Missing field ' . $value); | |
$out[$value] = null; | |
} else { | |
if ($stringFilter) { | |
$out[$value] = filter_var($_POST[$value], FILTER_SANITIZE_STRING); | |
} else { | |
$out[$value] = $_POST[$value]; | |
} | |
} | |
} | |
return $out; | |
//end __construct | |
} | |
//end InFields | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment