streamline / openactive-models
openactive/models库的分支。查看原始库:https://github.com/openactive/models-php
Requires
- php: ^5.6 | ^7.0 | ^8.0
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^5.7 | ^9.0
- squizlabs/php_codesniffer: ^3.5
- dev-master
- v0.13.0
- v0.12.0
- v0.10.1
- 0.10.0
- v0.9.0
- 0.2.0
- 0.1.0
- dev-allow-abstract-data-modifications
- dev-temporary-string-price-fix
- dev-fix-string-prices
- dev-add-php8-support
- dev-remove-deprecated-string-offset
- dev-bugfix/59-rpde-body-serialize
- dev-bugfix/55-type-checks-too-strict
- dev-fix-schema-context
- dev-fix-documentation
- dev-remove-composer-lock
- dev-add-order-rpde-support
- dev-improve-type-errors
- dev-fix-rpde-serialization
This package is auto-updated.
Last update: 2021-07-28 15:25:34 UTC
README
OpenActive机会和预订规范的PHP模型
OpenActive旨在为PHP、Ruby和.NET语言中定义的所有类提供模型文件。此存储库旨在用于PHP文件;有关Ruby和.NET实现,请参阅Ruby和.NET。
目录
要求
本项目需要PHP >=5.6。虽然大多数功能应该可以在PHP 5.4中工作,但某些功能(尤其是DateTimeZone的偏移量解析)将无法使用该版本的PHP(有关更多信息,请参阅DateTimeZone PHP文档)。
安装
要使用Composer安装,请在终端中运行
composer require openactive/models
使用
此包提供了OpenActive规范的PHP模型。
它还提供了一组schema.org规范的模型。
最后,它提供了一组处理OpenActive的RPDE数据流的类。
模型
模型包含在\OpenActive\Models
命名空间下。
您可以通过传递一个关联数组来实例化一个新模型,其中键是属性名称,值是属性值。
例如,从您的PHP应用程序中运行
// Make sure you use the right namespace for your models use OpenActive\Models\OA\SessionSeries; use OpenActive\Models\OA\Place; use OpenActive\Models\OA\GeoCoordinates; use OpenActive\Models\OA\Concept; use OpenActive\Models\OA\Organization; use OpenActive\Models\OA\Offer; $sessionSeries = new SessionSeries([ "name" => "Virtual BODYPUMP", "description" => "This is the virtual version of the original barbell class, which will help you get lean, toned and fit - fast", "startDate" => "2017-04-24T19:30:00-08:00", "endDate" => "2017-04-24T23:00:00-08:00", "location" => new Place([ "name" => "Raynes Park High School, 46A West Barnes Lane", "geo" => new GeoCoordinates([ "latitude" => 51.4034423828125, "longitude" => -0.2369088977575302, ]) ]), "activity" => new Concept([ "id" => "https://openactive.io/activity-list#5e78bcbe-36db-425a-9064-bf96d09cc351", "prefLabel" => "Bodypump™", "inScheme" => "https://openactive.io/activity-list" ]), "organizer" => new Organization([ "name" => "Central Speedball Association", "url" => "http://www.speedball-world.com" ]), "offers" => [new Offer([ "identifier" => "OX-AD", "name" => "Adult", "price" => 3.3, "priceCurrency" => "GBP", "url" => "https://profile.everyoneactive.com/booking?Site=0140&Activities=1402CBP20150217&Culture=en-GB" ])], ]);
请注意,在创建新模型时始终存在类型强制。
例如,在上面的示例中向target
属性提供string
将导致抛出\OpenActive\Exception\InvalidArgumentException
。
提供了一组所有属性的获取器和设置器。设置器也存在类型强制。
OpenActive
OpenActive模型包含在\OpenActive\Models\OA
命名空间下。
要实例化一个新模型,请参阅模型部分,确保您从模型使用正确的命名空间。
Schema.org
Schema.org模型包含在\OpenActive\Models\SchemaOrg
命名空间下。
要实例化一个新模型,请参阅模型部分,确保您从模型使用正确的命名空间。
RPDE
RpdeItem 和 RpdeBody 是生成 RPDE 数据源页面时需要使用的主要类。
数据源项
RpdeItem 用于创建页面上的每个单独项。它包括一个数据属性,该属性应为 OA 模型的实例,以及元数据(id、修改时间、状态和类型)。生成这些模型和元数据的任务留给了每个应用程序开发者。
例如,一个会议系列集合
use OpenActive\Rpde\RpdeItem; $feedItems = [ new RpdeItem([ "Id" => "2", "Modified" => 4, "State" => RpdeState::UPDATED, "Kind" => RpdeKind::SESSION_SERIES, "Data" => $sessionSeries2, ]), new RpdeItem([ "Id" => "1", "Modified" => 5, "State" => RpdeState::DELETED, "Kind" => RpdeKind::SESSION_SERIES, ]), ];
数据源页面
然后使用 RpdeBody 来包装一个项集合,并提供来自 RPDE 页面的预期许可证和下一个条目。为了保持页面有效并创建正确的下一个链接,请使用 RpdeBody::createFromNextChangeNumber
或 RpdeBody::createFromModifiedId
从 RpdeItem
数组创建 RPDE 页面数据源(构造函数已被设置为私有)。
RpdeBody::createFromNextChangeNumber
将检查所有数据源项确实在提供的 $changeNumber
参数之后。它将根据最新数据源项的修改值和提供的 $feedBaseUrl
参数构建下一个链接。
例如:
use OpenActive\Rpde\RpdeBody; $feedPage = RpdeBody::createFromNextChangeNumber( 'https://www.example.com/rpde-feeds/session-series', # $feedBaseUrl 0, # $changeNumber, $feedItems ); $feedPage->getNext(); # 'https://www.example.com/rpde-feeds/session-series?afterTimestamp=5&afterId=2'
RpdeBody::createFromModifiedId
将检查所有数据源项确实在提供的 $id
和 $modified
参数之后。它将根据最新数据源项的 id 和修改值以及提供的 $feedBaseUrl
参数构建下一个链接。
例如:
use OpenActive\Rpde\RpdeBody; $feedPage = RpdeBody::createFromModifiedId( 'https://www.example.com/rpde-feeds/session-series', # $feedBaseUrl 0, # $id 0, # $modified, $feedItems ); $feedPage->getNext(); # 'https://www.example.com/rpde-feeds/session-series?afterChangeNumber=5'
要覆盖默认许可证
$feedPage->setLicense('https://www.example.com/my-licence/v2.0');
序列化数据源页面
最后,可以使用 Serialize RpdeBody::serialize($feedPage)
将数据源页面序列化,这将还将负责将每个数据源项的数据属性序列化为 JSON-LD。
$jsonFeedPage = RpdeBody::serialize($feedPage);
有关 RPDE 数据源的更多信息,请参阅 OpenActive 发布数据指南 和 RPDE 规范。
枚举
每个枚举都由一个包含每个可用值的常量的类表示。
例如,向日程表中添加星期几
use OpenActive\Models\OA\Schedule; use OpenActive\Enums\SchemaOrg\DayOfWeek; new Schedule([ "scheduledEventType" => "Event", "startTime" => "12:00:00", "endTime" => "14:00:00", "byDay" => [ new DayOfWeek\Monday, new DayOfWeek\Wednesday, new DayOfWeek\Friday ], ... ]);
序列化
此包提供对 模型 和 \OpenActive\Rpde\RpdeBody
对象的 JSON-LD 序列化/反序列化支持。
serialize($obj, $prettyPrint = false)
返回给定对象 $obj
的 JSON-LD 字符串表示。
可用的附加参数 $prettyPrint
可以返回一个人类可读的 JSON-LD 字符串。
以下是一个示例,使用上面定义的 \OpenActive\Models\OA\SessionSeries
use OpenActive\Models\OA\SessionSeries; echo SessionSeries::serialize($sessionSeries, true);
将输出
{ "@context": [ "https:\/\/openactive.io\/", "https:\/\/openactive.io\/ns-beta" ], "type": "SessionSeries", "name": "Virtual BODYPUMP", "description": "This is the virtual version of the original barbell class, which will help you get lean, toned and fit - fast.", "startDate": "2017-04-24T19:30:00-08:00", "endDate": "2017-04-24T23:00:00-08:00", "location": {...}, "activity": {...}, "organizer": {...}, "offers": [...] }
请注意:目前,序列化输出中只渲染了 OpenActive @context
。此包的将来版本可能允许包含更多和/或不同的 @context
。
deserialize($data)
从给定的 JSON-LD 表示中返回一个对象。
$data
参数可以是 JSON-LD 字符串,也可以是关联数组,例如作为 json_encode($string, true)
的结果。
例如:
use OpenActive\Models\OA\Action; $jsonLd = '{"@context": ["https:\/\/openactive.io\/","https:\/\/openactive.io\/ns-beta"],"type": "Action","name": "Book","target": {"type": "EntryPoint","encodingType": "application\/vnd.openactive.v1.0+json","httpMethod": "POST","type": "EntryPoint","url": "https:\/\/example.com\/orders"}}'; $action = Action::deserialize($jsonLd); var_dump($action);
将产生
object(OpenActive\Models\OA\Action)#3 (24) {
["name":protected]=>
string(4) "Book"
["target":protected]=>
object(OpenActive\Models\OA\EntryPoint)#2 (20) {
["encodingType":protected]=>
string(36) "application/vnd.openactive.v1.0+json"
["httpMethod":protected]=>
string(4) "POST"
["urlTemplate":protected]=>
NULL
["actionApplication":protected]=>
NULL
["application":protected]=>
NULL
["actionPlatform":protected]=>
NULL
["contentType":protected]=>
NULL
["identifier":protected]=>
NULL
["name":protected]=>
NULL
["description":protected]=>
NULL
["sameAs":protected]=>
NULL
["url":protected]=>
string(26) "https://example.com/orders"
["image":protected]=>
NULL
["additionalType":protected]=>
NULL
["subjectOf":protected]=>
NULL
["mainEntityOfPage":protected]=>
NULL
["potentialAction":protected]=>
NULL
["disambiguatingDescription":protected]=>
NULL
["alternateName":protected]=>
NULL
["id":protected]=>
NULL
}
["result":protected]=>
NULL
["startTime":protected]=>
NULL
["actionStatus":protected]=>
NULL
["agent":protected]=>
NULL
["endTime":protected]=>
NULL
["instrument":protected]=>
NULL
["participant":protected]=>
NULL
["object":protected]=>
NULL
["error":protected]=>
NULL
["location":protected]=>
NULL
["identifier":protected]=>
NULL
["description":protected]=>
NULL
["sameAs":protected]=>
NULL
["url":protected]=>
NULL
["image":protected]=>
NULL
["additionalType":protected]=>
NULL
["subjectOf":protected]=>
NULL
["mainEntityOfPage":protected]=>
NULL
["potentialAction":protected]=>
NULL
["disambiguatingDescription":protected]=>
NULL
["alternateName":protected]=>
NULL
["id":protected]=>
NULL
}
贡献
安装
请注意: Composer 用于依赖管理。
git clone https://github.com/openactive/models-php.git
cd models-php
composer install
运行测试
使用 PHPUnit 5.7 来运行测试。
要运行整个测试套件
./vendor/bin/phpunit
如果您想以详细模式运行整个测试套件
./vendor/bin/phpunit --verbose
您也可以通过指定要执行测试的类的相对路径来运行测试套件的某个部分
./vendor/bin/phpunit --verbose tests/Unit/RpdeTest.php
有关 PHPUnit 可用的命令的更多信息,请参阅 他们的文档
更新模型
在 UPDATING.md 中提供了指南