we-simply-code/laravel-afas-rest-connector

本包将 AFAS REST API 集成到 Laravel 中

1.3.6 2024-05-06 13:25 UTC

README

本包以最小设置将 AFAS REST API 集成到 Laravel 中。

Latest Version on Packagist

目录

安装

composer require we-simply-code/laravel-afas-rest-connector

安装后发布配置文件

php artisan vendor:publish --provider="WeSimplyCode\LaravelAfasRestConnector\LaravelAfasRestConnectorServiceProvider"

在配置文件中,您可以添加不同的 AFAS profitServices 连接。默认情况下添加了一个连接。如果您只使用一个连接,请使用默认连接。如果您将使用多个连接,请使用您使用最多的默认连接,以便更容易。

在配置文件中的每个连接,您都需要添加连接器。每个连接包含 getConnectors 和 updateConnectors 数组。

将这些数组添加到您的连接器中。键可以是任何您想要的内容(您将使用它作为代码中连接器的名称),值必须是 AFAS profitService 中连接器的 ID。

将以下变量添加到您的 .env 文件中

AFAS_ENVIRONMENT="Your AFAS environment # here" //example: T11111AA
AFAS_TOKEN="Your AFAS token here" // example: <token><version>1</version><data>tokendata</data></token>

用法

我假设您知道 AFAS profitServices 的工作原理以及不同的连接器的作用。如果不是,请参阅 AFAS REST API 的文档:https://help.afas.nl/help/EN/SE/App_Cnr_Rest_Api.htm?query=rest

本包提供了一个外观(facade),可以轻松访问不同的连接器。在检索您的连接器后,您可以对其应用过滤器或将数据添加到其中,然后调用 execute() 方法以调用 AFAS profitServices。

GetConnector

使用 GetConnector 可以从 AFAS profitService 获取数据。getConnector 支持简单过滤或 JSON 过滤。配置好您连接的 getConnectors 后,可以使用如下方式

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will give you the "contacts" getConnector for the default connection
Afas::getConnector('contacts');

// This will give you the "contacts" getConnector for a different connection
Afas::getConnector('contacts', 'differentConnectionName');

过滤器

您可以在 getConnectors 上应用过滤器以获取更具体的数据。没有应用过滤器的特定顺序。您可以根据需要链式添加任意数量的过滤器,除了 take()skip() 过滤器。这些过滤器在每个请求中只能使用一次。默认情况下,此包将使用简单过滤器来检索结果。当您的查询变得复杂时,您可以选择启用 jsonFilter。有关简单过滤器和 jsonFilter 之间差异的官方文档:https://help.afas.nl/help/EN/SE/App_Cnr_Rest_GET.htm#o85263

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will give you a getConnector instance with the simple filter enabled
Afas::getConnector('contacts');

// This will give you a getConnector instance with the json filter enabled
Afas::getConnector('contacts', true);
Take

默认情况下,profitServices 返回 100 个结果。您可以通过添加 take() 过滤器来调整结果的数量。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will add the take filter to the connector with an amount of 10
Afas::getConnector('contacts')->take(10);
Skip

您可以通过添加 skip() 过滤器来跳过结果。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will add the skip filter to the connector with an amount of 10
Afas::getConnector('contacts')->skip(10);
SortOnField

根据任何字段对结果进行排序。默认情况下,结果将按升序排序,但通过添加额外的参数,您可以更改这一点。字段名称必须与 AFAS profitServices 中的字段名称完全相同。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will sort the results ascending by the field 'Name'
Afas::getConnector('contacts')->sortOnField('Name');

// Add true as second parameter to sort the results descending
// This will sort the results descending by the field 'Name'
Afas::getConnector('contacts')->sortOnField('Name', true);
Where

如果您想从 getConnector 获取特定结果,可以使用 where() 过滤器(记录必须匹配所有条件)。建议始终使用 getConnector 上的 where() 过滤器,因为它可以增强性能并仅提供您所需的结果。

所有 AFAS Profit 过滤器都适用于 where() 过滤器。过滤器在配置文件中列出。您可以使用它们的符号形式或文本形式。

默认情况下,getConnector使用简单过滤器。要启用jsonFilter,可以将true作为第二个参数传递给getConnector。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// The where() filter accepts the field type as first parameter, filter type as second and what the results should be filtered on as third
// Get only the contacts of type Person (simple filter)
Afas::getConnector('contacts')->where('type', '=', 'Person');

