PHP code example of rollerworks / search-processor
1. Go to this page and download the library: Download rollerworks/search-processor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rollerworks / search-processor example snippets
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;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.