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

Statement::getFilter()   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 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
 */
27
class Statement
28
{
29
    use Validator;
30
31
    /**
32
     * Callables to filter the iterator
33
     *
34
     * @var callable[]
35
     */
36
    protected $filters = [];
37
38
    /**
39
     * Callables to sort the iterator
40
     *
41
     * @var callable[]
42
     */
43
    protected $sort_by = [];
44
45
    /**
46
     * iterator Offset
47
     *
48
     * @var int
49
     */
50
    protected $offset = 0;
51
52
    /**
53
     * iterator maximum length
54
     *
55
     * @var int
56
     */
57
    protected $limit = -1;
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function __set($property, $value)
63
    {
64
        throw new InvalidArgumentException(sprintf('%s is an undefined property', $property));
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function __unset($property)
71
    {
72
        throw new InvalidArgumentException(sprintf('%s is an undefined property', $property));
73
    }
74
75
    /**
76
     * Return the added filter callable functions
77
     *
78
     * @return callable[]
79
     */
80
    public function getFilter()
81
    {
82
        return $this->filters;
83
    }
84
85
    /**
86
     * Return the added sorting callable functions
87
     *
88
     * @return callable[]
89
     */
90
    public function getSortBy()
91
    {
92
        return $this->sort_by;
93
    }
94
95
    /**
96
     * Return the limit clause
97
     *
98
     * @return int
99
     */
100
    public function getLimit()
101
    {
102
        return $this->limit;
103
    }
104
105
    /**
106
     * Return the offset clause
107
     *
108
     * @return int
109
     */
110
    public function getOffset()
111
    {
112
        return $this->offset;
113
    }
114
115
    /**
116
     * Set the offset clause
117
     *
118
     * @param $offset
119
     *
120
     * @return static
121
     */
122
    public function setOffset($offset)
123
    {
124
        $offset = $this->validateInteger($offset, 0, 'the offset must be a positive integer or 0');
125
        if ($offset === $this->offset) {
126
            return $this;
127
        }
128
129
        $clone = clone $this;
130
        $clone->offset = $offset;
131
132
        return $clone;
133
    }
134
135
    /**
136
     * Set the limit clause
137
     *
138
     * @param int $limit
139
     *
140
     * @return static
141
     */
142
    public function setLimit($limit)
143
    {
144
        $limit = $this->validateInteger($limit, -1, 'the limit must an integer greater or equals to -1');
145
        if ($limit === $this->limit) {
146
            return $this;
147
        }
148
149
        $clone = clone $this;
150
        $clone->limit = $limit;
151
152
        return $clone;
153
    }
154
155
    /**
156
     * Add a sorting callable function
157
     *
158
     * @param callable $callable
159
     *
160
     * @return static
161
     */
162
    public function addSortBy(callable $callable)
163
    {
164
        $clone = clone $this;
165
        $clone->sort_by[] = $callable;
166
167
        return $clone;
168
    }
169
170
    /**
171
     *Add a filter callable function
172
     *
173
     * @param callable $callable
174
     *
175
     * @return static
176
     */
177
    public function addFilter(callable $callable)
178
    {
179
        $clone = clone $this;
180
        $clone->filters[] = $callable;
181
182
        return $clone;
183
    }
184
185
    /**
186
     * Returns a Record object
187
     *
188
     * @param Reader $csv
189
     *
190
     * @return RecordSet
191
     */
192
    public function process(Reader $csv)
193
    {
194
        return new RecordSet($csv, $this);
195
    }
196
}
197