keny1911/populate

从数组或其他对象自动填充对象属性的库。

v1.0.2 2022-08-01 07:18 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:49 UTC


README

PHP Populate 是一个从源对象填充属性到目标对象的库。

例如,您可以使用此库自动从 DTO 填充属性到 ORM 实体。

安装

composer require kenny1911/populate

用法

简单用法

要将一个对象的字段用另一个对象的值填充,请使用方法 Kenny1911\Populate\PopulateInterface::populate()。方法参数

  • $src - 将从中取值的源数组或对象。
  • $dest - 目标对象。
  • $properties - 可更新的允许属性数组。
  • $ignoreProperties - 不更新的拒绝属性数组。
  • $mapping - 匹配源对象($src)和目标对象($dest)属性名称的键值映射。键 - 在 $src 中的属性名称,值 - 在 $dest 中。

使用 PopulateBuilder 创建新的 PopulateInterface 实例

use Kenny1911\Populate\PopulateBuilder;

$populate = PopulateBuilder::create()->build(); // Create new instance

class Src
{
    public $foo;
    public $bar;
    public $baz;
}

class Dest
{
    public $foo;
    public $bar;
    public $baz;
}

$src = new Src();
$src->foo = 'Foo';

$dest = new Dest();

$populate->populate(
    $src,               // Source object
    $dest,              // Destination object
    ['foo', 'bar'],     // Only properties `foo` and `bar` will be populated
    ['bar'],            // Property `bar` won't bw populated
    ['foo' => 'bar']    // Value of $src->foo will be set to $dest->bar
);

// $dest->bar === 'Foo';

高级用法

您可能需要使用预设的 $properties$ignoreProperties$mapping 参数设置。您可以使用 AdvancedPopulate 来实现这一点

use Kenny1911\Populate\PopulateBuilder;

$settings = [
    [
        'src' => 'Src',                 // Required
        'dest' => 'Dest',               // Required
        'properties' => ['foo', 'bar'], // Optional
        'ignore_properties' => ['bar'], // Optional
        'mapping' => ['foo' => 'bar']   // Optional
    ]
];

$populate = PopulateBuilder::create()->setSettings($settings)->build();

class Src
{
    public $foo;
    public $bar;
    public $baz;
}

class Dest
{
    public $foo;
    public $bar;
    public $baz;
}

$src = new Src();
$src->foo = 'Foo';

$dest = new Dest();

$populate->populate($src, $dest);

// $dest->bar === 'Foo';

如果您使用 $properties$ignoreProperties 参数,则不会使用预设设置。

如果您设置 $mapping 参数,它将合并到预设映射中。

与 Symfony 集成

  1. config/bundles.php 中注册捆绑包

    return [
        // ...
        Kenny1911\Populate\Bridge\Symfony\PopulateBundle::class => ['all' => true]
        // ...
    ];
  2. 创建文件 config/packages/populate.yaml。示例

    populate:
        settings:
            -   src: Src
                dest: Dest
                properties: [foo, bar]
                ignore_properties: [bar]
                mapping:
                    foo: bar

现在,您可以将 PopulateInterface 注入到您自己的服务中。

use Kenny1911\Populate\PopulateInterface;

class Service
{
    /** @var PopulateInterface */
    private $populate;

    public function __construct(PopulateInterface $populate)
    {
        $this->populate = $populate;
    }

    public function action($src, $dest)
    {
        $this->populate->populate($src, $dest);
    }
}

此外,您还可以使用公共 symfony 服务 populate

use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

class Service implements ContainerAwareInterface
{
    use ContainerAwareTrait;

    public function action($src, $dest)
    {
        $populate = $this->container->get('populate');

        $populate->populate($src, $dest);
    }
}