Skip to content
Snippets Groups Projects
AndFilter.php 2.13 KiB
Newer Older
ONGR Team's avatar
ONGR Team committed
<?php

/*
 * This file is part of the ONGR package.
 *
 * (c) NFQ Technologies UAB <info@nfq.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ONGR\ElasticsearchDSL\Filter;
ONGR Team's avatar
ONGR Team committed

Mantas Varatiejus's avatar
Mantas Varatiejus committed
@trigger_error(
    'The AndFilter class is deprecated and will be removed in 2.0. Use BoolQuery instead.',
    E_USER_DEPRECATED
);

use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
ONGR Team's avatar
ONGR Team committed

/**
 * Represents Elasticsearch "and" filter.
 *
 * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-and-filter.html
Mantas Varatiejus's avatar
Mantas Varatiejus committed
 *
 * @deprecated Will be removed in 2.0. Use the BoolQuery instead.
ONGR Team's avatar
ONGR Team committed
 */
class AndFilter implements BuilderInterface
{
    use ParametersTrait;

    /**
     * @var array
ONGR Team's avatar
ONGR Team committed
     */
    private $filters = [];
    
ONGR Team's avatar
ONGR Team committed
    /**
     * @param BuilderInterface[] $filters    Filter array.
ONGR Team's avatar
ONGR Team committed
     * @param array              $parameters Optional parameters.
     */
    public function __construct(array $filters = [], array $parameters = [])
ONGR Team's avatar
ONGR Team committed
    {
        $this->set($filters);
ONGR Team's avatar
ONGR Team committed
        $this->setParameters($parameters);
    }

    /**
     * Sets filters.
     *
     * @param BuilderInterface[] $filters Filter array.
ONGR Team's avatar
ONGR Team committed
     */
    public function set(array $filters)
ONGR Team's avatar
ONGR Team committed
    {
        foreach ($filters as $filter) {
            $this->add($filter);
        }
    }
    
    /**
     * Adds filter.
     *
     * @param BuilderInterface $filter
     *
     * @return AndFilter
     */
    public function add(BuilderInterface $filter)
    {
        $this->filters[] = [$filter->getType() => $filter->toArray()];
        
        return $this;
    }

    /**
     * Clears filters.
     */
    public function clear()
    {
        $this->filters = [];
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        $query = $this->processArray();
ONGR Team's avatar
ONGR Team committed

        if (count($query) > 0) {
            $query['filters'] = $this->filters;
        } else {
            $query = $this->filters;
ONGR Team's avatar
ONGR Team committed
        }

        return $query;
    }
ONGR Team's avatar
ONGR Team committed

    /**
     * {@inheritdoc}
     */
    public function getType()
    {
        return 'and';
ONGR Team's avatar
ONGR Team committed
    }
}