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

Card::getBoard()   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
            $labels = array();
93
94
            foreach($params['labels'] as $label){
95
                $labels[] = $label['color'];
96
            }
97
98
            $params['labels'] = implode(',', $labels);
99
        }
100
101 1
        return $this->post($this->getPath(), $params);
102
    }
103
104
    /**
105
     * Update a card
106
     * @link https://trello.com/docs/api/card/#put-1-cards
107
     *
108
     * @param string $id     the card's id or short link
109
     * @param array  $params card attributes to update
110
     *
111
     * @return array card info
112
     */
113 1
    public function update($id, array $params = array())
114
    {
115 1
        return $this->put($this->getPath().'/'.rawurlencode($id), $params);
116
    }
117
118
    /**
119
     * Set a given card's board
120
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-idboard
121
     *
122
     * @param string $id      the card's id or short link
123
     * @param string $boardId the board's id
124
     *
125
     * @return array board info
126
     */
127 1
    public function setBoard($id, $boardId)
128
    {
129 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/idBoard', array('value' => $boardId));
130
    }
131
132
    /**
133
     * Get a given card's board
134
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-idboard
135
     *
136
     * @param string $id     the card's id or short link
137
     * @param array  $params optional parameters
138
     *
139
     * @return array board info
140
     */
141 1
    public function getBoard($id, array $params = array())
142
    {
143 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/board', $params);
144
    }
145
146
    /**
147
     * Get the field of a board of a given card
148
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-board-field
149
     *
150
     * @param string $id    the card's id
151
     * @param array  $field the name of the field
152
     *
153
     * @return array board info
154
     *
155
     * @throws InvalidArgumentException if the field does not exist
156
     */
157 2
    public function getBoardField($id, $field)
158
    {
159 2
        $this->validateAllowedParameters(Board::$fields, $field, 'field');
160
161 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/board/'.rawurlencode($field));
162
    }
163
164
    /**
165
     * Set a given card's list
166
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-idlist
167
     *
168
     * @param string $id     the card's id or short link
169
     * @param string $listId the list's id
170
     *
171
     * @return array list info
172
     */
173 1
    public function setList($id, $listId)
174
    {
175 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/idList', array('value' => $listId));
176
    }
177
178
    /**
179
     * Get a given card's list
180
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-list
181
     *
182
     * @param string $id     the card's id or short link
183
     * @param array  $params optional parameters
184
     *
185
     * @return array list info
186
     */
187 1
    public function getList($id, array $params = array())
188
    {
189 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/list', $params);
190
    }
191
192
    /**
193
     * Get the field of a list of a given card
194
     * @link https://trello.com/docs/api/card/#get-1-cards-card-id-or-shortlink-list-field
195
     *
196
     * @param string $id    the card's id
197
     * @param array  $field the name of the field
198
     *
199
     * @return array board info
200
     *
201
     * @throws InvalidArgumentException if the field does not exist
202
     */
203 2
    public function getListField($id, $field)
204
    {
205 2
        $this->validateAllowedParameters(Cardlist::$fields, $field, 'field');
206
207 1
        return $this->get($this->getPath().'/'.rawurlencode($id).'/list/'.rawurlencode($field));
208
    }
209
210
    /**
211
     * Set a given card's name
212
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-name
213
     *
214
     * @param string $id   the card's id or short link
215
     * @param string $name the name
216
     *
217
     * @return array card info
218
     */
219 1
    public function setName($id, $name)
220
    {
221 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/name', array('value' => $name));
222
    }
223
224
    /**
225
     * Set a given card's description
226
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-desc
227
     *
228
     * @param string $id          the card's id or short link
229
     * @param string $description the description
230
     *
231
     * @return array card info
232
     */
233 1
    public function setDescription($id, $description)
234
    {
235 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/desc', array('value' => $description));
236
    }
237
238
    /**
239
     * Set a given card's state
240
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-closed
241
     *
242
     * @param string $id     the card's id or short link
243
     * @param bool   $closed whether the card should be closed or not
244
     *
245
     * @return array card info
246
     */
247 1
    public function setClosed($id, $closed = true)
248
    {
249 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/closed', array('value' => $closed));
250
    }
251
252
    /**
253
     * Set a given card's due date
254
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-due
255
     *
256
     * @param string    $id   the card's id or short link
257
     * @param \DateTime $date the due date
258
     *
259
     * @return array card info
260
     */
261 1
    public function setDueDate($id, \DateTime $date = null)
262
    {
263 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/due', array('value' => $date));
264
    }
265
266
    /**
267
     * Set a given card's position
268
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-pos
269
     *
270
     * @param string         $id       the card's id or short link
271
     * @param string|integer $position the position, eg. 'top', 'bottom'
272
     *                                 or a positive number
273
     *
274
     * @return array card info
275
     */
276 1
    public function setPosition($id, $position)
277
    {
278 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/pos', array('value' => $position));
279
    }
280
281
    /**
282
     * Set a given card's subscription state
283
     * @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-subscribed
284
     *
285
     * @param string $id         the list's id
286
     * @param bool   $subscribed subscription state
287
     *
288
     * @return array list info
289
     */
290 1
    public function setSubscribed($id, $subscribed)
291
    {
292 1
        return $this->put($this->getPath().'/'.rawurlencode($id).'/subscribed', array('value' => $subscribed));
293
    }
294
295
    /**
296
     * Actions API
297
     *
298
     * @return Card\Actions
299
     */
300 1
    public function actions()
301
    {
302 1
        return new Card\Actions($this->client);
303
    }
304
305
    /**
306
     * Attachments API
307
     *
308
     * @return Card\Attachments
309
     */
310 1
    public function attachments()
311
    {
312 1
        return new Card\Attachments($this->client);
313
    }
314
315
    /**
316
     * Checklists API
317
     *
318
     * @return Card\Checklists
319
     */
320 1
    public function checklists()
321
    {
322 1
        return new Card\Checklists($this->client);
323
    }
324
325
    /**
326
     * Labels API
327
     *
328
     * @return Card\Labels
329
     */
330 1
    public function labels()
331
    {
332 1
        return new Card\Labels($this->client);
333
    }
334
335
    /**
336
     * Members API
337
     *
338
     * @return Card\Members
339
     */
340 1
    public function members()
341
    {
342 1
        return new Card\Members($this->client);
343
    }
344
345
    /**
346
     * Stickers API
347
     *
348
     * @return Card\Stickers
349
     */
350 1
    public function stickers()
351
    {
352 1
        return new Card\Stickers($this->client);
353
    }
354
}
355