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

FieldSort   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 124
rs 10
c 0
b 0
f 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\Sort;
15
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
use ONGR\ElasticsearchDSL\ParametersTrait;
18
use stdClass;
19
20
class FieldSort implements BuilderInterface
21
{
22
    use ParametersTrait;
23
24
    public const ASC = 'asc';
25
26
    public const DESC = 'desc';
27
28
    private ?BuilderInterface $nestedFilter = null;
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 '?', expecting T_FUNCTION or T_CONST
Loading history...
29
30
    public function __construct(private string $field, private mixed $order = null, $params = [])
31
    {
32
        $this->setParameters($params);
33
    }
34
35
    public function getField(): string
36
    {
37
        return $this->field;
38
    }
39
40
    public function setField(string $field): static
41
    {
42
        $this->field = $field;
43
44
        return $this;
45
    }
46
47
    public function getOrder(): mixed
48
    {
49
        return $this->order;
50
    }
51
52
    public function setOrder(mixed $order): static
53
    {
54
        $this->order = $order;
55
56
        return $this;
57
    }
58
59
    public function getNestedFilter(): ?BuilderInterface
60
    {
61
        return $this->nestedFilter;
62
    }
63
64
    public function setNestedFilter(?BuilderInterface $nestedFilter): static
65
    {
66
        $this->nestedFilter = $nestedFilter;
67
68
        return $this;
69
    }
70
71
    public function getType(): string
72
    {
73
        return 'sort';
74
    }
75
76
    public function toArray(): array
77
    {
78
        if ($this->order) {
79
            $this->addParameter('order', $this->order);
80
        }
81
82
        if ($this->nestedFilter) {
83
            $this->addParameter('nested', $this->nestedFilter->toArray());
84
        }
85
86
        $output = [
87
            $this->field => $this->getParameters() ?: new stdClass(),
88
        ];
89
90
        return $output;
91
    }
92
}
93