定制 neutronstars/symfony-normalizer-enum-php 二次开发

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

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

neutronstars/symfony-normalizer-enum-php

Composer 安装命令:

composer require neutronstars/symfony-normalizer-enum-php

包简介

Added Symfony normalizer for the enumeration system designed to be as close to PHP 8.1 for earlier versions.

README 文档

README

Read the enum php doc:

https://github.com/Neutron-Pro/enum-php

Installation

composer require neutronstars/symfony-normalizer-enum-php

Add doctrine:

composer require neutronstars/doctrine-enum-php-type

https://github.com/Neutron-Pro/doctrine-enum-php-type

Example for serialize an object entity with an enum field:

/**
 * @method static self WAITING()
 * @method static self PUBLISHED()
 */
class MyStringEnum extends \NeutronStars\Enum\Enum {
  public const WAITING   = 'Waiting';
  public const PUBLISHED = 'Published';
}

/**
 * @Entity()
 */
class Post {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer") 
     * @Groups(["post:details"]) 
     * @var int 
     */
     private $id;
     
     // ... Other field (name, createdAt, updatedAt etc..)
     
     /**
      * For the customs type, please check the doctrine enum type documentation.
      * @ORM\Column(type="my_string_enum")
     *  @Groups(["post:details"]) 
      * @var MyStringEnum 
      */
      private $state;
      
      public function __construct() {
        $this->state = MyStringEnum::WAITING();
      }
      
      // ... All methods implemented.
}

/**
 * @Route("/posts", name="list_post")
 */
class ListPostController {

    /** @var PostRepository PostRepository */
    private $postRepository;
    
    public function __construct(PostRepository $postRepository) {
        $this->postRepository = $postRepository;
    }

    public function __invoke(): JSONResponse
    {
        $serializer = new \Symfony\Component\Serializer\Serializer([
            new \NeutronStars\Symfony\Enum\Normalizer\EnumNormalizer(MyStringEnum::class)
        ], [new \Symfony\Component\Serializer\Encoder\JsonEncode()]);
        
        return new JSONResponse(
            $serializer->normalize(
                $this->postRepository->findAll(),
                null,
                [ 'groups' => ['post:details'] ] 
            )
        );
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-07-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固