adventureres / php-sdk-v1
AdventureRes SDK for PHP
Requires
- php: >=5.4.0
- respect/validation: ^1.1
Requires (Dev)
- guzzlehttp/guzzle: ~5.0
- mockery/mockery: ~0.8
- phpunit/phpunit: ~4.0
- dev-master / 5.x-dev
- v1.1.x-dev
- v1.1
- v1.0.x-dev
- v1.0.1
- v1.0
- dev-feat/remove-fees
- dev-feat/insert-customer
- dev-feat/save-as-quote
- dev-feat/package-remove
- dev-feat/package-add
- dev-feat/package-display
- dev-feat/package-availability
- dev-feat/GroupList
- dev-cw_changes-for-ee
- dev-bw_reservation-service
- dev-bw_service-service
- dev-bw_service-models
- dev-bw_authentication
- dev-bw_persistent-data
This package is not auto-updated.
Last update: 2024-09-20 21:35:47 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(); }