byte5digital/laravel-harvest

此包已被废弃且不再维护。未建议替代包。

Laravel Harvest API 的包装器。

v2.2.3 2018-06-11 07:02 UTC

README

Software License Travis StyleCI Total Downloads

这是一个针对 harvest API 的小型包装器,旨在使您的操作更加便捷。

目前,此包只能用来从 harvest 接收数据,但不能创建内容。

安装

Laravel 版本 5.6+

composer require byte5digital/laravel-harvest

Laravel 版本 5.5

composer require byte5digital/laravel-harvest:2.0

如果您想将 harvest 数据存储到数据库中,请在 harvest 配置中将 uses_database 设置为 true 并发布迁移文件。

php artisan vendor:publish --provider="Byte5\LaravelHarvest\LaravelHarvestServiceProvider"

如果您只想发布配置文件,请添加: --tag="config"

使用方法

您可以使用 Harvest Facade 或从 ioc 容器中解析 ApiManager

// resolve out of ioc container
$harvest = app()->make('harvest');

获取数据

每个 API 调用看起来都像这样

$harvest->model_name->get();
Harvest::model_name()->get();

您可以使用 get()find($id) 获取结果。

// getting all clients
$harvest->clients->get();
Harvest::clients()->get();

// getting a client with id of 12345
$harvest->clients->find(12345);
Harvest::clients()->find(12345);

// getting all expenses
$harvest->expenses->get();
Harvest::expenses()->get();

// getting an expense with id of 12345
$harvest->expenses->find(12345);
Harvest::expenses()->find(12345);

//... you get the idea

有些情况有不同的方法,因为它们依赖于其他对象。

// get all user_assignments with the project id of 12345
$harvest->userAssignments->fromProject(12345)->get()

// get an user_assignments with the id of 12345 which belongs to the project id of 4567
$harvest->userAssignments->fromProject(4567)->find(12345)

// get all estimate messages with the estimate id of 12345
$harvest->estimateMessages->fromEstimate(12345)->get();

// get an estimate messages with the id of 12345 which belongs to the estimate id of 4567
$harvest->estimateMessages->fromEstimate(4567)->find(12345)

异常列表

  • EstimateMessage
  • InvoiceMessage
  • InvoicePayment
  • ProjectAssignment
  • TaskAssignment
  • UserAssignment

处理响应

API 响应可以是 jsoncollection分页集合,后者基本上是 jsoncollection 的组合。

// receiving some response
$respose = Harvest::users()->get();

// convert result to json
$result->toJson();

// convert result to collection
$result->toCollection();

// convert result to paginated collection
$result->toPaginatedCollection();

处理分页

默认情况下,harvest 返回包含最多 100 条记录的 JSON 响应。如果您想限制结果,应使用 limit()。如果您想获取特定页面的结果,请在 findget 之前使用 fromPage()

// get results from page 10
$harvest->projects()->page(10)->get()

// limit result entries to 50
$harvest->projects()->limit(50)->get();

// limit result entries and get results from page 10
$harvest->projects()->limit(50)->page(10)->get();

如果您的 harvest 条目超过 100 条,并且只想获取下一页或前一页的结果,可以在结果上调用 next() 以获取下一组 100 个结果。

// get next result page
$result = $result->next();

// get previous result page
$result = $result->previous();

附加参数

向请求中添加附加参数也是可能的。 并非所有参数都受支持

支持参数

  • is_active => active()

某些 API 调用允许您使用不同的参数

    // get all invoices with a state of 'draft'
    $harvest->invoices()->state('draft')->get();
    
    // get all invoices with a client_id of '123445'
    $harvest->invoices()->client('123445')->get();
    
    // get all invoices with a project_id of '123445'
    $harvest->invoices()->project('123445')->get();
    
    // get all invoices which were updated since '2018-01-12'
    // => does also accept other formats like '12.01.2018'
    $harvest->invoices()->updatedSince('2018-01-12')->get();
    
    // get all invoices with an issue_date >= '2018-01-01'
    $harvest->invoices()->from('2018-01-01')->get();
    
    // get all invoices with an issue_date <= '2018-01-01'
    $harvest->invoices()->to('2018-01-01')->get();

加载外部关系

当您查询具有外部关系的任何对象时,您可能想查看 loadExternal() 方法来本地加载这些关系。

// loading all external relations of one expense model
// by default if you have enabled `uses_database` in the config
// all external relations are saved to the database.
$expense->loadExternal();

// load all external relations without saving to db
$expense->loadExternal('*', false);

// load only user and client relations
$expense->loadExternal(['user', 'client']);

待办事项

  • 更新/创建记录
  • 改进测试

测试

使用以下命令运行测试

vendor/bin/phpunit

升级

请参阅UPGRADING以获取详细信息。

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请发送电子邮件至kkoenig@byte5.de,而不是使用问题跟踪器。

许可

MIT许可证(MIT)。请参阅许可文件以获取更多信息。