StatementsFilter::includeAttachments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[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
namespace Xabbuh\XApi\Model;
13
14
/**
15
 * Filter to apply on GET requests to the statements API.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
class StatementsFilter
20
{
21
    /**
22
     * @var array The generated filter
23
     */
24
    private $filter = array();
25
26
    /**
27
     * Filters by an Agent or an identified Group.
28
     *
29
     * @param Actor $actor The Actor to filter by
30
     *
31
     * @return self The statements filter
32
     *
33
     * @throws \InvalidArgumentException if the Actor is not identified
34
     */
35
    public function byActor(Actor $actor)
36
    {
37
        if (null === $actor->getInverseFunctionalIdentifier()) {
38
            throw new \InvalidArgumentException('Actor must be identified');
39
        }
40
41
        $this->filter['agent'] = $actor;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Filters by a verb.
48
     *
49
     * @param Verb $verb The Verb to filter by
50
     *
51
     * @return self The statements filter
52
     */
53
    public function byVerb(Verb $verb)
54
    {
55
        $this->filter['verb'] = $verb->getId();
56
57
        return $this;
58
    }
59
60
    /**
61
     * Filter by an Activity.
62
     *
63
     * @param Activity $activity The Activity to filter by
64
     *
65
     * @return self The statements filter
66
     */
67
    public function byActivity(Activity $activity)
68
    {
69
        $this->filter['activity'] = $activity->getId();
70
71
        return $this;
72
    }
73
74
    /**
75
     * Filters for Statements matching the given registration id.
76
     *
77
     * @param string $registration A registration id
78
     *
79
     * @return self The statements filter
80
     */
81
    public function byRegistration($registration)
82
    {
83
        $this->filter['registration'] = $registration;
84
    }
85
86
    /**
87
     * Applies the Activity filter to Sub-Statements.
88
     *
89
     * @return self The statements filter
90
     */
91
    public function enableRelatedActivityFilter()
92
    {
93
        $this->filter['related_activities'] = true;
94
    }
95
96
    /**
97
     * Don't apply the Activity filter to Sub-Statements.
98
     *
99
     * @return self The statements filter
100
     */
101
    public function disableRelatedActivityFilter()
102
    {
103
        $this->filter['related_activities'] = false;
104
    }
105
106
    /**
107
     * Applies the Agent filter to Sub-Statements.
108
     *
109
     * @return self The statements filter
110
     */
111
    public function enableRelatedAgentFilter()
112
    {
113
        $this->filter['related_agents'] = true;
114
    }
115
116
    /**
117
     * Don't apply the Agent filter to Sub-Statements.
118
     *
119
     * @return self The statements filter
120
     */
121
    public function disableRelatedAgentFilter()
122
    {
123
        $this->filter['related_agents'] = false;
124
    }
125
126
    /**
127
     * Filters for Statements stored since the specified timestamp (exclusive).
128
     *
129
     * @param \DateTime $timestamp The timestamp
130
     *
131
     * @return self The statements filter
132
     */
133
    public function since(\DateTime $timestamp)
134
    {
135
        $this->filter['since'] = $timestamp->format('c');
136
137
        return $this;
138
    }
139
140
    /**
141
     * Filters for Statements stored at or before the specified timestamp.
142
     *
143
     * @param \DateTime $timestamp The timestamp as a unix timestamp
144
     *
145
     * @return self The statements filter
146
     */
147
    public function until(\DateTime $timestamp)
148
    {
149
        $this->filter['until'] = $timestamp->format('c');
150
151
        return $this;
152
    }
153
154
    /**
155
     * Sets the maximum number of Statements to return. The server side sets
156
     * the maximum number of results when this value is not set or when it is 0.
157
     *
158
     * @param int $limit Maximum number of Statements to return
159
     *
160
     * @return self The statements filter
161
     *
162
     * @throws \InvalidArgumentException if the limit is not a non-negative
163
     *                                   integer
164
     */
165
    public function limit($limit)
166
    {
167
        if ($limit < 0) {
168
            throw new \InvalidArgumentException('Limit must be a non-negative integer');
169
        }
170
171
        $this->filter['limit'] = $limit;
172
173
        return $this;
174
    }
175
176
    /**
177
     * Specifies the format of the StatementResult being returned.
178
     *
179
     * "ids": Includes only information for the Agent, Activity and Group
180
     * needed to identify them.
181
     *
182
     * "exact": Agents, Groups and Activities will be returned as they were when
183
     * the Statements where received by the LRS.
184
     *
185
     * "canonical": For objects containing language maps, only the most appropriate
186
     * language will be returned. Agent objects will be returned as if the "exact"
187
     * format was given.
188
     *
189
     * @param string $format A valid format identifier (one of "ids", "exact"
190
     *                       or "canonical"
191
     *
192
     * @return self The statements filter
193
     *
194
     * @throws \InvalidArgumentException if no valid format is given
195
     */
196
    public function format($format)
197
    {
198
        if (!in_array($format, array('ids', 'exact', 'canonical'))) {
199
            throw new \InvalidArgumentException('Unknown format '.$format.' given');
200
        }
201
202
        $this->filter['format'] = $format;
203
    }
204
205
    /**
206
     * Query attachments for each Statement being returned.
207
     *
208
     * @return self The statements filter
209
     */
210
    public function includeAttachments()
211
    {
212
        $this->filter['attachments'] = true;
213
    }
214
215
    /**
216
     * Don't query for Statement attachments (the default behavior).
217
     *
218
     * @return self The statements filter
219
     */
220
    public function excludeAttachments()
221
    {
222
        $this->filter['attachments'] = false;
223
    }
224
225
    /**
226
     * Return statements in ascending order of stored time.
227
     *
228
     * @return self The statements filter
229
     */
230
    public function ascending()
231
    {
232
        $this->filter['ascending'] = 'True';
233
234
        return $this;
235
    }
236
237
    /**
238
     *Return statements in descending order of stored time (the default behavior).
239
     *
240
     * @return self The statements filter
241
     */
242
    public function descending()
243
    {
244
        $this->filter['ascending'] = 'False';
245
246
        return $this;
247
    }
248
249
    /**
250
     * Returns the generated filter.
251
     *
252
     * @return array The filter
253
     */
254
    public function getFilter()
255
    {
256
        return $this->filter;
257
    }
258
}
259