// Get only the contacts of type Person (json filter)
Afas::getConnector('contacts', true)->where('type', '=', 'Person');

// You can chain as much where filters as needed
// Get only the contacts from the Netherlands who are organizations
Afas::getConnector('contacts')
    ->where('country', '=', 'Netherlands')
    ->where('type', '=', 'Organization');
orWhere

您可以使用orWhere()过滤器向过滤器添加另一个where子句(记录必须至少匹配一个条件)。请查看官方文档了解其工作原理。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// The orWhere() filter accepts the field type as first parameter, filter type as second and what the results should be filtered on as third
// Get the contacts of type Person or Organization
Afas::getConnector('contacts')
    ->where('type', '=', 'Person')
    ->orWhere('type', '=', 'Organization');

// Get only the contacts from the Netherlands or Germany who are organizations (json filter)
Afas::getConnector('contacts', true)
    ->where('type', '=', 'Organization')
    ->where('country', '=', 'Netherlands')
    ->orWhere('type', '=', 'organization')
    ->where('country', '=', 'Germany');

有时简单过滤器不足以查询特定结果。在执行高级查询时启用jsonFilter。jsonFilter和简单过滤器并不总是返回相同的结果!

Execute

当您已相应配置连接器以调用AFAS profitServices时,使用execute()方法。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// Execute the call. This will retrieve 100 contacts from the AFAS profitServices
Afas::getConnector('contacts')->execute();

// Retrieve 10 contacts
Afas::getConnector('contacts')
    ->take(10)
    ->execute();

// Retrieve 100 contacts but skip the first
Afas::getConnector('contacts')
    ->skip(1)
    ->execute();

// Retrieve 10 contacts and skip the first
Afas::getConnector('contacts')
    ->skip(1)
    ->take(10)
    ->execute();

本包使用Laravel围绕Guzzle的包装来执行http调用。这意味着在调用execute()方法后,我们可以使用Laravel提供的其他方法来检查响应。示例

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will return the request status
Afas::getConnector('contacts')
    ->execute()
    ->status();

// This will return the response in JSON
Afas::getConnector('contacts')
    ->execute()
    ->json();

请查看Laravel HTTP客户端的文档:https://laravel.net.cn/docs/8.x/http-client#introduction

检查 where 过滤器

如果您想检查where过滤器的json,可以在配置getConnector后调用getJsonFilter()方法,而不是调用execute()方法。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// This will return the where filter in a json string
Afas::getConnector('contacts')
    ->take(10)
    ->where('Type', '=', 'Person')
    ->getJsonFilter();

此方法不会返回take、skip和sortOnField过滤器。

UpdateConnector

使用UpdateConnector,您可以插入、更新或删除AFAS profitService中的记录。您可以使用https://connect.afas.nl之类的工具来检查用于在AFAS profitService中操作数据的所需json对象,或者您可以在连接器上调用metaInfo()方法来检查字段。

插入和更新记录

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

$data = [
            "KnPerson" => [
                "Element" => [
                    "Fields" => [],
                ],
            ],
        ];
// Create a new record in AFAS profitService
Afas::updateConnector('person')->insert($data);

// Update an existing record in AFAS profitService
Afas::updateConnector('person')->update($data);

生成 URL

如果您想检查连接器生成的URL,可以在配置连接器后调用getUrl()方法,而不是调用execute()方法。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// Both of these will return the generated URL by the connector (you can use this directly to make a call in something like Postman)
Afas::getConnector('contacts')->getUrl();

Afas::getConnector('contacts')
    ->take(10)
    ->where('Type', '=', 'Person')
    ->getUrl();

元信息

要检查连接器的元信息,我们可以简单地调用metaInfo()方法。

use WeSimplyCode\LaravelAfasRestConnector\Facades\Afas;

// Get the meta info from the connector
Afas::getConnector('contacts')->metaInfo();
Afas::updateConnector('person')->metaInfo();

// Get the json representation of the meta info from the connector
Afas::getConnector('contacts')->metaInfo()->json();
Afas::updateConnector('person')->metaInfo()->json();

致谢

Sunil Kisoensingh

许可证

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