Skip to content
Snippets Groups Projects
AbstractAggregation.php 2.95 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\Aggregation;

use ONGR\ElasticsearchBundle\DSL\NamedBuilderBag;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
ONGR Team's avatar
ONGR Team committed

/**
 * AbstractAggregation class.
 */
abstract class AbstractAggregation implements NamedBuilderInterface
ONGR Team's avatar
ONGR Team committed
{
    const PREFIX = 'agg_';

ONGR Team's avatar
ONGR Team committed
    /**
     * @var string
     */
ONGR Team's avatar
ONGR Team committed

    /**
     * @var string
     */
ONGR Team's avatar
ONGR Team committed

    /**
     * @var NamedBuilderBag
ONGR Team's avatar
ONGR Team committed
     */
ONGR Team's avatar
ONGR Team committed

    /**
     * @return string
     */
    abstract public function getType();

    /**
     * Abstract supportsNesting method.
     *
     * @return bool
     */
    abstract protected function supportsNesting();

    /**
     * @return array|\stdClass
     */
    abstract protected function getArray();

    /**
     * Inner aggregations container init.
     *
     * @param string $name
     */
    public function __construct($name)
    {
        $this->name = $name;
        $this->aggregations = new NamedBuilderBag();
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * @param string $field
     */
    public function setField($field)
    {
        $this->field = $field;
    }

    /**
     * @return string
     */
    public function getField()
    {
        return $this->field;
    }

    /**
     * {@inheritdoc}
ONGR Team's avatar
ONGR Team committed
     */
    public function getName()
    {
        return self::PREFIX . $this->name;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Adds a sub-aggregation.
     *
     * @param AbstractAggregation $abstractAggregation
     */
    public function addAggregation(AbstractAggregation $abstractAggregation)
    {
        $this->aggregations->add($abstractAggregation);
    }

    /**
     * Returns all sub aggregations.
     *
     * @return AbstractAggregation[]
     */
    public function getAggregations()
    {
        return $this->aggregations->all();
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        $array = $this->getArray();
        $result = [
            $this->getName() => [
                $this->getType() => is_array($array) ? $this->processArray($array) : $array,
            ],
        ];
ONGR Team's avatar
ONGR Team committed

        if ($this->supportsNesting()) {
            $nestedResult = $this->collectNestedAggregations();

            if (!empty($nestedResult)) {
                $result[$this->getName()]['aggregations'] = $nestedResult;
            }
        }

        return $result;
    }

    /**
     * Process all nested aggregations.
     *
     * @return array
     */
    protected function collectNestedAggregations()
    {
        $result = [];
        foreach ($this->getAggregations() as $aggregation) {
ONGR Team's avatar
ONGR Team committed
            $result = array_merge($result, $aggregation->toArray());
        }

        return $result;
    }
}