tourware/sdk-php

用于 Tourware API 的 PHP SDK。

v0.5.9 2023-01-31 09:32 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:55 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

官方 PHP 客户端用于 tourware© REST-API。其目标是提供一个统一的平台,以支持所有与 tourware© 相关的 PHP 代码。

安装

通过 Composer

$ composer require tourware/sdk-php

使用方法

您应该在您的应用程序中使用 Composer 自动加载器来自动加载您的依赖项。以下所有示例都假设您已将其包含在您的文件中

use Tourware\Client;

require 'vendor/autoload.php';

以下是如何使用 SDK 获取 旅行 的示例

// First, instantiate the SDK with your x-api-key API credentials
$client = Client::create(xApiKey: 'xxxxxxx'); // For staging

$client = Client::create(xApiKey: 'xxxxxxx', staging: false); // For production

// Now, get a Travel by it's id 
$travel = $client->travels()->find('60feacb365f5f1002750c2b2');

实体客户端

每个 Tourware 实体都有自己的客户端,负责处理 HTTP 操作。每个实体客户端都可以通过使用 Tourware\Client 外观来访问。

例如,有多种方式可以检索旅行的客户端

use Tourware\Entities\Travel;

// By using helper method
$travel = $client->travel()->find();

// By using the entity class
$travel = $client->entity(new Travel)->find();

// Or by using the raw endpoint 
$travel = $client->raw('travels')->find();

CRUD

您可以使用 Tourware\Client 执行对您的记录的 CRUD 操作。

// Create a new travel
$response = $client->travel()->create([...]);

// Find a travel
$response = $client->travel()->find('bba0b42e4699');

// Update an existing travel
$response = $client->travel()->update('bba0b42e4699', [...]);

// List all travels
$response $client->travel()->list(offset: 0, limit: 50);

// List specific travel
$response = $client->travel()->delete('bba0b42e4699');

查询

查询构建器提供了一系列方法,帮助您过滤实体。

点符号

当您检索查询结果时,您可以通过点符号访问它们。

例如。

$travels = $client->travel()->query()->filter('title')->contains('kenya')->get();

//The the first name for the resposible user
$travels->get('records.0.responsibleUser.firstname');

筛选

假设您想筛选您的 旅行,并只获取包含单词 "kenya" 的记录。

您可以通过以下方式使用查询构建器来完成此操作

use Tourware\Operator\Contains;

// By using the query builder
$travels = $client->travel()->query()->filter('title')->contains('kenya')->get();

// By using the filter class
$travels = $client->travel()->query()->addFilter(new Contains('title', 'kenya'))->get();

// By using raw filter
$client->travel()->query()->addRawFilter(['property' => 'title', 'operator' => 'contains', 'value' => 'kenya'])->get();

排序

查询构建器还允许您对检索到的记录进行排序。

use Tourware\Orders\Asc;

// By using the sort builder
$travels = $client->travel()->query()->sort('id')->asc()->get();

// By using the sort class
$travels = $client->travel()->query()->addSort(new Asc('id'))->get();

// By using raw sort
$travels = $client->travel()->query()->addRawSort(['property' => 'id', 'direction' => 'asc'])->get();

偏移量 & 限制

通常情况下,您想对结果进行分页。因此,查询构建器还提供了 offsetlimit 方法。

以下是检索旅行块的方法

$travels = $client->travel()->query()->offset(5)->limit(20)->get();

自定义端点

在许多情况下,您可能想向 Tourware 发送 自定义 HTTP 请求。您可以通过在 Client 类中使用 custom 方法来实现这一点。

$relations = $client->custom("/relations/getRelations/travels/bba0b42e4699", 'GET')->call();

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

测试

$ composer test

贡献

请参阅 CONTRIBUTINGCODE_OF_CONDUCT 了解详细信息。

安全性

如果您发现任何与安全性相关的问题,请通过电子邮件 security@tourware.com 而不是使用问题跟踪器来报告。

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件