diff --git a/docs/Event/CardLabelEvent.md b/docs/Event/CardLabelEvent.md new file mode 100644 index 0000000..43add98 --- /dev/null +++ b/docs/Event/CardLabelEvent.md @@ -0,0 +1,13 @@ + +====================== + +### Set label +```php +setLabel(\Trello\Model\LabelInterface $label) +``` + +### Get label +```php +getLabel() +``` + diff --git a/docs/Manager.md b/docs/Manager.md index 7a68fce..a771400 100644 --- a/docs/Manager.md +++ b/docs/Manager.md @@ -26,6 +26,11 @@ getList(string $id) getCard(string $id) ``` +### Get label by id or create a new one +```php +getLabel(string $id) +``` + ### Get checklist by id or create a new one ```php getChecklist(string $id) diff --git a/docs/Model/Board.md b/docs/Model/Board.md index bf81b40..c701a48 100644 --- a/docs/Model/Board.md +++ b/docs/Model/Board.md @@ -151,6 +151,16 @@ setLabelNames(array $labelNames) getLabelNames() ``` +### Get labels +```php +getLabels() +``` + +### Get label that has name $name and color $color +```php +getLabel($name, $color) +``` + ### Set power ups ```php setPowerUps(array $powerUps) diff --git a/docs/Model/BoardInterface.md b/docs/Model/BoardInterface.md index 00dfd95..9381e85 100644 --- a/docs/Model/BoardInterface.md +++ b/docs/Model/BoardInterface.md @@ -151,6 +151,16 @@ setLabelNames(array $labelNames) getLabelNames() ``` +### Get labels +```php +getLabels() +``` + +### Get label that has name $name and color $color +```php +getLabel($name, $color) +``` + ### Set power ups ```php setPowerUps(array $powerUps) diff --git a/docs/Model/Card.md b/docs/Model/Card.md index e938a61..25fece4 100644 --- a/docs/Model/Card.md +++ b/docs/Model/Card.md @@ -281,24 +281,24 @@ setLabels(array $labels) getLabels() ``` -### Get the colors of labels associated to this card +### Get the ids of labels associated to this card ```php -getLabelColors() +getLabelIds() ``` -### Does the card have the label of which the color is $color? +### Does the card have the label? ```php -hasLabel($color) +hasLabel(LabelInterface $label) ``` -### Add the label of color $color +### Add the label $label ```php -addLabel($color) +addLabel(LabelInterface $label) ``` -### Remove the label of color $color +### Remove the label $label ```php -removeLabel($color) +removeLabel(LabelInterface $label) ``` ### Set Badges diff --git a/docs/Model/Label.md b/docs/Model/Label.md new file mode 100644 index 0000000..f48186a --- /dev/null +++ b/docs/Model/Label.md @@ -0,0 +1,47 @@ + +====================== + +### Get id +```php +getId() +``` + +### Set name +```php +setName($name) +``` + +### Get name +```php +getName() +``` + +### Set color +```php +setColor($color) +``` + +### Get color +```php +getColor() +``` + +### Set boardId +```php +setBoardId($boardId) +``` + +### Get boardId +```php +getBoardId() +``` + +### Set board +```php +setBoard(\Trello\Model\BoardInterface $board) +``` + +### Get board +```php +getBoard() +``` diff --git a/docs/Model/LabelInterface.md b/docs/Model/LabelInterface.md new file mode 100644 index 0000000..f48186a --- /dev/null +++ b/docs/Model/LabelInterface.md @@ -0,0 +1,47 @@ + +====================== + +### Get id +```php +getId() +``` + +### Set name +```php +setName($name) +``` + +### Get name +```php +getName() +``` + +### Set color +```php +setColor($color) +``` + +### Get color +```php +getColor() +``` + +### Set boardId +```php +setBoardId($boardId) +``` + +### Get boardId +```php +getBoardId() +``` + +### Set board +```php +setBoard(\Trello\Model\BoardInterface $board) +``` + +### Get board +```php +getBoard() +``` diff --git a/lib/Trello/Api/AbstractApi.php b/lib/Trello/Api/AbstractApi.php index 31e9f64..777ac50 100644 --- a/lib/Trello/Api/AbstractApi.php +++ b/lib/Trello/Api/AbstractApi.php @@ -211,13 +211,11 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr $parameters[$name] = $parameter ? 'true' : 'false'; } } - $response = $this->client->getHttpClient()->put( $path, $this->createParametersBody($parameters), $requestHeaders ); - return ResponseMediator::getContent($response); } @@ -258,9 +256,7 @@ protected function createParametersBody(array $parameters) if (is_bool($subParameter)) { $subParameter = $subParameter ? 'true' : 'false'; } - $parameters[$name.'/'.$subName] = $subParameter; } - unset($parameters[$name]); } elseif ($parameter instanceof DateTime) { $parameters[$name] = $parameter->format($parameter::ATOM); } diff --git a/lib/Trello/Api/Label.php b/lib/Trello/Api/Label.php new file mode 100644 index 0000000..c36567d --- /dev/null +++ b/lib/Trello/Api/Label.php @@ -0,0 +1,110 @@ +get($this->getPath().'/'.rawurlencode($id), $params); + } + + /** + * Create a label + * @link https://developers.trello.com/reference/#id-1 + * + * @param array $params optional attributes + * + * @return array label info + */ + public function create(array $params = array()) + { + $this->validateRequiredParameters(array('name', 'idBoard', 'color'), $params); + + return $this->post('boards/'.rawurlencode($params['idBoard']).'/labels/', $params); + } + + /** + * Update a label + * @link https://developers.trello.com/reference/#id-1 + * + * @param string $id the label's id + * @param array $params label attributes to update + * + * @return array label info + */ + public function update($id, array $params = array()) + { + return $this->put($this->getPath().'/'.rawurlencode($id).'/labels/', $params); + } + + /** + * Set a given label's board + * @link https://developers.trello.com/reference/#id-1 + * + * @param string $id the label's id + * @param string $boardId the board's id + * + * @return array board info + */ + public function setBoard($id, $boardId) + { + return $this->put($this->getPath().'/'.rawurlencode($id).'/idBoard', array('value' => $boardId)); + } + + /** + * Get a given label's board + * @link https://developers.trello.com/reference/#id + * + * @param string $id the label's id + * @param array $params optional parameters + * + * @return array board info + */ + public function getBoard($id, array $params = array()) + { + return $this->get($this->getPath().'/'.rawurlencode($id).'/board', $params); + } + + /** + * Get the field of a board of a given label + * @link https://developers.trello.com/reference/#id + * + * @param string $id the label's id + * @param array $field the name of the field + * + * @return array + * + * @throws InvalidArgumentException if the field does not exist + */ + public function getBoardField($id, $field) + { + $this->validateAllowedParameters(Board::$fields, $field, 'field'); + + return $this->get($this->getPath().'/'.rawurlencode($id).'/board/'.rawurlencode($field)); + } +} diff --git a/lib/Trello/Client.php b/lib/Trello/Client.php index d276941..cdc9c59 100644 --- a/lib/Trello/Client.php +++ b/lib/Trello/Client.php @@ -137,6 +137,10 @@ public function api($name) case 'webhooks': $api = new Api\Webhook($this); break; + case 'label': + case 'labels': + $api = new Api\Label($this); + break; default: throw new InvalidArgumentException(sprintf('Undefined api called: "%s"', $name)); } diff --git a/lib/Trello/Event/CardLabelEvent.php b/lib/Trello/Event/CardLabelEvent.php new file mode 100644 index 0000000..6866caf --- /dev/null +++ b/lib/Trello/Event/CardLabelEvent.php @@ -0,0 +1,33 @@ +label = $label; + } + + /** + * Get label + * + * @return LabelInterface + */ + public function getLabel() + { + return $this->label; + } +} diff --git a/lib/Trello/Events.php b/lib/Trello/Events.php index 17ceef7..67eebc0 100644 --- a/lib/Trello/Events.php +++ b/lib/Trello/Events.php @@ -202,13 +202,13 @@ final class Events /** * When a new label is added to a card - * The event listener method receives a Trello\Event\CardEvent instance. + * The event listener method receives a Trello\Event\CardLabelEvent instance. */ const CARD_ADD_LABEL = 'addLabelToCard'; /** * When a new label is removed from a card - * The event listener method receives a Trello\Event\CardEvent instance. + * The event listener method receives a Trello\Event\CardLabelEvent instance. */ const CARD_REMOVE_LABEL = 'removeLabelFromCard'; diff --git a/lib/Trello/Manager.php b/lib/Trello/Manager.php index 60e8363..8effb56 100644 --- a/lib/Trello/Manager.php +++ b/lib/Trello/Manager.php @@ -67,6 +67,18 @@ public function getCard($id = null) return new Model\Card($this->client, $id); } + /** + * Get label by id or create a new one + * + * @param string $id the label's id + * + * @return Model\LabelInterface + */ + public function getLabel($id = null) + { + return new Model\Label($this->client, $id); + } + /** * Get checklist by id or create a new one * diff --git a/lib/Trello/Model/Board.php b/lib/Trello/Model/Board.php index 733600b..2db5d3a 100644 --- a/lib/Trello/Model/Board.php +++ b/lib/Trello/Model/Board.php @@ -19,6 +19,7 @@ class Board extends AbstractObject implements BoardInterface 'membersInvited' => 'all', 'memberships' => 'all', 'lists' => 'all', + 'labels' => 'all', ); /** @@ -307,6 +308,36 @@ public function getLabelNames() return $this->data['labelNames']; } + /** + * {@inheritdoc} + */ + public function getLabels() + { + $labels = array(); + + foreach ($this->data['labels'] as $data) { + $labels[$data['id']] = new Label($this->client, $data['id']); + } + + return $labels; + } + + public function getLabel($name, $color) + { + foreach ($this->data['labels'] as $data) { + if ($data['name'] === $name && $data['color'] === $color) { + return new Label($this->client, $data['id']); + } + } + + throw new InvalidArgumentException(sprintf( + 'There is no list with name "%s" and color "%s" on this board ("%s")', + $name, + $color, + $this->getName() + )); + } + /** * {@inheritdoc} */ diff --git a/lib/Trello/Model/BoardInterface.php b/lib/Trello/Model/BoardInterface.php index 52b212e..248671a 100644 --- a/lib/Trello/Model/BoardInterface.php +++ b/lib/Trello/Model/BoardInterface.php @@ -237,9 +237,6 @@ public function getPreferences(); /** * Set label names * - * @param array $labelNames an array of 'color' => 'label name' - * existing colors are: 'green', 'yellow', 'orange', 'red', 'purple', 'blue' - * * @return BoardInterface */ public function setLabelNames(array $labelNames); @@ -251,6 +248,21 @@ public function setLabelNames(array $labelNames); */ public function getLabelNames(); + /** + * Get labels + * + * @return array + */ + public function getLabels(); + + /** + * Get label + * + * @return LabelInterface + */ + public function getLabel($name, $color); + + /** * Set power ups * diff --git a/lib/Trello/Model/Card.php b/lib/Trello/Model/Card.php index bead76d..3435a0a 100644 --- a/lib/Trello/Model/Card.php +++ b/lib/Trello/Model/Card.php @@ -23,6 +23,7 @@ class Card extends AbstractObject implements CardInterface 'attachments' => true, 'checklists' => 'all', 'checkItemStates' => true, + 'labels' => true, 'actions' => Events::CARD_COMMENT, ); @@ -681,7 +682,7 @@ public function getManualCoverAttachment() */ public function setLabels(array $labels) { - $this->data['labels'] = $labels; + $this->data['idLabels'] = $labels; return $this; } @@ -689,66 +690,46 @@ public function setLabels(array $labels) /** * {@inheritdoc} */ - public function getLabels() + public function getLabelIds() { - return $this->data['labels']; + return $this->data['idLabels']; } /** * {@inheritdoc} */ - public function getLabelColors() + public function getLabels() { - return array_map(function ($label) { - return $label['color']; - }, $this->data['labels']); + return $this->data['labels']; } /** * {@inheritdoc} */ - public function hasLabel($color) + public function hasLabel(LabelInterface $label) { - return in_array($color, $this->getLabelColors()); + return in_array($label->getId(), $this->data['idLabels']); } /** * {@inheritdoc} */ - public function addLabel($color) + public function addLabel(LabelInterface $label) { - if ($this->hasLabel($color)) { - throw new InvalidArgumentException(sprintf( - 'Card %s already has the %s label.', - $this->getName(), - $color - )); - } - - $this->data['labels'][] = array('color' => $color); - + $this->data['idLabels'][] = $label->getId(); return $this; } /** * {@inheritdoc} */ - public function removeLabel($color) + public function removeLabel(LabelInterface $label) { - if (!$this->hasLabel($color)) { - throw new InvalidArgumentException(sprintf( - "Can't remove the %s label because card %s doesn't have it.", - $color, - $this->getName() - )); - } - - foreach ($this->data['labels'] as $key => $label) { - if ($label['color'] === $color) { - unset($this->data['labels'][$key]); + foreach ($this->data['idLabels'] as $key => $id) { + if ($id === $label->getId()) { + unset($this->data['idLabels'][$key]); } } - return $this; } diff --git a/lib/Trello/Model/CardInterface.php b/lib/Trello/Model/CardInterface.php index 1805fa4..a589e81 100644 --- a/lib/Trello/Model/CardInterface.php +++ b/lib/Trello/Model/CardInterface.php @@ -472,7 +472,7 @@ public function getLabels(); * * @return array */ - public function getLabelColors(); + public function getLabelIds(); /** * Does the card have the label of which the color is $color? @@ -481,7 +481,7 @@ public function getLabelColors(); * * @return bool */ - public function hasLabel($color); + public function hasLabel(LabelInterface $label); /** * Add the label of color $color @@ -490,7 +490,7 @@ public function hasLabel($color); * * @return CardInterface */ - public function addLabel($color); + public function addLabel(LabelInterface $label); /** * Remove the label of color $color @@ -499,7 +499,7 @@ public function addLabel($color); * * @return CardInterface */ - public function removeLabel($color); + public function removeLabel(LabelInterface $label); /** * Set Badges diff --git a/lib/Trello/Model/Label.php b/lib/Trello/Model/Label.php new file mode 100644 index 0000000..99e4355 --- /dev/null +++ b/lib/Trello/Model/Label.php @@ -0,0 +1,94 @@ + 'all', + 'board' => true, + ); + + /** + * {@inheritdoc} + */ + public function getId() + { + return $this->data['id']; + } + + /** + * {@inheritdoc} + */ + public function setName($name) + { + $this->data['name'] = $name; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return $this->data['name']; + } + + /** + * {@inheritdoc} + */ + public function setColor($color) + { + $this->data['color'] = $color; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getColor() + { + return $this->data['color']; + } + + + /** + * {@inheritdoc} + */ + public function setBoardId($boardId) + { + $this->data['idBoard'] = $boardId; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getBoardId() + { + return $this->data['idBoard']; + } + + /** + * {@inheritdoc} + */ + public function setBoard(BoardInterface $board) + { + return $this->setBoardId($board->getId()); + } + + /** + * {@inheritdoc} + */ + public function getBoard() + { + return new Board($this->client, $this->getBoardId()); + } +} diff --git a/lib/Trello/Model/LabelInterface.php b/lib/Trello/Model/LabelInterface.php new file mode 100644 index 0000000..86417e6 --- /dev/null +++ b/lib/Trello/Model/LabelInterface.php @@ -0,0 +1,83 @@ +setCard($this->getCard($data['card']['id'])); + $event->setLabel($this->getLabel($data['label']['id'])); break; case Events::CARD_COPY: $event = new Event\CardCopyEvent(); @@ -235,7 +236,6 @@ public function handleWebhook(Request $request = null) serialize($data) )); } - $event->setRequestData($data); $this->dispatcher->dispatch($eventName, $event); diff --git a/test/Trello/Tests/Unit/HttpClient/HttpClientTest.php b/test/Trello/Tests/Unit/HttpClient/HttpClientTest.php index a65090c..5516117 100644 --- a/test/Trello/Tests/Unit/HttpClient/HttpClientTest.php +++ b/test/Trello/Tests/Unit/HttpClient/HttpClientTest.php @@ -226,7 +226,7 @@ public function getOption($name, $default = null) return isset($this->options[$name]) ? $this->options[$name] : $default; } - public function request($path, $body, $httpMethod = 'GET', array $headers = array(), array $options = array()) + public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array()) { $request = $this->client->createRequest($httpMethod, $path);