Completed
Pull Request — master (#178)
by ignace nyamagana
03:42
created

Statement::getSortBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
namespace League\Csv;
14
15
use InvalidArgumentException;
16
use Iterator;
17
use League\Csv\Config\Validator;
18
19
/**
20
 * A simple Statement class to fetch rows against a Csv file object
21
 *
22
 * @package League.csv
23
 * @since  9.0.0
24
 *
25
 */
26
class Statement
27
{
28
    use Validator;
29
30
    /**
31
     * Callables to filter the iterator
32
     *
33
     * @var callable[]
34
     */
35
    protected $filters = [];
36
37
    /**
38
     * Callables to sort the iterator
39
     *
40
     * @var callable[]
41
     */
42
    protected $sort_by = [];
43
44
    /**
45
     * iterator Offset
46
     *
47
     * @var int
48
     */
49
    protected $offset = 0;
50
51
    /**
52
     * iterator maximum length
53
     *
54
     * @var int
55
     */
56
    protected $limit = -1;
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function __set($property, $value)
62
    {
63
        throw new InvalidArgumentException(sprintf('%s is an undefined property', $property));
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function __unset($property)
70
    {
71
        throw new InvalidArgumentException(sprintf('%s is an undefined property', $property));
72
    }
73
74
    /**
75
     * Return the added filter callable functions
76
     *
77
     * @return callable[]
78
     */
79
    public function getFilter()
80
    {
81
        return $this->filters;
82
    }
83
84
    /**
85
     * Return the added sorting callable functions
86
     *
87
     * @return callable[]
88
     */
89
    public function getSortBy()
90
    {
91
        return $this->sort_by;
92
    }
93
94
    /**
95
     * Return the limit clause
96
     *
97
     * @return int
98
     */
99
    public function getLimit()
100
    {
101
        return $this->limit;
102
    }
103
104
    /**
105
     * Return the offset clause
106
     *
107
     * @return int
108
     */
109
    public function getOffset()
110
    {
111
        return $this->offset;
112
    }
113
114
    /**
115
     * Set the offset clause
116
     *
117
     * @param $offset
118
     *
119
     * @return static
120
     */
121
    public function setOffset($offset)
122
    {
123
        $offset = $this->validateInteger($offset, 0, 'the offset must be a positive integer or 0');
124
        if ($offset === $this->offset) {
125
            return $this;
126
        }
127
128
        $clone = clone $this;
129
        $clone->offset = $offset;
130
131
        return $clone;
132
    }
133
134
    /**
135
     * Set the limit clause
136
     *
137
     * @param int $limit
138
     *
139
     * @return static
140
     */
141
    public function setLimit($limit)
142
    {
143
        $limit = $this->validateInteger($limit, -1, 'the limit must an integer greater or equals to -1');
144
        if ($limit === $this->limit) {
145
            return $this;
146
        }
147
148
        $clone = clone $this;
149
        $clone->limit = $limit;
150
151
        return $clone;
152
    }
153
154
    /**
155
     * Add a sorting callable function
156
     *
157
     * @param callable $callable
158
     *
159
     * @return static
160
     */
161
    public function addSortBy(callable $callable)
162
    {
163
        $clone = clone $this;
164
        $clone->sort_by[] = $callable;
165
166
        return $clone;
167
    }
168
169
    /**
170
     *Add a filter callable function
171
     *
172
     * @param callable $callable
173
     *
174
     * @return static
175
     */
176
    public function addFilter(callable $callable)
177
    {
178
        $clone = clone $this;
179
        $clone->filters[] = $callable;
180
181
        return $clone;
182
    }
183
184
    /**
185
     * Returns a Record object
186
     *
187
     * @param Reader $csv
188
     *
189
     * @return RecordSet
190
     */
191
    public function process(Reader $csv)
192
    {
193
        return new RecordSet($csv, $this);
194
    }
195
}
196