rollerworks/search-processor

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

RollerworksSearch 的搜索请求处理

v2.0.0-ALPHA11 2017-12-21 08:26 UTC

README

此包为 RollerworksSearch 提供 SearchProcess 请求处理器。您需要一个 PSR-7 兼容的库来向处理器提供请求信息。

处理器可以与所有支持的输入处理器、条件导出器一起使用,并可用于 REST-API 端点。

安装

要安装此包,将 rollerworks/search-processor 添加到您的 composer.json 中

$ php composer.phar require rollerworks/search-processor

现在,Composer 将自动下载所有必需的文件,并为您安装它们。

基本用法

每个 SearchProcessor 都处理一个包含关于处理结果的只读信息的 SearchPayload

SearchProcessor 可重用,并懒加载相关依赖。这里显示的加载器不允许自定义输入/条件导出器,请使用 PSR-11 兼容的实现。

use Rollerworks\Component\Search\Searches;
use Rollerworks\Component\Search\Extension\Core\Type\TextType;
use Rollerworks\Component\Search\Extension\Core\Type\IntegerType;
use Rollerworks\Component\Search\Extension\Core\Type\ChoiceType;
use Rollerworks\Component\Search\Loader;
use Rollerworks\Component\Search\Processor\ProcessorConfig;
use Rollerworks\Component\Search\Processor\Psr7SearchProcessor;

// The factory is reusable, you create it only once.
$searchFactory = Searches::createSearchFactory();

// Create a fieldset to inform the system about your configuration.
// Usually you will have a FieldSet for each data structure (users, invoices, etc).
$userFieldSet = $searchFactory->createFieldSetBuilder()
    ->add('firstName', TextType::class)
    ->add('lastName', TextType::class)
    ->add('age', IntegerType::class)
    ->add('gender', ChoiceType::class, [
        'choices' => ['Female' => 'f', 'Male' => 'm'],
    ])
    ->getFieldSet('users');

$inputProcessorLoader = Loader\InputProcessorLoader::create();
$conditionExporterLoader = Loader\ConditionExporterLoader::create();    
$processor = new Psr7SearchProcessor($searchFactory, $inputProcessorLoader, $conditionExporterLoader);

$request = ...; // A PSR-7 ServerRequestInterface object instance

$processorConfig = new ProcessorConfig($userFieldSet);
$searchPayload = $processor->processRequest($request, $processorConfig);

// When a POST is provided the processor will validate the input
// and export it. Note that an empty result is also valid.
// 
// The searchCode depends on the implementation of the SearchProcessor,
// and in this case contains a JSON exported SearchCondition encoded for URI usage.
if ($searchPayload->isChanged() && $searchPayload->isValid()) {
    // Redirect to this page with the search-code provided.
    header('Location: /search?search='.$searchPayload->searchCode);
    exit();
}

// Always do this check because searchCode could be malformed resulting in
// an invalid SearchCondition.
if (!$payload->isValid()) {
    // Each error message can be easily transformed to a localized version.
    // Read the documentation for more details.
    foreach ($payload->messages as $error) {
       echo (string) $error.PHP_EOL;
    }
}

// Notice: This is null when there are errors, when the condition is valid but has
// no fields/values this is an empty SearchCondition object.
$condition = $payload->searchCondition;

为了提高分页搜索结果中的性能,可以使用 CachedSearchProcessor(需要 PSR-16 SimpleCache 实现)来缓存生成的负载。

版本控制

为了透明度和对发布周期的洞察,并力求保持向后兼容性,此包尽可能在语义版本控制指南下维护。

发布将按照以下格式编号

<主要>.<次要>.<补丁>

并遵循以下指南

  • 破坏向后兼容性会提升主要版本(并重置次要和补丁)
  • 没有破坏向后兼容性的新功能会提升次要版本(并重置补丁)
  • 错误修复和杂项更改会提升补丁

有关 SemVer 的更多信息,请访问 http://semver.org/

许可

此包的源代码受 MIT 许可证的约束,该许可证包含在此源代码文件中 LICENSE

贡献

这是一个开源项目。如果您想贡献,请阅读 Symfony 的 贡献代码 部分。如果您正在提交拉取请求,请遵循 提交补丁 部分的指南。