From 9fa26b8e6c8537ac4673f71a7d6c4070d85442f3 Mon Sep 17 00:00:00 2001 From: Tanner McColeman Date: Tue, 15 Sep 2015 14:55:05 -0700 Subject: [PATCH 1/3] Add colors, adhere to DRY principals Add the valid color options in a protected property to minimize repetition --- lib/Trello/Api/Card/Labels.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Trello/Api/Card/Labels.php b/lib/Trello/Api/Card/Labels.php index a8cfbfd..350d1a2 100644 --- a/lib/Trello/Api/Card/Labels.php +++ b/lib/Trello/Api/Card/Labels.php @@ -14,6 +14,21 @@ class Labels extends AbstractApi { protected $path = 'cards/#id#/labels'; + + protected $colors = array( + 'all', + 'green', + 'yellow', + 'orange', + 'red', + 'purple', + 'blue', + 'sky', + 'lime', + 'pink', + 'black', + 'null' + ); /** * Set a given card's labels @@ -29,7 +44,7 @@ class Labels extends AbstractApi public function set($id, array $labels) { foreach ($labels as $label) { - if (!in_array($label, array('all', 'green', 'yellow', 'orange', 'red', 'purple', 'blue'))) { + if (!in_array($label, $this->colors)) { throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $label)); } } @@ -52,7 +67,7 @@ public function set($id, array $labels) */ public function remove($id, $label) { - if (!in_array($label, array('green', 'yellow', 'orange', 'red', 'purple', 'blue'))) { + if (!in_array($label, $this->colors)) { throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $label)); } From 1ecb022dc364be13105ac91805285224dc29c334 Mon Sep 17 00:00:00 2001 From: Tanner McColeman Date: Tue, 15 Sep 2015 15:00:20 -0700 Subject: [PATCH 2/3] Show board colors by idLabel Remove the color requirements of Trello\Api\Board\Labels::show() to allow for the use of either a color or ID. --- lib/Trello/Api/Board/Labels.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/Trello/Api/Board/Labels.php b/lib/Trello/Api/Board/Labels.php index f924f76..c1033a5 100644 --- a/lib/Trello/Api/Board/Labels.php +++ b/lib/Trello/Api/Board/Labels.php @@ -38,21 +38,12 @@ public function all($id, array $params = array()) * @link https://trello.com/docs/api/board/#get-1-boards-board-id-labels-idlabel * * @param string $id the board's id - * @param string $color the label's color + * @param string $color the label's color or ID * * @return array */ public function show($id, $color) { - $colors = array('blue', 'green', 'orange', 'purple', 'red', 'yellow'); - - if (!in_array($color, $colors)) { - throw new InvalidArgumentException(sprintf( - 'The "color" parameter must be one of "%s".', - implode(", ", $colors) - )); - } - return $this->get($this->getPath($id).'/'.rawurlencode($color)); } From 11826fce009501884174fb6d03b9396ffe25f97a Mon Sep 17 00:00:00 2001 From: Tanner McColeman Date: Tue, 15 Sep 2015 15:40:32 -0700 Subject: [PATCH 3/3] Add Named Label Functionality to Cards Created the Card\Labels::add() command that allows appending a label (optionally named) to a card. --- lib/Trello/Api/Card/Labels.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/Trello/Api/Card/Labels.php b/lib/Trello/Api/Card/Labels.php index 350d1a2..8d65f85 100644 --- a/lib/Trello/Api/Card/Labels.php +++ b/lib/Trello/Api/Card/Labels.php @@ -53,6 +53,32 @@ public function set($id, array $labels) return $this->put($this->getPath($id), array('value' => $labels)); } + + /** + * Add a label to a given card + * @link https://trello.com/docs/api/card/#post-1-cards-card-id-or-shortlink-labels + * + * @param string $id the card's id or short link + * @param string $color the label's color + * @param string $name the label's name (optional) + * + * @return array label info + * + * @throws InvalidArgumentException If a label does not exist + */ + public function add($id, $color, $name = null) + { + if (!in_array($color, $this->colors)) { + throw new InvalidArgumentException(sprintf('Label "%s" does not exist.', $color)); + } + + $args = array( + 'color' => $color, + 'name' => $name + ); + + return $this->post($this->getPath($id), $args); + } /** * Remove a given label from a given card