Skip to content

Instantly share code, notes, and snippets.

@ErlendEllingsen
Created February 5, 2018 23:05
Show Gist options
  • Save ErlendEllingsen/fa79a3302df35057e2c4d00a3a30b36e to your computer and use it in GitHub Desktop.
Save ErlendEllingsen/fa79a3302df35057e2c4d00a3a30b36e to your computer and use it in GitHub Desktop.
<?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