跳至正文

更新日志 0.17.0 — 2022 年 11 月 8 日

重要变更

此版本中引入的主要功能是将灵活模式分为三种不同的模式:

  1. 柔性铸造

更改以下解释的行为:

$flexibleMapper = (new \CuyZ\Valinor\MapperBuilder())
    ->enableFlexibleCasting()
    ->mapper();

// ---
// Scalar types will accept non-strict values; for instance an
// integer type will accept any valid numeric value like the
// *string* "42".

$flexibleMapper->map('int', '42');
// => 42

// ---
// List type will accept non-incremental keys.

$flexibleMapper->map('list<int>', ['foo' => 42, 'bar' => 1337]);
// => [0 => 42, 1 => 1338]

// ---
// If a value is missing in a source for a node that accepts `null`,
// the node will be filled with `null`.

$flexibleMapper->map(
    'array{foo: string, bar: null|string}',
    ['foo' => 'foo'] // `bar` is missing
);
// => ['foo' => 'foo', 'bar' => null]

// ---
// Array and list types will convert `null` or missing values to an
// empty array.

$flexibleMapper->map(
    'array{foo: string, bar: array<string>}',
    ['foo' => 'foo'] // `bar` is missing
);
// => ['foo' => 'foo', 'bar' => []]
  1. 多余的按键

源数组中允许使用多余的键,以防止在值未绑定到任何对象属性/参数或成形数组元素时出现错误。

(new \CuyZ\Valinor\MapperBuilder())
    ->allowSuperfluousKeys()
    ->mapper()
    ->map(
        'array{foo: string, bar: int}',
        [
            'foo' => 'foo',
            'bar' => 42,
            'baz' => 1337.404, // `baz` will be ignored
        ]
    );
  1. 宽容类型

允许在映射期间使用mixed的许可类型和object 。

(new \CuyZ\Valinor\MapperBuilder())
    ->allowPermissiveTypes()
    ->mapper()
    ->map(
        'array{foo: string, bar: mixed}',
        [
            'foo' => 'foo',
            'bar' => 42, // Could be any value
        ]
    );

新功能

  • 添加对strict-array类型的支持 ( d456eb )
  • 引入新的回调消息格式化程序( 93f898 )
  • 引入新的帮助程序类来列出消息( 513827 )
  • 三种不同模式下的拆分映射器灵活模式 ( 549e5f )

错误修复

  • 在灵活模式下允许数组节点缺失值和空值( 034f1c )
  • 在灵活模式下允许指定结构的数组可为空节点缺失值 ( 08fb0e )
  • 仅在灵活模式下处理联合类型中的标量值转换 ( 752ad9 )

其他

  • 不要使用uniqid() ( b81847 )
  • 在灵活模式下将缺失的源值转换为null ( 92a41a )