Completed
Push — master ( 4cdb71...eee063 )
by Nicolas
02:28
created

Elastica/Aggregation/AbstractSimpleAggregation.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica\Aggregation;
3
4
use Elastica\Exception\InvalidException;
5
6
abstract class AbstractSimpleAggregation extends AbstractAggregation
7
{
8
    /**
9
     * Set the field for this aggregation.
10
     *
11
     * @param string $field the name of the document field on which to perform this aggregation
12
     *
13
     * @return $this
14
     */
15
    public function setField($field)
16
    {
17
        return $this->setParam('field', $field);
18
    }
19
20
    /**
21
     * Set a script for this aggregation.
22
     *
23
     * @param string|\Elastica\Script\AbstractScript $script
24
     *
25
     * @return $this
26
     */
27
    public function setScript($script)
28
    {
29
        return $this->setParam('script', $script);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function toArray()
36
    {
37
        if (!$this->hasParam('field') && !$this->hasParam('script')) {
38
            throw new InvalidException(
39
                'Either the field param or the script param should be set'
40
            );
41
        }
42
        $array = parent::toArray();
43
44
        $baseName = $this->_getBaseName();
45
46 View Code Duplication
        if (isset($array[$baseName]['script']) && is_array($array[$baseName]['script'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            $script = $array[$baseName]['script'];
48
49
            unset($array[$baseName]['script']);
50
51
            $array[$baseName] = array_merge($array[$baseName], $script);
52
        }
53
54
        return $array;
55
    }
56
}
57