螺旋/打包器-桥

Spiral 框架的打包器桥

v1.0.1 2023-10-05 12:30 UTC

README

PHP Version Require Latest Stable Version phpunit psalm Codecov Total Downloads type-coverage psalm-level

需求

请确保您的服务器配置了以下 PHP 版本和扩展

  • PHP >=8.1
  • Spiral 框架 ^3.0

安装

您可以通过 composer 安装此包

composer require spiral/marshaller-bridge

安装包后,您需要从包中注册引导加载器。

protected const LOAD = [
    // ...
    \Spiral\MarshallerBridge\Bootloader\MarshallerBootloader::class,
];

注意 引导加载器 Spiral\Serializer\Bootloader\SerializerBootloader 可以被移除。如果您使用 spiral-packages/discoverer,则不需要自己注册引导加载器。

配置

默认已配置此包,除非您需要更改默认配置,否则请使用以下功能。

此包提供了配置 Spiral\Marshaller\Mapper\MapperFactoryInterfaceSpiral\Marshaller\Marshaller 类使用的 matchers 的能力。创建一个文件 app/config/marshaller.php。添加 mapperFactorymatchers 配置参数。例如

<?php

declare(strict_types=1);

use Spiral\Core\Container\Autowire;
use Spiral\Marshaller\Mapper\AttributeMapperFactory;
use Spiral\Marshaller\Type\ArrayType;
use Spiral\Marshaller\Type\DateTimeType;
use Spiral\Marshaller\Type\DateIntervalType;
use Spiral\Marshaller\Type\EnumType;
use Spiral\Marshaller\Type\ObjectType;

return [
    'mapperFactory' => AttributeMapperFactory::class,
    'matchers' => [
        EnumType::class,
        DateTimeType::class,
        DateIntervalType::class,
        ArrayType::class,
        ObjectType::class,
    ],
];

用法

Spiral\Serializer\SerializerManager 一起使用。例如

use Spiral\Serializer\SerializerManager;

$serializer = $this->container->get(SerializerManager::class);

$result = $manager->serialize($payload, 'marshaller-json');
$result = $manager->serialize($payload, 'marshaller-serializer');

$result = $manager->unserialize($payload, Post::class, 'marshaller-json');
$result = $manager->unserialize($payload, Post::class, 'marshaller-serializer');

Spiral\Marshaller\MarshallerInterface 一起使用。例如

use Spiral\Marshaller\MarshallerInterface;

$marshaller = $this->container->get(MarshallerInterface::class);

$result = $marshaller->marshal($from); // from object to array
$result = $marshaller->unmarshal($from, new Post()); // from array to object

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近的变化。

贡献

请参阅 CONTRIBUTING 了解详情。

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件