承接 paysera/lib-object-wrapper 相关项目开发

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

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

paysera/lib-object-wrapper

Composer 安装命令:

composer require paysera/lib-object-wrapper

包简介

Wrapper around JSON-decoded data that lets easily get structured items

README 文档

README

This library provides class that lets easily get structured items from JSON-decoded data.

Why?

So that it would be easier and quicker to:

  • check correct data types. This allows to use PHP7 static type-hints easily;
  • check required keys/items;
  • give defaults to missing items;
  • provide information about exact place in object tree where the data fails your requirements.

Usage

$jsonString = json_encode([
    // ...
]);
/** @var stdClass $jsonData */
$jsonData = json_decode($jsonString);

$data = new ObjectWrapper($jsonData);

// Use like an associative array if needed
foreach ($data as $key => $value) {
    var_dump(isset($data[$value]));
    var_dump($data[$value]);
}

var_dump($data['non_existing']);    // just null - no notice

var_dump($data->getRequired('required_key'));   // throws exception if missing

var_dump($data->getString('key_with_string'));  // throws exception if not string, defaults to null
var_dump($data->getString('key_with_string', 'default'));  // different default
var_dump($data->getRequiredString('key_with_string'));  // throws exception if missing or not string

var_dump(get_class($data->getRequiredObject('inner_data'))); // always another ObjectWrapper instance
var_dump(get_class($data->getObject('inner_data'))); // another ObjectWrapper instance or null

var_dump($data->getArrayOfString('keys'));    // array of strings
var_dump($data->getArrayOfOject('children')); // array of ObjectWrapper instances

try {
    $data->getRequiredObject('inner_data')->getArrayOfObject('list')[0]->getRequiredString('key');
} catch (MissingItemException $e) {
    echo $e->getMessage(); // Missing required key "inner_data.list.0.key"
    echo $e->getKey();     // inner_data.list.0.key
}

Methods

  • $data[$key] - use array accessor to get any value inside, returns null if it's unavailable. If value is object, instance of ObjectWrapper will be returned;
  • getRequired. Returns mixed value, but checks that it is provided;
  • getRequiredBool. Returns bool;
  • getBool. Returns bool or null;
  • getRequiredFloat. Returns float. Accepts int, but casts it to float;
  • getFloat. Returns float or null. Accepts int, but casts it to float;
  • getRequiredInt. Returns int;
  • getInt. Returns int or null;
  • getRequiredObject. Returns another ObjectWrapper instance;
  • getObject. Returns another ObjectWrapper instance or null;
  • getRequiredString. Returns string;
  • getString. Returns string or null;
  • getArray. Returns array. If no item is provided, returns empty array, so no getRequiredArray is available;
  • getArrayOfBool. Returns array, all it's items are bool (or it's empty);
  • getArrayOfFloat. Returns array, all it's items are float (or it's empty);
  • getArrayOfInt. Returns array, all it's items are int (or it's empty);
  • getArrayOfString. Returns array, all it's items are string (or it's empty);
  • getArrayOfObject. Returns array, all it's items are instances of ObjectWrapper (or it's empty).
  • getDataAsArray. Returns array, all it's items are array or primitive types.
  • getOriginalData. Returns stdClass, the original data passed to constructor.

Some things to keep in mind

  • stdClass is used for objects - this is default for json_decode. This is to easily check where the data was an object and where it was an array. For example, if empty object or empty array is passed as JSON and you denormalize to an array, there is no way to check what was the type of original data;
  • null values are treated the same as it would not be provided at all:
    • it will not throw InvalidItemTypeException if you provide null and some type was expected (even array or object);
    • it will throw MissingItemException even if you provide the value but it is null;
  • the object cannot be modified - setting or unsetting anything is not supported.

paysera/lib-object-wrapper 适用场景与选型建议

paysera/lib-object-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.66k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 paysera/lib-object-wrapper 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-10-10