Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Label Functionality #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/Trello/Api/Board/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
45 changes: 43 additions & 2 deletions lib/Trello/Api/Card/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
Expand All @@ -38,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
Expand All @@ -52,7 +93,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));
}

Expand Down