Completed
Pull Request — master (#32)
by
unknown
03:22
created

Card::setClosed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Trello\Api;
4
5
use Trello\Exception\InvalidArgumentException;
6
7
/**
8
 * Trello Card API
9
 * @link https://trello.com/docs/api/card
10
 *
11
 * Unimplemented:
12
 * - https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-checklist-idchecklist-checkitem-idcheckitem-name
13
 * - https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-checklist-idchecklist-checkitem-idcheckitem-pos
14
 * - https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-checklist-idchecklist-checkitem-idcheckitem-state
15
 * - https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-checklist-idchecklistcurrent-checkitem-idcheckitem
16
 * - https://trello.com/docs/api/card/#post-1-cards-card-id-or-shortlink-checklist-idchecklist-checkitem
17
 * - https://trello.com/docs/api/card/#post-1-cards-card-id-or-shortlink-checklist-idchecklist-checkitem-idcheckitem-converttocard
18
 * - https://trello.com/docs/api/card/#post-1-cards-card-id-or-shortlink-markassociatednotificationsread
19
 */
20
class Card extends AbstractApi
21
{
22
    /**
23
     * Base path of cards api
24
     * @var string
25
     */
26
    protected $path = 'cards';
27
28
    /**
29
     * Card fields
30
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-field
31
     * @var array
32
     */
33
    public static $fields = array(
34
        'badges',
35
        'checkItemStates',
36
        'closed',
37
        'dateLastActivity',
38
        'desc',
39
        'descData',
40
        'due',
41
        'email',
42
        'idBoard',
43
        'idChecklists',
44
        'idList',
45
        'idMembers',
46
        'idMembersVoted',
47
        'idShort',
48
        'idAttachmentCover',
49
        'manualCoverAttachment',
50
        'labels',
51
        'name',
52
        'pos',
53
        'shortLink',
54
        'shortUrl',
55
        'subscribed',
56
        'url',
57
    );
58
59
    /**
60
     * Find a card by id
61
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink
62
     *
63
     * @param string $id     the card's id or short link
64
     * @param array  $params optional attributes
65
     *
66
     * @return array card info
67
     */
68 1
    public function show($id, array $params = array())
69
    {
70 1
        return $this->get($this->getPath().'/'.rawurlencode($id), $params);
71
    }
72
73
    /**
74
     * Create a card
75
     * @link https://trello.com/docs/api/card/#post-1-cards
76
     *
77
     * @param array  $params optional attributes
78
     *
79
     * @return array card info
80
     */
81 3
    public function create(array $params = array())
82
    {
83 3
        $this->validateRequiredParameters(array('idList', 'name'), $params);
84
85 1
        if (!array_key_exists('due', $params)) {
86 1
            $params['due'] = null;
87 1
        }
88 1
        if (!array_key_exists('urlSource', $params)) {
89 1
            $params['urlSource'] = null;
90 1
        }
91 1
        if(array_key_exists('labels', $params) && count($params['labels'])){
92
            $params['labels'] = implode(',', $params['labels']);
93
        }
94
95 1
        return $this->post($this->getPath(), $params);
96
    }
97
98
    /**
99
     * Update a card
100
     * @link https://trello.com/docs/api/card/#put-1-cards
101
     *
102
     * @param string $id     the card's id or short link
103
     * @param array  $params card attributes to update
104
     *
105
     * @return array card info
106
     */
107 1
    public function update($id, array $params = array())
108
    {
109 1
        return $this->put($this->getPath().'/'.rawurlencode($id), $params);
110
    }
111
112
    /**
113
     * Set a given card's board
114
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-idboard
115
     *
116
     * @param string $id      the card's id or short link
117
     * @param string $boardId the board's id
118
     *
119
     * @return array board info
120
     */
121 1
    public function setBoard($id, $boardId)
122
    {
123 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/idBoard', array('value' => $boardId));
124
    }
125
126
    /**
127
     * Get a given card's board
128
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-idboard
129
     *
130
     * @param string $id     the card's id or short link
131
     * @param array  $params optional parameters
132
     *
133
     * @return array board info
134
     */
135 1
    public function getBoard($id, array $params = array())
136
    {
137 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/board', $params);
138
    }
139
140
    /**
141
     * Get the field of a board of a given card
142
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-board-field
143
     *
144
     * @param string $id    the card's id
145
     * @param array  $field the name of the field
146
     *
147
     * @return array board info
148
     *
149
     * @throws InvalidArgumentException if the field does not exist
150
     */
151 2
    public function getBoardField($id, $field)
152
    {
153 2
        $this->validateAllowedParameters(Board::$fields, $field, 'field');
154
155 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/board/'.rawurlencode($field));
156
    }
157
158
    /**
159
     * Set a given card's list
160
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-idlist
161
     *
162
     * @param string $id     the card's id or short link
163
     * @param string $listId the list's id
164
     *
165
     * @return array list info
166
     */
167 1
    public function setList($id, $listId)
168
    {
169 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/idList', array('value' => $listId));
170
    }
171
172
    /**
173
     * Get a given card's list
174
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-list
175
     *
176
     * @param string $id     the card's id or short link
177
     * @param array  $params optional parameters
178
     *
179
     * @return array list info
180
     */
181 1
    public function getList($id, array $params = array())
182
    {
183 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/list', $params);
184
    }
185
186
    /**
187
     * Get the field of a list of a given card
188
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-list-field
189
     *
190
     * @param string $id    the card's id
191
     * @param array  $field the name of the field
192
     *
193
     * @return array board info
194
     *
195
     * @throws InvalidArgumentException if the field does not exist
196
     */
197 2
    public function getListField($id, $field)
198
    {
199 2
        $this->validateAllowedParameters(Cardlist::$fields, $field, 'field');
200
201 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/list/'.rawurlencode($field));
202
    }
203
204
    /**
205
     * Set a given card's name
206
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-name
207
     *
208
     * @param string $id   the card's id or short link
209
     * @param string $name the name
210
     *
211
     * @return array card info
212
     */
213 1
    public function setName($id, $name)
214
    {
215 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/name', array('value' => $name));
216
    }
217
218
    /**
219
     * Set a given card's description
220
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-desc
221
     *
222
     * @param string $id          the card's id or short link
223
     * @param string $description the description
224
     *
225
     * @return array card info
226
     */
227 1
    public function setDescription($id, $description)
228
    {
229 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/desc', array('value' => $description));
230
    }
231
232
    /**
233
     * Set a given card's state
234
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-closed
235
     *
236
     * @param string $id     the card's id or short link
237
     * @param bool   $closed whether the card should be closed or not
238
     *
239
     * @return array card info
240
     */
241 1
    public function setClosed($id, $closed = true)
242
    {
243 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/closed', array('value' => $closed));
244
    }
245
246
    /**
247
     * Set a given card's due date
248
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-due
249
     *
250
     * @param string    $id   the card's id or short link
251
     * @param \DateTime $date the due date
252
     *
253
     * @return array card info
254
     */
255 1
    public function setDueDate($id, \DateTime $date = null)
256
    {
257 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/due', array('value' => $date));
258
    }
259
260
    /**
261
     * Set a given card's position
262
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-pos
263
     *
264
     * @param string         $id       the card's id or short link
265
     * @param string|integer $position the position, eg. 'top', 'bottom'
266
     *                                 or a positive number
267
     *
268
     * @return array card info
269
     */
270 1
    public function setPosition($id, $position)
271
    {
272 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/pos', array('value' => $position));
273
    }
274
275
    /**
276
     * Set a given card's subscription state
277
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-subscribed
278
     *
279
     * @param string $id         the list's id
280
     * @param bool   $subscribed subscription state
281
     *
282
     * @return array list info
283
     */
284 1
    public function setSubscribed($id, $subscribed)
285
    {
286 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/subscribed', array('value' => $subscribed));
287
    }
288
289
    /**
290
     * Actions API
291
     *
292
     * @return Card\Actions
293
     */
294 1
    public function actions()
295
    {
296 1
        return new Card\Actions($this->client);
297
    }
298
299
    /**
300
     * Attachments API
301
     *
302
     * @return Card\Attachments
303
     */
304 1
    public function attachments()
305
    {
306 1
        return new Card\Attachments($this->client);
307
    }
308
309
    /**
310
     * Checklists API
311
     *
312
     * @return Card\Checklists
313
     */
314 1
    public function checklists()
315
    {
316 1
        return new Card\Checklists($this->client);
317
    }
318
319
    /**
320
     * Labels API
321
     *
322
     * @return Card\Labels
323
     */
324 1
    public function labels()
325
    {
326 1
        return new Card\Labels($this->client);
327
    }
328
329
    /**
330
     * Members API
331
     *
332
     * @return Card\Members
333
     */
334 1
    public function members()
335
    {
336 1
        return new Card\Members($this->client);
337
    }
338
339
    /**
340
     * Stickers API
341
     *
342
     * @return Card\Stickers
343
     */
344 1
    public function stickers()
345
    {
346 1
        return new Card\Stickers($this->client);
347
    }
348
}
349