承接 darsyn/unboxer 相关项目开发

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

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

darsyn/unboxer

Composer 安装命令:

composer require darsyn/unboxer

包简介

Simple utility to unbox complex data structures (objects) to native data types, suitable for encoding into formats such as JSON, YAML, etc.

README 文档

README

Simple utility to unbox complex data structures (objects) to native data types, suitable for encoding (for example, JSON).

Documentation

Code of Conduct

This project includes and adheres to the Contributor Covenant as a Code of Conduct.

Supported Types

This library returns all scalar and null values as-is, plus recursively processing all array (and stdClass) types.

When this library encounters an object that is an instance of a known type, it will attempt to convert it by using the return value of a specific method. Object types supported by this library out-of-the-box include:

  • Dates (objects implementing DateTimeInterface) which are converted to strings according to RFC3339 (eg, 2019-02-05T12:15:32+00:00).
  • Timezones (objects implementing DateTimeZone) which result in a string containing the timezone name (eg, America/Vancouver).
  • Exceptions and errors (objects implementing Throwable) which result in a string containing the exception message.
  • JSON (objects implementing JsonSerializable) which result in the library recursively iterating over the JSON data returned.
  • Doctrine collections (objects implementing Collection interface) which result in the library iterating over each of the items inside the collection.

Additionally, any user-land object can implement UnboxableInterface. Similar to JsonSerializable::jsonSerialize() method, the __unbox method can return anything as a representation of its internal state. It is recommended to return unboxable objects as-is, as everything returned from UnboxableInterface::__unbox is recursively iterated over anyway.

Brief Example

<?php declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;
use Darsyn\Unboxer\UnboxableInterface;
use Darsyn\Unboxer\UnboxingException;

class Group implements UnboxableInterface {
    public function __construct(
        private string $name
    ) {}

    public function __unbox() {
        return $this->name;
    }
}

class Options implements \JsonSerializable {
    public function __construct(
        private bool $active,
        private bool $verified,
        private \DateTimeZone $timezone
    ) {}

    public function jsonSerialize() {
        return [
            'active' => $this->active,
            'verified' => $this->verified,
            'tz' => $this->timezone,
        ];
    }
}

class Member implements UnboxableInterface
{
    private ArrayCollection $groups;

    public function __construct(
        private int $id,
        private string $username,
        array $groups = [],
        private ?Options $options = null
    ) {
        $this->groups = new ArrayCollection($groups);
    }

    public function __unbox()
    {
        return [
            // Scalars are used as-is.
            'id' => $this->id,
            'username' => $this->username,
            // Objects of known types are returned as-is, but recursively iterated over.
            'groups' => $this->groups,
            // JSON-serializable objects are never actually run through json_encode().
            'options' => $this->options ?: [],
        ];
    }
}

$member = new Member(123, 'dr-evil', [
    new Group('admin'),
    new Group('moderator'),
    new Group('sharks-with-lasers'),
], new Options(true, false, new \DateTimeZone('America/Vancouver')));

try {
    $output = (new Unboxer)->unbox($member);
    var_dump($output);
} catch (UnboxingException $e) {
    echo $e->getMessage();
}

var_dumping the variable $output results in:

array(4) {
  'id' =>
  int(123)
  'username' =>
  string(7) "dr-evil"
  'groups' =>
  array(3) {
    [0] =>
    string(5) "admin"
    [1] =>
    string(9) "moderator"
    [2] =>
    string(18) "sharks-with-lasers"
  }
  'options' =>
  array(3) {
    'active' =>
    bool(true)
    'verified' =>
    bool(false)
    'tz' =>
    string(17) "America/Vancouver"
  }
}

Note that returning multiple nested unboxable objects will result in the output collapsing down into a single value:

<?php declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;
use Darsyn\Unboxer\UnboxableInterface;
use Darsyn\Unboxer\UnboxingException;

$data = new class implements UnboxableInterface {
    public function __unbox() {
        return new class implements UnboxableInterface {
            public function __unbox() {
                return new class implements UnboxableInterface {
                    public function __unbox() {
                        return new \RuntimeException('Error Message');
                    }
                };
            }
        };
    }
};

try {
    $output = (new Unboxer)->unbox($data);
    var_dump($output);
} catch (UnboxingException $e) {
    echo $e->getMessage();
}
string(13) "Error Message"

Extending

Additional known object types can be added by extending Unboxer and overriding the getKnownDataTypes method. For each known object type, either a closure or an array specifying which method to call on the object may can specified:

<?php declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;

class MyUnboxer extends Unboxer {
    protected function getKnownDataMethods(): iterable {
        // Don't forget to return the known data methods defined in the original, parent Unboxer.
        // The parent returns an array, but any iterable is acceptable.
        yield from parent::getKnownDataMethods();

        // Config array example.
        // Must be in the format ['methodToCall', ['optional', 'arguments', 'array']].
        yield \DateTimeInterface::class => ['format', [\DateTimeInterface::RFC3339]];

        // Closure example.
        yield \DateTimeInterface::class => function (\DateTimeInterface $date): string {
            return $date->format(\DateTimeInterface::RFC3339);
        };
    }
}

The unboxer will, by default, convert any objects with the __toString() magic method to a string. To turn this functionality off, extend Unboxer and override the class constant STRINGIFY_OBJECTS.

<?php declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;

class MyUnboxer extends Unboxer {
    public const STRINGIFY_OBJECTS = false;
}

License

Please see the separate license file included in this repository for a full copy of the MIT license, which this project is licensed under.

Authors

If you make a contribution (submit a pull request), don't forget to add your name here!

darsyn/unboxer 适用场景与选型建议

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

它主要适用于以下技术方向: 「data」 「convert」 「native」 「complex」 「unbox」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-14