承接 eddiriarte/oh 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

eddiriarte/oh

Composer 安装命令:

composer create-project eddiriarte/oh

包简介

Another simple yet neat object hydrator library for PHP.

README 文档

README

OH Logo

OH

Another simple yet neat object hydrator library for PHP.

Install

composer require eddiriarte/oh

Usage

This library allows conversion of data from (nested) array format into proper PHP objects.

// Define your class(es):
class Person
{
    public function __construct(
        private string $firstName,
        private string $lastName
    ) {}
    
    //... here you might define getters/setters 
}

// Initialize hydrator manager:
$manager = new \EddIriarte\Oh\Manager();

// Execute hydration:
$person = $manager->hydrate(Person::class, ['first_name' => 'Bruce', 'last_name' => 'Wayne']);

assert($person instanceof Person);

Array-Key Cases

The default behaviour is to map snake-case keys from array into the class property/parameter names. However, whenever required its possible to configure that behaviour in the manager:

use EddIriarte\Oh\Manager;

$snakeCaseManager = new Manager(['source_naming_case' => StringCase::SnakeCase]); // default behaviour
$person1 = $snakeCaseManager->hydrate(Person::class, ['first_name' => 'Bruce', 'last_name' => 'Wayne']);

$camelCaseManager = new Manager(['source_naming_case' => StringCase::CamelCase]);
$person2 = $camelCaseManager->hydrate(Person::class, ['firstName' => 'Bruce', 'lastName' => 'Wayne']);

$studlyCaseManager = new Manager(['source_naming_case' => StringCase::StudlyCase]);
$person3 = $studlyCaseManager->hydrate(Person::class, ['FirstName' => 'Bruce', 'LastName' => 'Wayne']);

$kebabCaseManager = new Manager(['source_naming_case' => StringCase::KebabCase]);
$person4 = $kebabCaseManager->hydrate(Person::class, ['first-name' => 'Bruce', 'last-name' => 'Wayne']);

$anyCaseManager = new Manager(['source_naming_case' => StringCase::AnyCase]);
$person5 = $anyCaseManager->hydrate(Person::class, ['firstName' => 'Bruce', 'last_name' => 'Wayne']);

Data Structure Objects

If for some reason your classes do not provide a respective constructor method, then the hydrator will try to populate matching class properties.

// Define your class(es):
class Person
{
    private string $firstName;
    private string $lastName;
    
    //... here you might define getters/setters 
}

// Initialize hydrator manager:
$manager = new \EddIriarte\Oh\Manager();

// Execute hydration:
$person = $manager->hydrate(Person::class, ['first_name' => 'Bruce', 'last_name' => 'Wayne']);

assert($person instanceof Person);

Additionally, the manager allows to specify the visibility of properties to consider:

use EddIriarte\Oh\Manager;
use EddIriarte\Oh\Enums\PropertyVisibility;

class ReadOnlyPerson
{
    public readonly string $firstName;
    public readonly string $lastName;
    public int $age;
}

// Initialize hydrator manager:
$manager = new Manager(['property_visibility' => PropertyVisibility::ReadOnly]);

// Execute hydration:
$person = $manager->hydrate(ReadOnlyPerson::class, ['first_name' => 'Bruce', 'last_name' => 'Wayne', 'age' => 44]);

var_dump($person);

// class ReadOnlyPerson#700 (3) {
//   public readonly string $firstName =>
//   string(5) "Bruce"
//   public readonly string $lastName =>
//   string(5) "Wayne"
//   public int $age =>
//   *uninitialized*
// }

Nested Objects

The hydrator loops over all properties/parameters and tries to initialize also nested types:

use EddIriarte\Oh\Manager;
use EddIriarte\Oh\Enums\PropertyVisibility;

class Person
{
    public function __construct(
        private string $firstName,
        private string $lastName
    ) {}
}

class Hero
{
    public function __construct(
        private string $name,
        private Person $alias
    ) {}
}

// Execute hydration:
$hero = (new Manager())->hydrate(
    Hero::class,
    [
        'name' => 'Batman',
        'alias' => [
            'first_name' => 'Bruce', 
            'last_name' => 'Wayne',
        ],
    ]
);

List, Dictionaries, Arrays

The hydrator already supports the population of object lists. In order to hydrate nested items into specific classes, it requires additional information, that can be provided wit PHP Attributes.

use EddIriarte\Oh\Attributes\ListMemberType;
use EddIriarte\Oh\Enums\PropertyVisibility;
use EddIriarte\Oh\Manager;

class Hero
{
    public function __construct(private string $name)
    {}
}

class HeroTeam
{
    public function __construct(
        private string $name,
        #[ListMemberType(Hero::class)]
        private array $heroes
    ) {}
}

$team = (new Manager())->hydrate(
    HeroTeam::class,
    [
        'name' => 'Batman & Robin',
        'heroes' => [
            ['name' => 'Batman'], 
            ['name' => 'Robin'],
        ],
    ]
);

The usage of instantiable Arrayable classes instead of arrays is also allowed. In this example we use Doctrine's ArrayCollection, but could also be done with Laravel collections:

use Doctrine\Common\Collections\ArrayCollection;

use EddIriarte\Oh\Attributes\ListMemberType;
use EddIriarte\Oh\Enums\PropertyVisibility;
use EddIriarte\Oh\Manager;

class Hero
{
    public function __construct(private string $name)
    {}
}

class HeroTeam
{
    public function __construct(
        private string $name,
        #[ListMemberType(Hero::class)]
        private ArrayCollection $heroes
    ) {}
}

$team = (new Manager())->hydrate(
    HeroTeam::class,
    [
        'name' => 'Batman & Robin',
        'heroes' => [
            ['name' => 'Batman'], 
            ['name' => 'Robin'],
        ],
    ]
);

Configuration

Still in progress...

Name Type Description
source_naming_case StringCase (Enum) The case of the keys used in the source array, needed to map them into the specific class property/parameter.

Default value: StringCase::SnakeCase
property_visibility PropertyVisibility (Enum) The property visibility on structure classes(without constructor) to filter fields.

Default value: PropertyVisibility::Private

Credits

String case functions were copied from Laravel.

Disclaimer

This library is still under active development and usage may change in order to optimize performance, add caching and dehydration.

eddiriarte/oh 适用场景与选型建议

eddiriarte/oh 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 eddiriarte/oh 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eddiriarte/oh 我们能提供哪些服务?
定制开发 / 二次开发

基于 eddiriarte/oh 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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