PHP code example of rollerworks / search-dev

1. Go to this page and download the library: Download rollerworks/search-dev 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-dev example snippets


use Rollerworks\Component\Search\Searches;
use Rollerworks\Component\Search\Exception\InvalidSearchConditionException;
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\Input\ErrorPathHumanizer;
use Rollerworks\Component\Search\Input\StringQueryInput;
use Rollerworks\Component\Search\Input\ProcessorConfig;

// 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('type', ChoiceType::class, [
        'choices' => ['Consumer' => 'c', 'Business' => 'b'],
    ])
    ->getFieldSet('users');
    
// Now lets process a string query.
// Tip: the input processor is reusable.
$inputProcessor = new StringQueryInput();

try {
    // The ProcessorConfig allows to limit the amount of values, groups
    // and maximum nesting level. The defaults should be restrictive enough
    // for most situations.
    $processorConfig = new ProcessorConfig($userFieldSet);
    
    // The `process` method processes the input and produces 
    // a valid SearchCondition (or throws an exception when something is wrong).
    $condition = $inputProcessor->process('firstName: sebastiaan, melany;');
} catch (InvalidSearchConditionException $e) {
    // Each error message can be transformed to a localized version
    // using the Symfony Translator contract.
    
    $translator = ...; // \Symfony\Contracts\Translation\TranslatorInterface
    
    // Note: The ErrorPathHumanizer only works for the `StringQueryInput` input processor.
    $errorPathHumanizer = new ErrorPathHumanizer($translator);
    
    foreach ($e->getErrors() as $error) {
       echo '<span>' $errorPathHumanizer->humanize($error->path) .  ' </span><span>' . htmlentities($error->trans($translator), ENT_COMPAT | ENT_IGNORE, 'UTF-8') '</span>' . PHP_EOL;
    }
}