PHP code example of krzysztof-gzocha / searcher-bundle

1. Go to this page and download the library: Download krzysztof-gzocha/searcher-bundle 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/ */

    

krzysztof-gzocha / searcher-bundle example snippets


public function registerBundles()
{
  $bundles = array(
    /** Your bundles **/
    new KGzocha\Bundle\SearcherBundle\KGzochaSearcherBundle(),
  );
  /** rest of the code  **/
}

$this->get('k_gzocha_searcher.people.searcher');

$this->get('k_gzocha_searcher.people.criteria.age_range');

$this->get('k_gzocha_searcher.people.builder.my_criteria_builder');

class AgeRangeCriteria implements CriteriaInterface
{
    private $minimalAge;
    private $maximalAge;

    /**
    * Only 
    }

    // getters, setters, what ever
}

class AgeRangeCriteriaBuilder implements FilterImposerInterface
{
    public function buildCriteria(
        CriteriaInterface $criteria,
        SearchingContextInterface $searchingContext
    ) {
        $searchingContext
            ->getQueryBuilder()
            ->andWhere('e.age >= :minimalAge')
            ->andWhere('e.age <= :maximalAge')
            ->setParameter('minimalAge', $criteria->getMinimalAge())
            ->setParameter('maximalAge', $criteria->getMaximalAge());
    }

    public function allowsCriteria(
        CriteriaInterface $criteria
    ) {
        // No need to check shouldBeApplied(). Searcher will check it
        return $criteria instanceof AgeRangeCriteria;
    }

    /**
    * You can skip this method if you will extend from QueryBuilderFilterImposer.
    */
    public function supportsSearchingContext(
        SearchingContextInterface $searchingContext
    ) {
        return $searchingContext instanceof \KGzocha\Searcher\Context\Doctrine\QueryBuilderSearchingContext;
    }
}

use KGzocha\Bundle\SearcherBundle\Form\SearchForm;

class MySearchForm extends SearchForm
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('minimalAge', 'integer', [
                'property_path' => $this->getPath('ageRange', 'minimalAge'),
            ])
            ->add('maximalAge', 'integer', [
                'property_path' => $this->getPath('ageRange', 'maximalAge'),
            ])
            /** and any other fields.. **/
            ->add('<PARAM NAME IN REQUEST>', '<ANY FORM TYPE>', [
                'property_path' => $this->getPath(
                    '<CRITERIA NAME FROM CONFIG>',
                    '<CRITERIA ATTRIBUTE NAME>'
                ),
            ]);
    }
}

public function searchAction(Request $request)
{
    $form = $this->createForm(
        new MySearchForm(),
        $this->get('k_gzocha_searcher.people.criteria_collection')
    );

    $form->handleRequest($request);
    // Now we can check if form is valid

    $searcher = $this->get('k_gzocha_searcher.people.searcher');
    $results = $searcher->search($form->getData());
    // Yay, we have our results!
    // $results is instance of ResultCollection by default. Read for 'wrapper_class'
}

$this->get('k_gzocha_searcher.chains.people_log.searcher');         // ChainSearch service

$this->get('k_gzocha_searcher.chains.people_log.cell.peopleCell');  // #1 Cell service 
$this->get('k_gzocha_searcher.chains.people_log.cell.logCell');     // #2 Cell service

$this->get('k_gzocha_searcher.chains.people_log.transformer.peopleToLogId'); // Transformer service