amacode/property-info-override

symfony PropertyInfo 组件的包装器,如果设置了,则返回用户定义的属性信息

1.0.1 2023-01-23 15:48 UTC

This package is auto-updated.

Last update: 2024-09-23 19:36:13 UTC


README

Packagist Version GitHub Workflow Status Codecov GitHub

概览

此包是 PropertyInfo 组件的扩展。
有时默认的 PropertyInfoExtractor 不会按照您期望的方式定义属性元信息。它可能会破坏您API模式生成或请求验证(对 ApiPlatform 也适用)。
此包允许您覆盖属性类型元信息,如果它被错误地定义,则第三方库可以使用(请参阅下面的使用示例)。

安装

composer require amacode/property-info-override

使用

PropertyType 注解/属性添加到由 PropertyInfoExtractor 错误定义类型的属性中
<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Amacode\PropertyInfoOverride\Annotation\PropertyType;

/**
 * @ORM\Entity()
 */
class TestEntity
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * By default, bigint will be defined as string by DoctrineExtractor because it can be bigger than PHP_INT_MAX in 32-bit systems.
     * But if you're sure that your bigint will always be less than PHP_INT_MAX, you can override type manually.
     * 
     * @PropertyType(type="int")
     *
     * @ORM\Column(type="bigint")
     */
    private $bigIntAsInt;
    
    /**
     * @ORM\Column(type="bigint")
     */
    private $bigIntAsString;
}

客户端代码

<?php

class ClientCode
{
    private PropertyInfoExtractorInterface $propertyInfoExtractor;

    public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor)
    {
        $this->propertyInfoExtractor = $propertyInfoExtractor;
    }
    
    public function action(): void
    {
        $types = $this->propertyInfoExtractor->getTypes(TestEntity::class, 'bigIntAsInt');
        
        echo $types[0]->getBuiltinType(); // int
        
        $types = $this->propertyInfoExtractor->getTypes(TestEntity::class, 'bigIntAsString');
        
        echo $types[0]->getBuiltinType(); // string
    }
}