amacode/property-info-override

Wrapper for symfony PropertyInfo component which returns user-defined property info if set

1.0.1 2023-01-23 15:48 UTC

This package is auto-updated.

Last update: 2024-10-23 19:52:42 UTC


README

Packagist Version GitHub Workflow Status Codecov GitHub

Overview

This package is an extension for PropertyInfo component.
Sometimes the default PropertyInfoExtractor defines the property meta info not the way you want. It may break you API schema generation or requests validations (is actual for ApiPlatform).
This package allows you to override a property type meta info which can be used by 3rd party libraries if it had been defined incorrectly (see usage example below).

Installation

composer require amacode/property-info-override

Usage

Add PropertyType annotation/attribute to property where type is defined incorrectly by 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;
}

Client code

<?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
    }
}