answear / luigis-box-bundle
Symfony 的 Luigi's Box 集成。
Requires
- php: >=7.4|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^6.0 || ^7.0
- marc-mabe/php-enum: ^3.0|^4.3
- symfony/http-kernel: ^5.4|^6.0
- symfony/property-access: ^5.4|^6.0
- symfony/serializer: ^5.4|^6.0
- symfony/validator: ^5.4|^6.0
- webmozart/assert: ^1.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.9.5
- matthiasnoback/symfony-config-test: ^4.3
- phpro/grumphp: 1.13.x
- phpstan/phpstan: ^1.10.34
- phpstan/phpstan-webmozart-assert: ^1.2.4
- phpunit/phpunit: ^9.5.23
- roave/security-advisories: dev-master
- symfony/phpunit-bridge: 6.1.*
README
Symfony 的 Luigi's Box 集成。Luigi's Box 文档可在此处找到:https://live.luigisbox.com/。
安装
- 使用 Composer 安装
composer require answear/luigis-box-bundle
设置
- 提供所需的配置数据:
publicKey
和privateKey
searchCacheTtl
是 Luigi 缓存请求的生存时间(秒,最大 300 秒 - 这是 Luigi's Box 的限制)
# config/packages/answear_luigis_box.yaml answear_luigis_box: default_config: second_config_name configs: your_config_name: host: 'https://live.luigisbox.com' #default publicKey: 'your_public_key' privateKey: 'your_private_key' connectionTimeout: 4.0 #default requestTimeout: 10.0 #default searchTimeout: 6.0 #default searchCacheTtl: 0 #default second_config_name: publicKey: 'your_public_key' privateKey: 'your_private_key'
如果您只有一个配置,可以省略 default_config
节点。配置将被传递到 \Answear\LuigisBoxBundle\Service\ConfigProvider
类。
如果您有更多配置,可以按以下方式更改它们
use Answear\LuigisBoxBundle\Service\ConfigProvider; /** @var ConfigProvider $configProvider **/ $configProvider->setConfig('your_config_name');
并像以前一样使用应用程序。
如果您需要向搜索请求传递自定义头信息,请按以下方式操作
use Answear\LuigisBoxBundle\Service\ConfigProvider; /** @var ConfigProvider $configProvider **/ $configProvider->setHeader('header-name', 'header-value'); /* reset all headers */ $configProvider->resetHeaders();
动态配置
您可以通过传递额外的配置。
use Answear\LuigisBoxBundle\DTO\ConfigDTO; use Answear\LuigisBoxBundle\Service\ConfigProvider; /** @var ConfigProvider $configProvider **/ $configProvider->addConfig('your_config_name', new ConfigDTO(...));
使用方法
内容请求
- 完整的 内容更新 文档
use Answear\LuigisBoxBundle\ValueObject\ContentUpdate; use Answear\LuigisBoxBundle\ValueObject\ContentUpdateCollection; // ... $collection = new ContentUpdateCollection([new ContentUpdate('product title', 'product/url', 'object type', ['field' => 'field 1'])]); /** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/ $apiResponse = $request->contentUpdate($collection);
第一个参数($title
)将被用作 Luigi's Box 中的产品标题,除非在 $fields
参数中存在 title
字段。
use Answear\LuigisBoxBundle\ValueObject\ContentUpdateCollection; use Answear\LuigisBoxBundle\ValueObject\PartialContentUpdate; // ... $collection = new ContentUpdateCollection([new PartialContentUpdate('product/url', 'object type', ['title' => 'product title'])]); /** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/ $apiResponse = $request->partialContentUpdate($collection);
use Answear\LuigisBoxBundle\ValueObject\ContentRemoval; use Answear\LuigisBoxBundle\ValueObject\ContentRemovalCollection; // ... $collection = new ContentRemovalCollection([new ContentRemoval('product/url', 'product')]); /** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/ $apiResponse = $request->contentRemoval($collection);
- 更改可用性
一种简单启用/禁用对象的方法 - 将使用部分更新。
use Answear\LuigisBoxBundle\ValueObject\ContentAvailability; use Answear\LuigisBoxBundle\ValueObject\ContentAvailabilityCollection; // ... $isAvailable = true; $collection = new ContentAvailabilityCollection([new ContentAvailability('product/url', $isAvailable)]); /** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/ $apiResponse = $request->changeAvailability($collection); // ... or pass one object $isAvailable = true; /** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/ $apiResponse = $request->changeAvailability(new ContentAvailability('product/url', $isAvailable));
在所有请求中,您可以捕获一些异常
BadRequestException
- 请求无效,TooManyItemsException
- 使用更少的项进行请求,MalformedResponseException
- Luigi's Box API 响应出现问题,TooManyRequestsException
- 延迟请求速率,ServiceUnavailableException
请单独捕获它们
use Answear\LuigisBoxBundle\Exception\BadRequestException; use Answear\LuigisBoxBundle\Exception\TooManyItemsException; use Answear\LuigisBoxBundle\Exception\MalformedResponseException; use Answear\LuigisBoxBundle\Exception\TooManyRequestsException; use Answear\LuigisBoxBundle\Exception\ServiceUnavailableException; try { // ... request } catch (BadRequestException $e){ //bad request $request = $e->getRequest(); $response = $e->getResponse(); } catch (TooManyItemsException $e){ //items limit reached $limit = $e->getLimit(); } catch (MalformedResponseException $e){ //bad response $response = $e->getResponse(); } catch (TooManyRequestsException $e){ //repeat request after $retryAfter seconds $retryAfter = $e->getRetryAfterSeconds(); } catch (ServiceUnavailableException $e){ //delay request }
内容响应
\Answear\LuigisBoxBundle\Response\ApiResponse
:
- (bool)
$success
- 如果所有文档都成功传递,则为true
, - (int)
$okCount
- 成功传递的文档数, - (int)
$errorsCount
- 失败的文档数, - (array)
$errors
-\Answear\LuigisBoxBundle\Response\ApiResponseError
对象数组, - (array)
$rawResponse
- 从 API 解码的响应。
ApiResponseError
:
- (string)
$url
- 文档的 URL - (string)
$type
- 错误类型(例如malformed_input
) - (string)
$reason
- 失败文本(例如incorrect object format
) - (array|null)
$causedBy
- 错误的具体原因(例如["url": ["is missing"]]
)
注意!
ApiResponse::$success
如果传递的任何文档失败,则设置为 false
。如果您想了解更新了多少文档,请检查 $okCount
,如果要检查哪些文档失败,请检查 $errors
。
搜索(文档 在此)
- 请求
use Answear\LuigisBoxBundle\ValueObject\SearchUrlBuilder; // ... $page = 3; $urlBuilder = new SearchUrlBuilder($page); $urlBuilder ->setQuery('nice top') ->addFilter('type', 'product') ->addFilter('category', 'top') ->addFilter('brand', 'Medicine') ->addFilter('brand', 'Answear') ->addPrefer('brand', 'Answear') ->setSort('size', 'asc'); //the above code produces a url query like `size=10&page=3&q=nice+top&f%5B0%5D=type%3Aproduct&f%5B1%5D=category%3Atop&f%5B2%5D=brand%3AMedicine&f%5B3%5D=brand%3AAnswear&sort=size%3Aasc&prefer%5B0%5D=brand%3AAnswear` /** @var \Answear\LuigisBoxBundle\Service\SearchRequestInterface $request **/ $searchResponse = $request->search($urlBuilder);
请参阅 Luigi's Box 文档以了解每个字段 SearchUrlBuilder
暴露的确切用途。
- 响应
SearchRequest::search()
将返回一个包含以下字段的 SearchResponse
对象
- (string) $searchUrl
- (string) $query
- (string|null) $correctedQuery
- (array) $filters
- (Hit[]) $hits.
Hit
- (string) $url;
- (array) $attributes;
- (array) $nested;
- (字符串) $type;
- (数组) $highlight;
- (布尔) $exact;
- (布尔) $alternative;
- (命中记录[]) $quickSearchHits
- 与上面相同
- (属性[]) $facets.
属性
- (字符串) $name;
- (字符串) $type;
- (数组) $values;
- (整数) $totalHits
- (整数) $currentSize
通过查询更新(文档在此)
- 更新
use Answear\LuigisBoxBundle\ValueObject\UpdateByQuery; // ... $types = ['product']; $fields = ['color' => 'green']; $search = new UpdateByQuery\Search($types, $fields); $updateFields = ['color' => ['olive', 'emerald']]; $update = new UpdateByQuery\Update($updateFields); $updateByQuery = new UpdateByQuery($search, $update); /** @var \Answear\LuigisBoxBundle\Service\UpdateByQueryRequest $request **/ $response = $request->update($updateByQuery); $jobId = $response->getJobId();
- 检索作业状态
use Answear\LuigisBoxBundle\ValueObject\UpdateByQuery; $jobId = 1; /** @var \Answear\LuigisBoxBundle\Service\UpdateByQueryRequest $request **/ $response = $request->getStatus($jobId);
getStatus
将返回包含以下字段的 UpdateByQueryStatusResponse
- (数组) $rawResponse
- (字符串) $trackerId
- (布尔) $completed
- (整数|null) $okCount - 如果未完成则为 null
- (整数|null) $errorsCount - 如果未完成则为 null
- (ApiResponseError[]|null) $errors - 如果未完成则为 null
最后备注
请随时提交带有新功能、改进或错误修复的拉取请求。Answear 团队将非常感谢任何评论。
祝您玩得开心!