Completed
Pull Request — master (#178)
by ignace nyamagana
02:27
created

Statement::addSortBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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
 * Immutable value object to generate statements
21
 * to select records against a {@link Reader} object
22
 *
23
 * @package League.csv
24
 * @since  9.0.0
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
     * Triggered when writing data to inaccessible properties
60
     *
61
     * @param string $property property name
62
     * @param mixed  $value    property value
63
     *
64
     * @throws InvalidArgumentException for all undefined properties
65
     */
66
    public function __set($property, $value)
67
    {
68
        throw new InvalidArgumentException(sprintf('%s is an undefined property', $property));
69
    }
70
71
    /**
72
     * Triggered when __unset is used on inaccessible properties
73
     *
74
     * @param string $property property name
75
     *
76
     * @throws InvalidArgumentException for all undefined properties
77
     */
78
    public function __unset($property)
79
    {
80
        throw new InvalidArgumentException(sprintf('%s is an undefined property', $property));
81
    }
82
83
    /**
84
     * Returns the added filter callable functions
85
     *
86
     * @return callable[]
87
     */
88
    public function getFilter()
89
    {
90
        return $this->filters;
91
    }
92
93
    /**
94
     * Returns the added sorting callable functions
95
     *
96
     * @return callable[]
97
     */
98
    public function getSortBy()
99
    {
100
        return $this->sort_by;
101
    }
102
103
    /**
104
     * Returns the limit clause
105
     *
106
     * @return int
107
     */
108
    public function getLimit()
109
    {
110
        return $this->limit;
111
    }
112
113
    /**
114
     * Returns the offset clause
115
     *
116
     * @return int
117
     */
118
    public function getOffset()
119
    {
120
        return $this->offset;
121
    }
122
123
    /**
124
     * Sets the offset clause
125
     *
126
     * @param $offset
127
     *
128
     * @return static
129
     */
130
    public function setOffset($offset)
131
    {
132
        $offset = $this->filterInteger($offset, 0, 'the offset must be a positive integer or 0');
133
        if ($offset === $this->offset) {
134
            return $this;
135
        }
136
137
        $clone = clone $this;
138
        $clone->offset = $offset;
139
140
        return $clone;
141
    }
142
143
    /**
144
     * Sets the limit clause
145
     *
146
     * @param int $limit
147
     *
148
     * @return static
149
     */
150
    public function setLimit($limit)
151
    {
152
        $limit = $this->filterInteger($limit, -1, 'the limit must an integer greater or equals to -1');
153
        if ($limit === $this->limit) {
154
            return $this;
155
        }
156
157
        $clone = clone $this;
158
        $clone->limit = $limit;
159
160
        return $clone;
161
    }
162
163
    /**
164
     * Adds a sorting callable function
165
     *
166
     * @param callable $callable
167
     *
168
     * @return static
169
     */
170
    public function addSortBy(callable $callable)
171
    {
172
        $clone = clone $this;
173
        $clone->sort_by[] = $callable;
174
175
        return $clone;
176
    }
177
178
    /**
179
     *Adds a filter callable function
180
     *
181
     * @param callable $callable
182
     *
183
     * @return static
184
     */
185
    public function addFilter(callable $callable)
186
    {
187
        $clone = clone $this;
188
        $clone->filters[] = $callable;
189
190
        return $clone;
191
    }
192
193
    /**
194
     * Returns a collection of selected records
195
     * from the submitted {@link Reader} object
196
     *
197
     * @param Reader $csv
198
     *
199
     * @return RecordSet
200
     */
201
    public function process(Reader $csv)
202
    {
203
        return new RecordSet($csv, $this);
204
    }
205
}
206