liamhackett/valueobjectcompiler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

liamhackett/valueobjectcompiler

最新稳定版本:0.0.2

Composer 安装命令:

composer require liamhackett/valueobjectcompiler

包简介

Compile strict type, read only value objects from JSON

README 文档

README

Source Code Latest Version Software License PHP Version Coverage Status Build

This is currently in Alpha

This package takes a source file, such as JSON and creates strict typed, PSR12, readonly Value Objects.

The aim is to slowly support more file formats with the same file output.

Installation

composer require --dev liamhackett/valueobjectcompiler

How To Use

Single JSON file Compiler

vendor/bin/ValueObjectCompiler compile:json {jsonLocation} --outputDir={dir}

Single file compiler will take a JSON file and output its value object representation. By default, it will output in the current Directory, but you can specify your own directory using the outputDir flag.

Example

example.json

{
  "id": 12,
  "name": "Json Object",
  "status": "Published",
  "tags": ["tag1", "tag2", "tag3"],
  "sub_objects": [
    {
      "name": "Hello", 
      "value": 13.132
    },
    {
      "name": "World",
      "value": null
    }
  ]
}

Example.php

<?php

readonly class Example
{
    /**
     * @var string[] $tags
     * @var SubObject[] $subObjects
     */
    public function __construct(
        public int $id,
        public string $name,
        public string $status,
        public array $subObjects,
        public array $tags,
    ) {
    }

    /**
     * @return self[]
     */
    public static function hydrateMany(array $bulkData): array
    {
        $result = [];

        foreach ($bulkData as $data) {
            $result[] = self::hydrate($data);
        }

        return $result;
    }

    public static function hydrate(array $data): self
    {
        if (!isset($data['id'], $data['name'], $data['status'], $data['sub_objects'], $data['tags'])) {
            throw new \RuntimeException('Missing required parameter');
        }
        return new self(
            id: $data['id'],
            name: $data['name'],
            status: $data['status'],
            subObjects: SubObject::hydrateMany($data['sub_objects']),
            tags: $data['tags'],
        );
    }
}

SubObject.php

<?php

readonly class SubObject
{
    public function __construct(
        public string $name,
        public ?float $value = null,
    ) {
    }

    /**
     * @return self[]
     */
    public static function hydrateMany(array $bulkData): array
    {
        $result = [];

        foreach ($bulkData as $data) {
            $result[] = self::hydrate($data);
        }

        return $result;
    }

    public static function hydrate(array $data): self
    {
        if (!isset($data['name'])) {
            throw new \RuntimeException('Missing required parameter');
        }
        return new self(
            name: $data['name'],
            value: $data['value'] ?? null,
        );
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-11-08