rhinox/input-data

v0.9.11 2024-09-12 19:19 UTC

This package is auto-updated.

Last update: 2024-09-12 19:20:07 UTC


README

InputData 是一个 PHP 库,它可以帮助解析用户输入的数据,或可能无效、更改或损坏的数据源。它根据预期和合理的默认值来解析数据。它不代替验证或清理,而是用于基于最佳努力的解析。

安装

composer require rhinox/input-data

基本用法

use Rhino\InputData\InputData;

$data = new InputData([
    'foo' => 'bar',
]);

// or

try {
    $data = InputData::jsonDecode('{"foo":"bar"}');
} catch (\Rhino\InputData\ParseException $exception) {
    // Handle invalid JSON
}

echo $data->string('foo'); // outputs "bar"

用例

  • 处理用户输入(PHP, Laravel)
  • 处理 API 数据
  • 读取/导入文件(CSV, JSON)

用户输入

$post = new InputData($_POST);
$get = new InputData($_GET);
$body = InputData::jsonDecode(file_get_contents('php://input'));

API 数据

$response = (new \GuzzleHttp\Client())->get('https://api.agify.io/?name=petah');
$data = InputData::jsonDecode($response->getBody());

读取文件

function readCsv(string $file) {
    $handle = fopen($file, 'r');
    for ($i = 0; $row = fgetcsv($handle); ++$i) {
        yield $i => new InputData($row);
    }
    fclose($handle);
}

foreach (readCsv('csv-data.csv') as $i => $row) {
    if ($i->int() === 0) continue;
    $name = $row->string(0);
    $number = $row->decimal(1);
    $date = $row->dateTime(2, 'UTC');
}

方法

以下使用示例数据

$data = new InputData([
    'foo' => 'bar',
    'seven' => '7.5',
    'july3' => '2021-07-03 11:45:30',
    'fruit' => ['apple', 'banana', 'pear'],
]);

字符串

$data->string(string $key, ?string $default): ?string
$data->string('foo') // "bar"
$data->string('seven') // "7.5"
$data->string('baz') // ""
$data->string('baz', null) // null
$data->string('baz', 'qux') // "qux"
$data->string('fruit') // ""

整数

$data->int(int $key, ?int $default): ?int
$data->int('foo') // int(0)
$data->int('seven') // int(7)
$data->int('baz') // int(0)
$data->int('baz', 12) // int(12)
$data->int('baz', null) // null
$data->int('fruit') // int(0)

十进制

$data->decimal(string $key, ?float $default): ?float
$data->decimal('foo') // float(0)
$data->decimal('seven') // float(7.5)
$data->decimal('baz') // float(0)
$data->decimal('baz', 12.5) // float(12.5)
$data->decimal('baz', null) // null
$data->decimal('fruit') // float(0)

日期时间

$data->dateTime(?string $name, ?string $timezone = null, ?string $default = null): ?DateTimeImmutable
$data->dateTime('july3', 'Pacific/Auckland') // object(DateTimeImmutable) Pacific/Auckland
$data->dateTime('july3') // object(DateTimeImmutable) server timezone
$data->dateTime('baz') // null
$data->dateTime('baz', null, 'now') // object(DateTimeImmutable) current date/time

数组

$data->arr(string $key, ?array $default): InputData
// Output each item of an array
foreach ($data->arr('fruit') as $fruit) {
    echo $fruit->string();
}
$data->arr('fruit')->getData() // ["apple", "banana", "pear"]
$data->arr('fruit')->count() // int(3)
$data->arr('fruit')->isEmpty() // false
$data->arr('fruit')->isArray() // true
$data->string('fruit.0') // "apple"

// Outputs nothing, treated as empty array
foreach ($data->arr('baz') as $baz) {
    echo $baz->string();
}
$data->arr('baz')->getData() // array(0)
$data->arr('baz')->count() // int(0)
$data->arr('baz')->isEmpty() // true
$data->arr('baz')->isArray() // false
$data->string('baz.0') // ""

其他方法

$data->json(string $key, ?mixed $default): InputData

解析 JSON 字符串并返回 InputData 实例。

$data->raw(string $key, ?mixed $default): mixed

以数组形式返回原始数据项。

$data->getData(): mixed

以原始形式返回原始数据。

修改数据

有两个类可供使用,允许修改数据

  • MutableInputData 所有方法修改当前实例中的数据
  • ImmutableInputData 返回一个包含修改后数据的新的实例
$data->extend(array ...$newData): static
$data->filter(?callable $callback = null): static
$data->mapRecursive(callable $callback): static
$data->values(): static
$data->set(string $name, $value): static
$data->unset(string $name): static

扩展 InputData

您可以通过扩展基类 InputData 来实现自己的类型解析。一些示例包括

  • UUID examples/uuid.php
  • JWT examples/jwt.php