Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
created

lib/Elastica/Aggregation/ScriptedMetric.php (4 issues)

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
3
namespace Elastica\Aggregation;
4
5
/**
6
 * Class ScriptedMetric.
7
 *
8
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
9
 */
10
class ScriptedMetric extends AbstractAggregation
11
{
12
    /**
13
     * @param string      $name          the name if this aggregation
14
     * @param string|null $initScript    Executed prior to any collection of documents
15
     * @param string|null $mapScript     Executed once per document collected
16
     * @param string|null $combineScript Executed once on each shard after document collection is complete
17
     * @param string|null $reduceScript  Executed once on the coordinating node after all shards have returned their results
18
     */
19
    public function __construct($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
20
    {
21
        parent::__construct($name);
22
        if ($initScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $initScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
23
            $this->setInitScript($initScript);
24
        }
25
        if ($mapScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mapScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
26
            $this->setMapScript($mapScript);
27
        }
28
        if ($combineScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $combineScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
29
            $this->setCombineScript($combineScript);
30
        }
31
        if ($reduceScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $reduceScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32
            $this->setReduceScript($reduceScript);
33
        }
34
    }
35
36
    /**
37
     * Executed once on each shard after document collection is complete.
38
     *
39
     * Allows the aggregation to consolidate the state returned from each shard.
40
     * If a combine_script is not provided the combine phase will return the aggregation variable.
41
     *
42
     * @param string $script
43
     *
44
     * @return $this
45
     */
46
    public function setCombineScript($script)
47
    {
48
        return $this->setParam('combine_script', $script);
49
    }
50
51
    /**
52
     * Executed prior to any collection of documents.
53
     *
54
     * Allows the aggregation to set up any initial state.
55
     *
56
     * @param string $script
57
     *
58
     * @return $this
59
     */
60
    public function setInitScript($script)
61
    {
62
        return $this->setParam('init_script', $script);
63
    }
64
65
    /**
66
     * Executed once per document collected.
67
     *
68
     * This is the only required script. If no combine_script is specified, the resulting state needs to be stored in
69
     * an object named _agg.
70
     *
71
     * @param string $script
72
     *
73
     * @return $this
74
     */
75
    public function setMapScript($script)
76
    {
77
        return $this->setParam('map_script', $script);
78
    }
79
80
    /**
81
     * Executed once on the coordinating node after all shards have returned their results.
82
     *
83
     * The script is provided with access to a variable _aggs which is an array of the result of the combine_script on
84
     * each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
85
     *
86
     * @param string $script
87
     *
88
     * @return $this
89
     */
90
    public function setReduceScript($script)
91
    {
92
        return $this->setParam('reduce_script', $script);
93
    }
94
}
95