vijoni/class-generator

根据 YAML 配置生成类似类

v1.0.3 2022-06-29 06:18 UTC

This package is auto-updated.

Last update: 2024-09-29 06:09:08 UTC


README

使用通用功能创建表现一致的类

生成的源代码示例

<?php

declare(strict_types=1);

namespace VijoniOutput\Integration\Security\Shared\Generated;

use Vijoni\ClassGenerator\ClassBase;

class GeneratedUser extends ClassBase
{
  private string $email = '';
  private \VijoniOutput\Integration\Security\Shared\Generated\GeneratedSecret $secret;
  private array $roles = [];

  public const EMAIL = 'email';
  public const SECRET = 'secret';
  public const ROLES = 'roles';

  public function __construct()
  {
      $this->setSecret(new \VijoniOutput\Integration\Security\Shared\Generated\GeneratedSecret());
  }

  public function getEmail(): string
  {
    return $this->email;
  }

  public function setEmail(string $email): void
  {
    $this->email = $email;
    $this->modified[self::EMAIL] = true;
  }

  public function getSecret(): \VijoniOutput\Integration\Security\Shared\Generated\GeneratedSecret
  {
    return $this->secret;
  }

  public function setSecret(\VijoniOutput\Integration\Security\Shared\Generated\GeneratedSecret $secret): void
  {
    $this->secret = $secret;
    $this->modified[self::SECRET] = true;
  }

  /**
   * @return \VijoniOutput\Integration\Security\Shared\Generated\GeneratedRole[]
   */
  public function getRoles(): array
  {
    return $this->roles;
  }

  public function setRoles(array $roles): void
  {
    $this->roles = $roles;
    $this->modified[self::ROLES] = true;
  }

  public function toArray(): array
  {
    return [
      self::EMAIL => $this->getEmail(),
      self::SECRET => $this->getSecret(),
      self::ROLES => $this->getRoles(),
    ];
  }

  public function toDeepArray(): array
  {
    $asArray = $this->toArray();
    $asArray[self::SECRET] = $this->getSecret()->toDeepArray();
    $asArray[self::ROLES] = $this->collectionToArray($this->getRoles());

    return $asArray;
  }

  public function fromArray(array $properties): void
  {
    isset($properties[self::EMAIL]) && $this->setEmail($properties[self::EMAIL]);
    isset($properties[self::SECRET]) && $this->setSecret($properties[self::SECRET]);
    isset($properties[self::ROLES]) && $this->setRoles($properties[self::ROLES]);
  }
}

YAML 中的类定义示例

---
outputPath: _output/Integration/Security/Shared/Generated
namespace: VijoniOutput\Integration\Security\Shared\Generated
className: GeneratedUser

fields:
  email: string
  secret: \VijoniTest\Integration\Security\Shared\Generated\GeneratedSecret
  roles: \VijoniTest\Integration\Security\Shared\Generated\GeneratedRole[]

用法

查看测试以获取更多详细信息

$srcDirectory = PROJECT_DIR . '/src'; // all file paths will be related to this directory

$classGenerator = new Generator($srcDirectory, new SchemaFinder());
$classGenerator->generate('class-schema-*.yml'); // glob pattern for class definition file names

定义文件将在 /src 和其子目录中查找。
生成器处理对其他嵌套生成类的依赖,因此无需担心文件路径的加载顺序。
类将根据提供的源路径和每个类定义中定义的 outputPath 属性生成。

类定义语法

类使用 YAML 语言定义。

# location for generated class source code, relative to the directory provided when creating Generator instance
outputPath: _output/Acceptance/Shop/Shared/Generated/ 
namespace: VijoniOutput\Acceptance\Shop\Shared\Generated
className: GeneratedCustomer

# private fields for which accessor methods and array keys will be generated
# fields consist of a field name, type and optional default value
fields:
  id: string
  firstname: string
  lastname: string
  email: string
  birthDate: \DateTime
  age: int, 0
  address: \VijoniOutput\Acceptance\Shop\Shared\Generated\Address[]

# you can also define hardcoded constants, they can be useful for child classes
constants:
  DEFAULT_DATE: "'1900-01-01 00:00:00'"

基本数据类型的默认值

string' => '',
int' => -1,
float' => -1.0,
array' => [],
?xxx => null

生成器使用严格的类型检查。对于非可空值,您需要创建一个子类并重写生成的构造函数。PHP 要求严格类型属性必须初始化属性。

<?php

declare(strict_types=1);

namespace VijoniTest\Integration\ClassGenerator;

use DateTime;
use VijoniOutput\Integration\Shop\Shared\Generated\GeneratedCustomer;

class Customer extends GeneratedCustomer
{
  public function __construct()
  {
    $this->setBirthDate(new DateTime(static::DEFAULT_DATE));
    // as all setter methods are marking property as modified, your need to unset any modifications
    $this->resetModifiedValues();
  }
}

生成的类方法

toArray(): array

collectionToArray(array $collection): array

toDeepArray(): array

fromArray(array $properties): void

intersectArray(array $map, array $source): array

intersect(array $map, array $source): void

readModifiedValues(): array

resetModifiedValues(): void

mapTo(array $mapKeys): array

mapModifiedTo(array $mapKeys): array