filter_input_array() UUIDs

Code sagt mehr als tausend Worte:

<?php
/**
 * Use a callback function to filter UUIDs from POST with filter_input_array()
 * c0fc2876-2551-426b-8cc1-69730d22774a
 * 2c91ff35-7089-4383-91e3-0976a7bf06a9
 * ddc25d51-d2a7-4b32-9ca8-3cf70d89dffb
 */
require '../../vendor/autoload.php';
 
use Laminas\Validator\Uuid;
 
$uuid = new Uuid();
function filterUuid(string $value): string
{
    global $uuid;
    return $uuid->isValid($value) ? $value : '';
}
 
class MyStaticFilter
{
    protected static Uuid $uuid;
 
    public static function filterUuid(string $value): string
    {
        if (!isset(self::$uuid)) {
            self::$uuid = new Uuid();
        }
        return self::$uuid->isValid($value) ? $value : '';
    }
}
 
class MyFilter
{
    protected Uuid $uuid;
 
    public function filterUuid(string $value): string
    {
        if (!isset($this->uuid)) {
            $this->uuid = new Uuid();
        }
        return $this->uuid->isValid($value) ? $value : '';
    }
}
 
$myFilter = new MyFilter();
 
$call = (int)filter_input(INPUT_POST, 'callback', FILTER_SANITIZE_NUMBER_INT);
switch ($call) {
    case 1:
        $uuids = filter_input_array(INPUT_POST, ['anuuid' => [
            'filter'  => FILTER_CALLBACK,
            'flags'   => FILTER_REQUIRE_ARRAY,
            'options' => 'filterUuid',
        ]]);
        break;
    case 2:
        $uuids = filter_input_array(INPUT_POST, ['anuuid' => [
            'filter'  => FILTER_CALLBACK,
            'flags'   => FILTER_REQUIRE_ARRAY,
            'options' => ['MyStaticFilter', 'filterUuid'],
        ]]);
        break;
    case 3:
        $uuids = filter_input_array(INPUT_POST, ['anuuid' => [
            'filter'  => FILTER_CALLBACK,
            'flags'   => FILTER_REQUIRE_ARRAY,
            'options' => [$myFilter, 'filterUuid'],
        ]]);
        break;
    default:
 
}
 
$uuids = $uuids['anuuid'] ?? [];
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <title>filter uuid</title>
    <link href="../../../w3.css" media="screen" rel="stylesheet" type="text/css">
<body>
<div class="w3-container">
    <form action="filter-uuid.php" method="post" style="width: 400px">
        <label class="w3-block">1. UUID <input type="text" class="w3-input" name="anuuid[]" value="<?php echo $uuids[0] ?? '' ?>"></label>
        <label class="w3-block">2. UUID <input type="text" class="w3-input" name="anuuid[]" value="<?php echo $uuids[1] ?? '' ?>"></label>
        <label class="w3-block">4. UUID <input type="text" class="w3-input" name="anuuid[]" value="<?php echo $uuids[2] ?? '' ?>"></label>
        <select class="w3-select" name="callback">
            <option value="1" <?php echo $call == 1 ? 'selected="selected"' : '' ?>>function</option>
            <option value="2" <?php echo $call == 2 ? 'selected="selected"' : '' ?>>static</option>
            <option value="3" <?php echo $call == 3 ? 'selected="selected"' : '' ?>>object</option>
        </select>
        <button type="submit" class="w3-button w3-blue-gray">ok</button>
    </form>
</div>
<div class="w3-container">
    <pre><?= print_r($uuids, true) ?></pre>
</div>
</body>

https://www.php.net/manual/de/function.filter-input-array

https://www.php.net/manual/en/filter.filters.misc.php

https://www.php.net/manual/de/language.types.callable.php

Schreibe einen Kommentar