ListCriteria::getFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright © Thomas Klein, All rights reserved.
4
 */
5
declare(strict_types=1);
6
7
namespace Zoho\Desk\Model;
8
9
use function implode;
10
11
final class ListCriteria implements ListCriteriaInterface
12
{
13
    /**
14
     * @var string[]
15
     */
16
    private array $fields;
17
18
    /**
19
     * @var string[]
20
     */
21
    private array $filters;
22
23
    /**
24
     * @var string[]
25
     */
26
    private array $include;
27
28
    private ?int $from;
29
30
    private ?int $limit;
31
32
    private ?string $sortBy;
33
34
    private ?string $sortOrder;
35
36
    private ?int $viewId;
37
38
    public function __construct(
39
        array $filters,
40
        array $fields,
41
        array $include,
42
        ?int $from,
43
        ?int $limit,
44
        ?string $sortBy,
45
        ?string $sortOrder,
46
        ?int $viewId
47
    ) {
48
        $this->filters = $filters;
49
        $this->fields = $fields;
50
        $this->include = $include;
51
        $this->from = $from;
52
        $this->limit = $limit;
53
        $this->sortBy = $sortBy;
54
        $this->sortOrder = $sortOrder;
55
        $this->viewId = $viewId;
56
    }
57
58
    public function getFilters(): array
59
    {
60
        return (array) $this->filters;
61
    }
62
63
    public function getFields(): array
64
    {
65
        return (array) $this->fields;
66
    }
67
68
    public function getInclude(): array
69
    {
70
        return (array) $this->include;
71
    }
72
73
    public function getFrom(): ?int
74
    {
75
        return $this->from ? (int) $this->from : null;
76
    }
77
78
    public function getLimit(): ?int
79
    {
80
        return $this->limit ? (int) $this->limit : null;
81
    }
82
83
    public function getSortBy(): ?string
84
    {
85
        return $this->sortBy ? (string) $this->sortBy : null;
86
    }
87
88
    public function getSortOrder(): ?string
89
    {
90
        return $this->sortOrder ? (string) $this->sortOrder : null;
91
    }
92
93
    public function getViewId(): ?int
94
    {
95
        return $this->viewId ? (int) $this->viewId : null;
96
    }
97
98
    public function getQueryParams(): array
99
    {
100
        $query = $this->getFilters();
101
102
        if (!$query) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
103
            if ($this->getFields()) {
104
                $query['fields'] = implode(',', $this->getFields());
105
            }
106
            if ($this->getInclude()) {
107
                $query['include'] = implode(',', $this->getInclude());
108
            }
109
            if ($this->getViewId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getViewId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
110
                $query['viewId'] = $this->getViewId();
111
            }
112
        }
113
        if ($this->getFrom()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getFrom() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
114
            $query['from'] = $this->getFrom();
115
        }
116
        if ($this->getLimit()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getLimit() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
117
            $query['limit'] = $this->getLimit();
118
        }
119
        if ($this->getSortBy()) {
120
            $query['sortBy'] = ($this->getSortOrder() === 'DESC' ? '-' : '') . $this->getSortBy();
121
        }
122
123
        return $query;
124
    }
125
}
126