Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
created

TopHitsAggregation::getArray()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5866
c 0
b 0
f 0
cc 7
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Aggregation\Metric;
15
16
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait;
18
use ONGR\ElasticsearchDSL\BuilderInterface;
19
use stdClass;
20
21
/**
22
 * Top hits aggregation.
23
 *
24
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
25
 */
26
class TopHitsAggregation extends AbstractAggregation
27
{
28
    use MetricTrait;
29
30
    private array $sorts = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
31
32
    public function __construct(
33
        private string $name,
34
        private ?int $size = null,
35
        private ?int $from = null,
36
        ?BuilderInterface $sort = null
37
    ) {
38
        parent::__construct($name);
39
        $this->setFrom($from);
40
        $this->setSize($size);
41
        $this->addSort($sort);
42
    }
43
44
    public function getFrom(): ?int
45
    {
46
        return $this->from;
47
    }
48
49
    public function setFrom(?int $from): static
50
    {
51
        $this->from = $from;
52
53
        return $this;
54
    }
55
56
    public function getSorts(): array
57
    {
58
        return $this->sorts;
59
    }
60
61
    public function setSorts(array $sorts): static
62
    {
63
        $this->sorts = $sorts;
64
65
        return $this;
66
    }
67
68
    public function addSort(?BuilderInterface $sort): void
69
    {
70
        $this->sorts[] = $sort;
71
    }
72
73
    public function setSize(?int $size): static
74
    {
75
        $this->size = $size;
76
77
        return $this;
78
    }
79
80
    public function getSize(): ?int
81
    {
82
        return $this->size;
83
    }
84
85
    public function getType(): string
86
    {
87
        return 'top_hits';
88
    }
89
90
    public function getArray(): array|stdClass
91
    {
92
        $sortsOutput = null;
93
        $addedSorts = array_filter($this->getSorts());
94
95
        if ($addedSorts) {
96
            $sortsOutput = [];
97
            foreach ($addedSorts as $sort) {
98
                $sortsOutput[] = $sort->toArray();
99
            }
100
        }
101
102
        $output = array_filter(
103
            [
104
                'sort' => $sortsOutput,
105
                'size' => $this->getSize(),
106
                'from' => $this->getFrom(),
107
            ],
108
            fn(mixed $val): bool => (($val || is_array($val) || ($val || is_numeric($val))))
109
        );
110
111
        return $output ?? new stdClass();
112
    }
113
114
    public function getSort(): ?BuilderInterface
115
    {
116
        if (isset($this->sorts[0])) {
117
            return $this->sorts[0];
118
        }
119
120
        return null;
121
    }
122
123
    public function setSort(BuilderInterface $sort): static
124
    {
125
        $this->sort = $sort;
126
127
        return $this;
128
    }
129
}
130