Skip to content
Snippets Groups Projects
FieldSort.php 1.95 KiB
Newer Older
<?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\Sort;

use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;

/**
 * Holds all the values required for basic sorting.
 */
class FieldSort implements BuilderInterface
{
    use ParametersTrait;

    const ASC = 'asc';
    const DESC = 'desc';
    /**
     * @var BuilderInterface
     */
    private $nestedFilter;

    /**
     * @param string $field  Field name.
     * @param string $order  Order direction.
     * @param array  $params Params that can be set to field sort.
    public function __construct($field, $order = null, $params = [])
    {
        $this->field = $field;
        $this->setParameters($params);
    }

    /**
     * @return BuilderInterface
     */
    public function getNestedFilter()
    {
        return $this->nestedFilter;
    }

    /**
     * @param BuilderInterface $nestedFilter
     */
    public function setNestedFilter(BuilderInterface $nestedFilter)
    {
        $this->nestedFilter = $nestedFilter;
    }

    /**
     * Returns element type.
     *
     * @return string
     */
    public function getType()
    {
        return 'sort';
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
            $this->addParameter('order', $this->order);
        if ($this->nestedFilter) {
            $this->addParameter('nested_filter', $this->nestedFilter->toArray());
            $this->field => !$this->getParameters() ? new \stdClass() : $this->getParameters(),
        return $output;