PHP code example of vaened / php-search-engine

1. Go to this page and download the library: Download vaened/php-search-engine 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/ */

    

vaened / php-search-engine example snippets


class ProductSearchEngine extends SearchEngine  
{  
    use Flaggable, Indexable;  
  
    public function __construct(  
        private readonly IndexRepository $index,  
        private readonly FlagFiltrator   $filter  
    ) {  
        $this->apply(Criterias\ProductStatus::active());  
    }  
  
    protected function filterer(): Filterer  
    {  
        return $this->filter;  
    }  
  
    protected function indexer(): indexer  
    {  
        return $this->index;  
    }  
}

protected function apply(Scope|Expression|Filter ...$criterias): void
{
    $this->criterias = array_merge($this->criterias, $criterias);
}

protected function criteria(int $page = 1, ?int $perPage = null): Criteria
{
    $perPage ??= $this->perPage;
    $offset  = ($page - 1) * $perPage;

    return new Criteria($this->criterias, $this->order, $perPage, $offset);
}

public function filter(FlagBag $flags): self
{
    $this->filterer()
         ->only($flags)
         ->each(
             fn(Scope|Expression|Filter $criteria) => $this->apply($criteria)
         );

    return $this;
}

public function search(BackedEnum $index, ?string $queryString): static
{
    if (
        null !== $queryString &&
        null !== (
        $criteria = $this->indexer()->search($index, $queryString)
        )
    ) {
        $this->apply($criteria);
    }

    return $this;
}

interface Filter  
{  
    public function field(): FilterField;  
  
    public function operator(): FilterOperator;  
  
    public function value(): FilterValue;  
}

public function only(FlagBag $flags): ArrayList
{
    return $this->flags()
                ->only($flags)
                ->map(static fn(callable $criteria) => apply($criteria));
}

public function search(BackedEnum $index, string $queryString): null|Scope|Expression|Filter
{
    $criteria = $this->indexes()->get($index);

    if (null === $criteria) {
        return null;
    }

    return apply($criteria, [$queryString]);
}