public static function post(Array $post) {
$cleaned = [];
foreach ($post as $key => $value) {
$cleaned[] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
}
return $cleaned;
}
/**
* Очищает массив от пробелов и слэшей.
*
* @param array
* @return array
*/
private static function clearData(&$in)
{
if ($in && is_array($in)) {
foreach ($in as $key => $value) {
if (is_array($value)) {
self::clearData($in[$key]);
} else {
$value = trim($value);
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$in[$key] = $value;
}
}
}
return $in;
}
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class InvalidRequestParam extends \InvalidArgumentException
{}
class MyController
{
/**
* @param Request $request
* @return Response
*/
public function myAction(Request $request)
{
try {
$intPostParam = $request->request->get('intPostParam');
$unsignedIntPostParam = $request->request->get('unsignedIntPostParam');
$md5GetParam = $request->query->get('md5GetParam');
$optionalDigitGetParam = $request->query->get('optionalDigitGetParam');
if (is_null($intPostParam)) {
throw new InvalidRequestParam('"intPostParam" is required');
} elseif (!is_numeric($intPostParam)) {
throw new InvalidRequestParam('"intPostParam" must be numeric');
}
if (is_null($unsignedIntPostParam)) {
throw new InvalidRequestParam('"unsignedIntPostParam" is required');
} elseif (!ctype_digit($unsignedIntPostParam)) {
throw new InvalidRequestParam('"unsignedIntPostParam" must be digit');
}
if (is_null($md5GetParam)) {
throw new InvalidRequestParam('"md5GetParam" is required');
} elseif (!preg_match('/^[\da-f]{32}$/', $md5GetParam)) {
throw new InvalidRequestParam('"md5GetParam" must be correct md5 hash');
}
if (!is_null($optionalDigitGetParam) && !ctype_digit($optionalDigitGetParam)) {
throw new InvalidRequestParam('"optionalDigitGetParam" must be digit or null');
}
// Тут ваша бизнес логика
return new Response('All params correct');
} catch (InvalidRequestParam $e) {
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
} catch (\Throwable $e) {
return new Response('Some thing went wrong', Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}