emilianozublena / sheetsu-php
Sheetsu API 的 PHP 5.6 库 http://sheetsu.com
Requires
- php: >=5.6
- curl/curl: ^1.5
Requires (Dev)
- phpunit/phpunit: 5.7
This package is not auto-updated.
Last update: 2024-09-16 00:29:38 UTC
README
安装
Sheetsu PHP 库可以通过 Composer 获取。
您可以编辑 composer.json 文件,或者直接在终端运行此命令
composer require emilianozublena/sheetsu-php
用法
实例化 Sheetsu 客户端
您需要实例化主 Sheetsu 对象并给出 SheetID,您可以在 Sheetsu 控制台 上找到此 URL。请记住使用 composer 的自动加载。
require('vendor/autoload.php'); use Sheetsu\Sheetsu; $sheetsu = new Sheetsu([ 'sheetId' => 'sheetId' ]);
如果您已为 API 启用 HTTP Basic 认证,您应在此处传递 key
和 secret
,如下所示
require('vendor/autoload.php'); use Sheetsu\Sheetsu; $sheetsu = new Sheetsu([ 'sheetId' => 'sheetId', 'key' => 'key', 'secret' => 'secret' ]);
初始化库
如果需要,您可以在创建后重新初始化(或初始化)库
# Initialize after creation $sheetsu = new Sheetsu(); $sheetsu->initialize([ 'sheetId' => 'sheetId', 'key' => 'key', 'secret' => 'secret' ])
集合-模型
Sheetsu PHP 库包含一个 集合抽象数据类型 的简单实现。
模型是集合的单位(在这种情况下,每个模型表示给定表的行)。
您可以使用类似的方式操作 Sheetsu 客户端(对于 CRUD 操作),而不是给 Sheetsu 客户端传递数组
$collection = new Collection(); $collection->addMultiple([ Model::create(['name' => 'John']), Model::create(['name' => 'Steve']) ]); $response = $sheetsu->create($collection);
集合和模型是每次调用 API 都将获得的两个对象。
创建
要将数据添加到 Google 表格,发送一个数组、一个数组数组,或者更简单地说,使用模型或集合
# Adds single row from array $sheetsu->create(['name' => 'John']); # Adds multiple rows from array $sheetsu->create([ ['name' => 'John'], ['name' => 'Steve'] ]); # Adds single row from Model $sheetsu->create(Model::create(['name' => 'John'])); # Adds multiple rows from Collection $collection = new Collection(); $collection->addMultiple([ Model::create(['name' => 'John']), Model::create(['name' => 'Steve']) ]); $response = $sheetsu->create($collection);
调用后返回一个响应对象。
读取
读取整个表
$response = $sheetsu->read(); $collection = $response->getCollection();
读取函数允许两个参数
limit
- 限制结果数量offset
- 从第 N 条记录开始
# Get first two rows from sheet starting from the first row $response = $sheetsu->read(2, 0); $collection = $response->getCollection();
搜索
要获取符合搜索条件的行,请传递一个包含条件的数组
# Get all rows where column 'id' is 'foo' and column 'value' is 'bar' $response = $sheetsu->search([ 'id' => 'foo', 'value' => 'bar' ]); $collection = $response->getCollection(); # Get all rows where column 'First name' is 'Peter' and column 'Score' is '42' $response = $sheetsu->search([ 'First name' => 'Peter', 'Score' => '42' ]); $collection = $response->getCollection(); # Get first two row where column 'First name' is 'Peter', # column 'Score' is '42' from sheet named "Sheet3" $response = $sheetsu->search([ 'First name' => 'Peter', 'Score' => '42' ], 2, 0); $collection = $response->getCollection(); # Get first two row where column 'First name' is 'Peter', # column 'Score' is '42' from sheet named "Sheet3" # with ignore case $response = $sheetsu->search([ 'First name' => 'Peter', 'Score' => '42' ], 2, 0, true); $collection = $response->getCollection();
更新
要更新行,请传递列名及其值(用于查找行)以及要更新的数据数组或模型。
# Update all columns where 'name' is 'Peter' to have 'score' = 99 and 'last name' = 'Griffin' $model = Model::create(['score' => '99', 'last name' => 'Griffin']); $response = $sheetsu->update('name', 'Peter', $model);
默认情况下,发送 PATCH 请求,只更新传递给方法的集合中的值。要发送 PUT 请求,请传递第四个参数为 true
。有关 PUT 和 PATCH 之间差异的更多信息,请参阅 sheetsu 文档。
删除
要删除行,请传递列名及其值(用于查找行)。
# Delete all rows where 'name' equals 'Peter' $response = $sheetsu->delete('name', 'Peter');
更改活动表
如果您需要更改正在处理的表,可以通过使用 sheet 函数并传递新的 Sheetsu ID 来实现。
# Change active sheetsu to 'THIS' $sheetsu->sheet('THIS')
您还可以将此函数与其他函数链接起来,如下所示
# Change active sheetsu to 'THIS' $sheetsu->sheet('THIS')->read();
返回到使用整个电子表格
如果您需要使用整个电子表格,则非常简单
# Back to whole spreadsheet $sheetsu->whole()
您还可以将此函数与其他函数链接起来,如下所示
# Back to whole spreadsheet $sheetsu->whole()->read();
响应、连接和错误处理
Sheetsu PHP 库通过 Connection 类处理连接。此类使用 cURL 进行连接,并使用 Response 类作为返回值。Response 对象负责提供集合、模型或错误(或最后一次调用的任何其他响应)。错误处理也是通过 Response 对象进行的(Response 使用 ErrorHandler 类来抽象 try/catch 块,并与 ErrorException php 类紧密耦合)。
$response = $sheetsu->read(); #if you need only the error messages, you can get the errors like this $errors = $response->getErrors(); $firstError = $response->getError(); #if you need to get the exceptions thrown, do it like this. $exceptions = $response->getExceptions(); $firstException = $response->getException();
使用 PHPUnit 进行单元测试
此库的代码覆盖率超过 97%。一些测试尚未使用模拟对象,这在我们待办事项列表中,并希望我们能这样做。测试已准备好与 PHPUnit 一起使用,测试套件通过 XML 配置,因此您只需在您此存储库的分支中执行 PHPUnit 即可,如下所示
./vendor/bin/phpunit
待办事项
- 定义和实现 ErrorHandler 以利用最终用户处理 HTTP 状态码
- 使此存储库作为 Composer 包工作
- 创建至少 80% 覆盖率的单元测试(目前为 91%)
- 将 ignore_case 添加到搜索中
- 添加一种管理活动工作表的方式
- 为测试创建模拟对象
- 使类不可知,以便集合和模型类可以替换为其他类(或者可能为 Eloquent 这样的 ORM)