1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Trello\Api\Card; |
4
|
|
|
|
5
|
|
|
use Trello\Api\AbstractApi; |
6
|
|
|
use Trello\Exception\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trello Card Labels API |
10
|
|
|
* @link https://trello.com/docs/api/card |
11
|
|
|
* |
12
|
|
|
* Fully implemented. |
13
|
|
|
*/ |
14
|
|
|
class Labels extends AbstractApi |
15
|
|
|
{ |
16
|
|
|
protected $path = 'cards/#id#/labels'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set a given card's labels |
20
|
|
|
* @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-labels |
21
|
|
|
* |
22
|
|
|
* @param string $id the card's id or short link |
23
|
|
|
* @param array $labels the labels |
24
|
|
|
* |
25
|
|
|
* @return array card info |
26
|
|
|
* |
27
|
|
|
* @throws InvalidArgumentException If a label does not exist |
28
|
|
|
*/ |
29
|
2 |
|
public function set($id, array $labels) |
30
|
|
|
{ |
31
|
2 |
|
foreach ($labels as $label) { |
32
|
2 |
|
if (!in_array($label, array('all', 'green', 'yellow', 'orange', 'red', 'purple', 'blue'))) { |
33
|
1 |
|
throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $label)); |
34
|
|
|
} |
35
|
1 |
|
} |
36
|
|
|
|
37
|
1 |
|
$labels = implode(',', $labels); |
38
|
|
|
|
39
|
1 |
|
return $this->put($this->getPath($id), array('value' => $labels)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set a given card's label by labelId |
44
|
|
|
* @link https://trello.com/docs/api/card/#put-1-cards-card-id-or-shortlink-labels |
45
|
|
|
* |
46
|
|
|
* @param string $id the card's id or short link |
47
|
|
|
* @param string $labelId the label id |
48
|
|
|
* |
49
|
|
|
* @return array card info |
50
|
|
|
* |
51
|
|
|
* @throws InvalidArgumentException If a label does not exist |
52
|
|
|
*/ |
53
|
2 |
|
public function setById($id, $labelId) |
54
|
|
|
{ |
55
|
2 |
|
//first set the right path based on the documentation; |
56
|
1 |
|
$this->path = 'cards/#id#/idLabels'; |
57
|
|
|
|
58
|
|
|
return $this->post($this->getPath($id), array('value' => $labelId)); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Remove a given label from a given card |
63
|
|
|
* @link https://trello.com/docs/api/card/#delete-1-cards-card-id-or-shortlink-labels-color |
64
|
|
|
* |
65
|
|
|
* @param string $id the card's id or short link |
66
|
|
|
* @param string $label the label to remove |
67
|
|
|
* |
68
|
|
|
* @return array card info |
69
|
|
|
* |
70
|
|
|
* @throws InvalidArgumentException If a label does not exist |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function remove($id, $label) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
if (!in_array($label, array('green', 'yellow', 'orange', 'red', 'purple', 'blue'))) { |
75
|
|
|
throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $label)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->delete($this->getPath($id).'/'.rawurlencode($label)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Remove a given label from a given card based on the label id |
83
|
|
|
* @link https://developers.trello.com/advanced-reference/card#delete-1-cards-card-id-or-shortlink-idlabels-idlabel |
84
|
|
|
* |
85
|
|
|
* @param string $id the card's id or short link |
86
|
|
|
* @param string $labelId the label id to remove |
87
|
|
|
* |
88
|
|
|
* @return array card info |
89
|
|
|
* |
90
|
|
|
* @throws InvalidArgumentException If a label does not exist |
91
|
|
|
*/ |
92
|
|
|
public function removeById($id, $labelId) |
93
|
|
|
{ |
94
|
|
|
$this->path = 'cards/#id#/idLabels'; |
95
|
|
|
return $this->delete($this->getPath($id).'/'.rawurlencode($labelId)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.