Skip to content
Snippets Groups Projects
Bool.php 1.85 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\ElasticsearchBundle\DSL\Bool;

use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait;

/**
 * Bool operator. Can be used for filters and queries.
ONGR Team's avatar
ONGR Team committed
 */
class Bool implements BuilderInterface
{
    use ParametersTrait;

    const MUST = 'must';
    const MUST_NOT = 'must_not';
    const SHOULD = 'should';

ONGR Team's avatar
ONGR Team committed
    /**
     * @var array
     */
    private $container = [];

    /**
     * Checks if bool filter is relevant.
     *
     * @return bool
     */
    public function isRelevant()
    {
        return (bool)count($this->container);
    }

    /**
     * Add BuilderInterface object to bool operator.
ONGR Team's avatar
ONGR Team committed
     *
     * @param BuilderInterface $bool
     * @param string           $type
     *
     * @throws \UnexpectedValueException
ONGR Team's avatar
ONGR Team committed
     */
    public function addToBool(BuilderInterface $bool, $type = self::MUST)
ONGR Team's avatar
ONGR Team committed
    {
        $constants = (new \ReflectionObject($this))->getConstants();

        if (!in_array($type, $constants)) {
            throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
        }

        $this->container[$type][] = $bool;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * {@inheritdoc}
     */
    public function getType()
    {
        return 'bool';
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        $output = $this->processArray();

        foreach ($this->container as $type => $filters) {
            /** @var BuilderInterface $bool */
            foreach ($filters as $bool) {
                $output[$type][] = [$bool->getType() => $bool->toArray()];
            }
        }

        return $output;
    }
}