Completed
Push — master ( 0d45ed...ea3502 )
by Nicolas
02:38
created

src/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 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...
28
            $this->setInitScript($initScript);
29
        }
30
        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...
31
            $this->setMapScript($mapScript);
32
        }
33
        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...
34
            $this->setCombineScript($combineScript);
35
        }
36
        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...
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
     * @return $this
48
     */
49
    public function setCombineScript(string $script): self
50
    {
51
        return $this->setParam('combine_script', $script);
52
    }
53
54
    /**
55
     * Executed prior to any collection of documents.
56
     *
57
     * Allows the aggregation to set up any initial state.
58
     *
59
     * @return $this
60
     */
61
    public function setInitScript(string $script): self
62
    {
63
        return $this->setParam('init_script', $script);
64
    }
65
66
    /**
67
     * Executed once per document collected.
68
     *
69
     * This is the only required script. If no combine_script is specified, the resulting state needs to be stored in
70
     * an object named _agg.
71
     *
72
     * @return $this
73
     */
74
    public function setMapScript(string $script): self
75
    {
76
        return $this->setParam('map_script', $script);
77
    }
78
79
    /**
80
     * Executed once on the coordinating node after all shards have returned their results.
81
     *
82
     * The script is provided with access to a variable _aggs which is an array of the result of the combine_script on
83
     * each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
84
     *
85
     * @return $this
86
     */
87
    public function setReduceScript(string $script): self
88
    {
89
        return $this->setParam('reduce_script', $script);
90
    }
91
}
92