xp-forge/address
Composer 安装命令:
composer require xp-forge/address
包简介
Creates objects from XML input streams while parsing them.
README 文档
README
Creates objects from XML input streams while parsing them. Yes, this still happens today 😉
Example
Given the following two value objects:
class Book { public $name, $author; public function __construct(string $name, Author $author) { $this->name= $name; $this->author= $author; } } class Author { public $name; public function __construct(string $name) { $this->name= $name; } }
...and this XML:
<?xml version="1.0" encoding="utf-8"?> <book> <name>A Short History of Nearly Everything</name> <author> <name>Bill Bryson</name> </author> </book>
...the following will map the XML to an object instance while reading it from the socket.
use util\address\{XmlStreaming, ObjectOf}; $socket= /* ... */ $stream= new XmlStreaming($socket); $book= $stream->next(new ObjectOf(Book::class, [ 'name' => fn($self) => $self->name= yield, 'author' => fn($self) => $self->author= yield new ObjectOf(Author::class, [ 'name' => fn($self) => $self->name= yield ?: '(unknown author)'; } ]) ]);
Creating values
Definitions are used to create structured data from the XML input. Here are all the implementations:
ValueOf
Simplemost version which is given a seed value, which it can modify through the given address functions.
use util\address\{XmlStreaming, ValueOf}; // Parse into string 'Tim Taylor' $stream= new XmlStreaming('<name>Tim Taylor</name>'); $name= $stream->next(new ValueOf(null, [ '.' => fn(&$self) => $self= yield, ]); // Parse into array ['More', 'Power'] $stream= new XmlStreaming('<tools><tool>More</tool><tool>Power</tool></tools>'); $name= $stream->next(new ValueOf([], [ 'tool' => fn(&$self) => $self[]= yield, ]); // Parse into map ['id' => 6100, 'name' => 'more power'] $stream= new XmlStreaming('<tool id="6100">more power</tool>'); $book= $stream->next(new ValueOf([], [ '@id' => fn(&$self) => $self['id']= (int)yield, '.' => fn(&$self) => $self['name']= yield, ]);
ObjectOf
Creates objects without invoking their constructors. Modifies the members directly, including non-public ones.
use util\address\{XmlStreaming, ObjectOf}; class Book { public $isbn, $name; } // Parse into Book(isbn: '978-0552151740', name: 'A Short History...') $stream= new XmlStreaming('<book isbn="978-0552151740"><name>A Short History...</name></book>'); $book= $stream->next(new ObjectOf(Book::class, [ '@isbn' => fn($self) => $self->isbn= yield, 'name' => fn($self) => $self->name= yield, ]);
RecordOf
Works with record classes, which are defined as being immutable and having an all-arg constructor. Modifies the named constructor arguments.
use util\address\{XmlStreaming, RecordOf}; class Book { public function __construct(private string $isbn, private string $name) { } public function isbn() { return $this->isbn; } public function name() { return $this->name; } } // Parse into Book(isbn: '978-0552151740', name: 'A Short History...') $stream= new XmlStreaming('<book isbn="978-0552151740"><name>A Short History...</name></book>'); $book= $stream->next(new RecordOf(Book::class, [ '@isbn' => fn(&$args) => $args['isbn']= yield, 'name' => fn(&$args) => $args['name']= yield, ]);
Iteration
Any Address instance can be iterated using the foreach statement. Using the data sequences library in combination with calling the value() method here's a way to parse an RSS feed's items:
use peer\http\HttpConnection; use util\data\Sequence; use util\Date; use util\address\{XmlStream, ObjectOf}; use util\cmd\Console; class Item { public $title, $description, $pubDate, $generator, $link, $guid; } $definition= new ObjectOf(Item::class, [ 'title' => fn($self) => $self->title= yield, 'description' => fn($self) => $self->description= yield, 'pubDate' => fn($self) => $self->pubDate= new Date(yield), 'generator' => fn($self) => $self->generator= yield, 'link' => fn($self) => $self->link= yield, 'guid' => fn($self) => $self->guid= yield, ]); $conn= new HttpConnection('https://www.tagesschau.de/xml/rss2/'); $stream= new XmlStream($conn->get()->in()); Sequence::of($stream->pointers('//channel/item')) ->map(fn($pointer) => $pointer->value($definition)) ->each(fn($item) => Console::writeLine('- ', $item->title, "\n ", $item->link)) ;
xp-forge/address 适用场景与选型建议
xp-forge/address 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-forge/address 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-forge/address 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-forge/address 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
bootstrap select field module implementing http://silviomoreto.github.io/bootstrap-select/
Yii2 module to manage website content. Kind of a CMS...
Codeurx Modular is simply a package for Laravel to help you create and manage modules.
User Module for the Attogram Framework
Info Module for the Attogram Framework
Contact Form Module for the Attogram Framework
统计信息
- 总下载量: 8.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-06-13

