Completed
Push — master ( aa6457...bb06c3 )
by Federico
02:20
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(
20
        string $name,
21
        string $initScript = null,
22
        string $mapScript = null,
23
        string $combineScript = null,
24
        string $reduceScript = null
25
    ) {
26
        parent::__construct($name);
27
        if ($initScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $initScript of type null|string 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...
28
            $this->setInitScript($initScript);
29
        }
30
        if ($mapScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mapScript of type null|string 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...
31
            $this->setMapScript($mapScript);
32
        }
33
        if ($combineScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $combineScript of type null|string 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...
34
            $this->setCombineScript($combineScript);
35
        }
36
        if ($reduceScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $reduceScript of type null|string 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...
37
            $this->setReduceScript($reduceScript);
38
        }
39
    }
40
41
    /**
42
     * Executed once on each shard after document collection is complete.
43
     *
44
     * Allows the aggregation to consolidate the state returned from each shard.
45
     * If a combine_script is not provided the combine phase will return the aggregation variable.
46
     *
47
     * @param string $script
48
     *
49
     * @return $this
50
     */
51
    public function setCombineScript(string $script): self
52
    {
53
        return $this->setParam('combine_script', $script);
54
    }
55
56
    /**
57
     * Executed prior to any collection of documents.
58
     *
59
     * Allows the aggregation to set up any initial state.
60
     *
61
     * @param string $script
62
     *
63
     * @return $this
64
     */
65
    public function setInitScript(string $script): self
66
    {
67
        return $this->setParam('init_script', $script);
68
    }
69
70
    /**
71
     * Executed once per document collected.
72
     *
73
     * This is the only required script. If no combine_script is specified, the resulting state needs to be stored in
74
     * an object named _agg.
75
     *
76
     * @param string $script
77
     *
78
     * @return $this
79
     */
80
    public function setMapScript(string $script): self
81
    {
82
        return $this->setParam('map_script', $script);
83
    }
84
85
    /**
86
     * Executed once on the coordinating node after all shards have returned their results.
87
     *
88
     * The script is provided with access to a variable _aggs which is an array of the result of the combine_script on
89
     * each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
90
     *
91
     * @param string $script
92
     *
93
     * @return $this
94
     */
95
    public function setReduceScript(string $script): self
96
    {
97
        return $this->setParam('reduce_script', $script);
98
    }
99
}
100