psx / cloudevents
生成和消费云事件
v1.0.2
2024-05-10 16:33 UTC
Requires
- php: >=8.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^5.0
This package is auto-updated.
Last update: 2024-09-10 17:12:59 UTC
README
关于
帮助生成和消费云事件的库。 https://github.com/cloudevents/spec
生成
要创建一个 CloudEvent 对象,可以使用构建器,例如:
<?php $event = (new Builder()) ->withType('com.example.someevent') ->withSource('/mycontext') ->withId('1234-1234-1234') ->withTime(new \DateTime('2018-04-05T17:31:00Z')) ->withDataContentType('application/json') ->withData(['foo' => 'bar']) ->withExtension('bar', 'foo') ->build();
然后可以将事件 JSON 序列化,生成以下 JSON 输出
{ "specversion": "1.0", "type": "com.example.someevent", "source": "\/mycontext", "id": "1234-1234-1234", "time": "2018-04-05T17:31:00+00:00", "datacontenttype": "application\/json", "data": { "foo": "bar" }, "bar": "foo" }
消费
要从原始 JSON 数据创建 CloudEvent 对象,可以使用解析器
<?php $json = '{ ... }'; $event = Parser::parse(json_decode($json)); assert('1.0' === $event->getSpecVersion()); assert('com.example.someevent' === $event->getType()); assert('/mycontext' === $event->getSource()); assert('1234-1234-1234' === $event->getId()); assert($event->getTime() instanceof \DateTimeInterface::class); assert('Thu, 05 Apr 2018 17:31:00 +0000' === $event->getTime()->format('r')); assert('application/json' === $event->getDataContentType()); assert(['foo' => 'bar'] === $event->getData()); assert(['bar' => 'foo'] === $event->getExtensions());