adventureres/php-sdk-v1

v1.1 2017-05-08 18:43 UTC

README

此存储库包含开源PHP软件开发工具包,它为PHP开发者提供了一个简单的方式与AdventureRes API进行通信。完整的类参考可以在这里找到。

重要说明

这不是与AdventureRes API的即插即用商店集成。例如,它不能直接与WordPress一起使用。要使用此存储库,需要开发者创建商店界面并管理用户通过结账体验的路径。此存储库不包含设计资产、HTML、样式或JavaScript。

我们的目标是提供一个简单且一致的方式来在您的网站/应用程序与AdventureRes系统之间传递数据。此存储库提供的是一个可以在各种基于PHP的内容管理系统和PHP平台上使用的PHP类。它处理输入验证、API端点请求、响应格式化、错误处理和浏览器会话。这减少了大部分潜在的错误,并解决了与直接使用AdventureRes API相关的大多数常见需求,同时保留了全部功能。

简单来说,此存储库不是一个电子商务商店——它只是中间步骤。我们构建了所有配套部件,但需要一些组装。

安装

composer require adventureres/php-sdk-v1

用法

注意:此SDK需要PHP版本5.4或更高。

设置SDK

要设置SDK,传入所需的配置参数

$advRes = new AdventureRes\AdventureRes(
    $baseDomain = 'http://reservations.domain.com',
    $apiKey     = '12345abcde', // <- Comes from your AdventureRes installation
    $username   = 'theuser', 
    $password   = 'opensesame',
    $location   = 10
);

模型

传递到API并从中检索的所有数据都应该在模型中。当调用API时,SDK将验证所有输入模型,但开发者将负责验证输出模型。

创建输入模型和设置属性

可以通过使用静态populateModel方法或使用类构造函数来创建模型。

use AdventureRes\Models\Input\ServiceAvailabilityInputModel;

// Static declaration:
$input = ServiceAvailabilityModel::populateModel([
    'AdultQty'   => 2,
    'YouthQty'   => 0,
    'Units'      => 0,
    'StartDate'  => '06/01/2016',
    'Display'    => 'ITEM'
]);

// Class declaration with attributes passed via the constructor:
$input = new ServiceAvailabilityModel([
    'AdultQty'   => 2,
    'YouthQty'   => 0,
    'Units'      => 0,
    'StartDate'  => '06/01/2016',
    'Display'    => 'ITEM'
]);

也可以通过使用setAttribute方法在模型实例化后设置属性

$input->setAttribute('AdultQty', 5);

验证模型和获取错误

要验证模型,请使用isValid方法。如果模型无效,您还可以使用getErrors方法检索错误。

注意:必须在getErrors之前运行isValid

if (! $model->isValid()) {
    // Get errors as an array
    $errors = $model->getErrors();

    // Get errors as a concatenated string
    $errorString = $model->getErrorsAsString();
}

从模型属性获取值

可以通过几种不同的方式访问模型属性

// Gets an array of all attributes and their values
$attributes = $model->getAttributes();

// Gets the value of one attribute
$attribute = $model->getAttribute('AdultPrice');

// Gets the value of one attribute using magic method
$attribute = $model->AdultPrice;

与API端点通信

SDK的分割方式与API相同——通过服务方法、包方法、预订方法和客户方法

$serviceAvailability = $advRes->service()->getServiceAvailability($inputModel);
$package = $advRes->package()->getPackage($inputModel);
$costSummary = $advRes->reservation()->getCostSummary($inputModel);
$newCustomer = $advRes->customer()->createCustomer($inputModel);

完整示例

use AdventureRes\AdventureRes;
use AdventureRes\Models\Input\ServiceAvailabilityInputModel;

$advRes = new AdventureRes($config['baseDomain'], $config['apiKey'], $config['username'], $config['password'], $config['location']);

try {
    $input = ServiceAvailabilityInputModel::populateModel([
        'ServiceId' => 123,
        'AdultQty'  => 2,
        'YouthQty'  => 0,
        'Units'     => 0,
        'StartDate' => '06/01/2016',
        'Display'   => 'ITEM'
    ]);

    $serviceAvailability = $advRes->service()->getServiceAvailability($input);

    if (! $serviceAvailability->isValid()) {
        throw new \Exception($serviceAvailability->getErrorsAsString());
    }

} catch (AdventureRes\Exceptions\AdventureResSDKException $ex) {
    return $ex->getMessage();
}