定制 lesphp/property-info-typed-array 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lesphp/property-info-typed-array

Composer 安装命令:

composer require lesphp/property-info-typed-array

包简介

Extract key and value types using PHP Attributes for array class properties. This dispense the use of PhpDocExtractor for typed arrays.

README 文档

README

This is a package that offer an PHP native attribute for symfony/property-info retrieve correct types of array elements. Now days, this is only possible with DocBlock annotation. DocBlocks is PHP first-class citizen, but it isn't for type checking/enforcing. A wrong class name inside a docblock is only textual documentation for PHP, but not for developer. Then, documentations can be removed/replaced and the program behavior should be the same.

Instalation

composer require lesphp/property-info-typed-array

Compatibility

symfony/property-info lesphp/property-info-typed-array
6.x 6.x

Usage

The TypedArray attribute is repeatable, them multiples annotations are evaluated as a OR clause.

use LesPhp\PropertyInfo\TypedArray;
use LesPhp\PropertyInfo\TypedArrayAttributeExtractor;

class FooBar {
    #[TypedArray(type: Baz::class, nullable: false, keyType: 'int')]
    private array $baz;
    
    #[TypedArray(type: Baz::class, nullable: true)]
    #[TypedArray(type: 'string', nullable: true)]
    private array $bazOrString;
}

$typedArrayExtractor = new TypedArrayAttributeExtractor();

$typedArrayExtractor->getTypes(Foo_Bar::class, 'baz');
$typedArrayExtractor->getTypes(Foo_Bar::class, 'bazOrString');

In combination with symfony/serializer, this can be a powerful annotation and dispense requirement of DocBlock annotation readers.

use LesPhp\PropertyInfo\TypedArrayAttributeExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader());
$propertyTypeExtractor = new PropertyInfoExtractor([new ReflectionExtractor()], [new TypedArrayAttributeExtractor()]);
$propertyNormalizer = new PropertyNormalizer($classMetadataFactory, null, $propertyTypeExtractor);

$serializer = new Serializer(
    [$propertyNormalizer, new ArrayDenormalizer()],
    [new JsonEncoder()]
);

统计信息

  • 总下载量: 119
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固