psx/data
Composer 安装命令:
composer require psx/data
包简介
Data processing library to read and write POPOs in different formats
关键字:
README 文档
README
Data processing library which helps to read and write data to and from POPOs in different formats.
Usage
The following example showcases how you could read/write a complex model.
// create processor $processor = new Processor(Configuration::createDefault()); // example json data which we want to parse in our model $in = <<<JSON { "id": 1, "title": "Lorem ipsum", "author": { "id": 1, "name": "Foo", "email": "foo@bar.com", }, "comments": [{ "id": 1, "author": { "id": 1, "name": "Foo", "email": "foo@bar.com", }, "text": "Lorem ipsum" },{ "id": 2, "author": { "id": 1, "name": "Foo", "email": "foo@bar.com", }, "text": "Lorem ipsum" }], "date": "2016-03-28T22:40:00Z" } JSON; // reads the json data into a custom model class $model = $processor->read(News::class, Payload::json($in)); // the model can be used to get or set data $model->getAuthor()->getName(); $model->getComments()[0]->getText(); // writes the model back to json $out = $processor->write(Payload::json($model)); // model classes class News { private ?int $id = null; private ?string $title = null; protected ?Author $author = null; /** * @var array<Comment>|null */ private ?array $comments = null; #[Format('date-time')] private ?string $date; // getter/setter implementations removed for readability } class Author { private ?int $id = null; private ?string $name = null; private ?string $email = null; // getter/setter implementations removed for readability } class Comment { private ?int $id = null; private ?Author $author = null; private ?string $text = null; // getter/setter implementations removed for readability }
Formats
The library supports different reader and writer classes to produce different data formats. If you want to read a specific format you can provide the content type of the data. I.e. if you want read XML you could use the following payload:
$payload = Payload::create($in, 'application/xml');
The processor uses a reader factory to obtain the fitting reader for a specific content type. In this case it would use the XML reader. The reader factory can be easily extended with different reader classes to support other data formats.
$configuration->getReaderFactory()->addReader(new Acme\Reader(), 32);
In order to produce a payload from an incoming HTTP request you simply have to set the body as data and the content type from the header. How you access this data depends on the HTTP interface. For an PSR-7 request you could use:
$payload = Payload::create( (string) $request->getBody(), $request->getHeaderLine('Content-Type') );
On the other hand if you want to write data as response you would use:
$payload = Payload::create( $model, $request->getHeaderLine('Accept') );
The writer factory can also be extended with custom writer implementations.
$configuration->getWriterFactory()->addWriter(new Acme\Writer(), 64);
Transformations
Each reader class returns the data in a form which can be easily processed. I.e.
the json reader returns a stdClass produced by json_decode and the xml
reader returns a DOMDocument. To unify the output we use transformation
classes which take the output of a reader and return a normalized format. I.e.
for xml content we apply by default the XmlArray transformer which transforms
the DOMDocument. So you can use a transformer if you directly want to work
with the output of the reader.
In case you want to validate incoming XML data after a XSD schema you could use
the XmlValidator transformer:
$payload = Payload::xml($data); $payload->setTransformer(new XmlValidator('path/to/schema.xsd')); $model = $processor->read(News::class, $payload);
Exporter
If you write data you can set as payload an arbitrary object. We use an exporter class to return the actual data representation of that object. By default the exporter reads also the psx/schema attributes so you can use the same model for incoming and outgoing data. But it is also possible to use different classes. I.e. you could create model classes using the psx/schema attributes only for incoming data and for outgoing data you could use the JMS exporter in case you have already objects which have these annotations.
If you have another way how to extract data of an object (i.e. a toArray method which returns the available fields of the object) you can easily write a custom exporter.
psx/data 适用场景与选型建议
psx/data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 175.64k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「xml」 「data」 「processor」 「popo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psx/data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psx/data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psx/data 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Kinikit - PHP Application development framework MVC component
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 175.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 21
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-03-31