Skip to content

Commit bc764a8

Browse files
committed
Refactored BaseMessage::toArray and added BaseMessage::getData in Push API
1 parent 13192fe commit bc764a8

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

src/Mremi/Flowdock/Api/Push/BaseMessage.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,42 @@ public function hasErrors()
9595
return count($this->errors) > 0;
9696
}
9797

98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function getData()
102+
{
103+
$array = $this->toArray();
104+
105+
// to be consistent with the Flowdock API
106+
unset($array['errors']);
107+
108+
return $array;
109+
}
110+
98111
/**
99112
* {@inheritdoc}
100113
*/
101114
public function toArray()
102115
{
103-
return array(
104-
'content' => $this->content,
105-
'tags' => $this->tags,
106-
);
116+
$array = get_object_vars($this);
117+
118+
$keys = array_map(array($this, 'underscore'), array_keys($array));
119+
120+
return array_combine($keys, array_values($array));
121+
}
122+
123+
/**
124+
* A string to underscore.
125+
*
126+
* This has been copied from the DependencyInjection Symfony component.
127+
*
128+
* @param string $string The string to underscore
129+
*
130+
* @return string The underscored string
131+
*/
132+
private function underscore($string)
133+
{
134+
return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($string, '_', '.')));
107135
}
108136
}

src/Mremi/Flowdock/Api/Push/BaseMessageInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ public function getErrors();
7373
*/
7474
public function hasErrors();
7575

76+
/**
77+
* Returns an array representation of the message data
78+
*
79+
* @return array
80+
*/
81+
public function getData();
82+
7683
/**
7784
* Returns an array representation of the message
7885
*

src/Mremi/Flowdock/Api/Push/Chat/Chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function sendMessage(MessageInterface $message, array $options = array())
3232
$request = $client->post(null, array(
3333
'Content-Type' => 'application/json'
3434
), json_encode(
35-
$message->toArray()
35+
$message->getData()
3636
), $options);
3737

3838
$response = $request->send();

src/Mremi/Flowdock/Api/Push/Chat/Message.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class Message extends BaseMessage implements MessageInterface
2323
/**
2424
* @var string
2525
*/
26-
private $externalUserName;
26+
protected $externalUserName;
2727

2828
/**
2929
* @var integer
3030
*/
31-
private $messageId;
31+
protected $messageId;
3232

3333
/**
3434
* {@inheritdoc}
@@ -65,15 +65,4 @@ public function getMessageId()
6565
{
6666
return $this->messageId;
6767
}
68-
69-
/**
70-
* {@inheritdoc}
71-
*/
72-
public function toArray()
73-
{
74-
return array_merge(parent::toArray(), array(
75-
'external_user_name' => $this->externalUserName,
76-
'message_id' => $this->messageId,
77-
));
78-
}
7968
}

tests/Mremi/Flowdock/Tests/Api/Push/Chat/ChatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testSendMessage()
3838
$client
3939
->expects($this->exactly(2))
4040
->method('post')
41-
->with($this->equalTo(null), $this->equalTo(array('Content-Type' => 'application/json')), $this->equalTo(json_encode($message->toArray())), $this->equalTo(array()))
41+
->with($this->equalTo(null), $this->equalTo(array('Content-Type' => 'application/json')), $this->equalTo(json_encode($message->getData())), $this->equalTo(array()))
4242
->will($this->returnValue($request));
4343

4444
$chat = $this->getMockBuilder('Mremi\Flowdock\Api\Push\Chat\Chat')

tests/Mremi/Flowdock/Tests/Api/Push/Chat/MessageTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ public function testAddErrors()
6868
$this->assertTrue($this->message->hasErrors());
6969
}
7070

71+
/**
72+
* Tests the getData method
73+
*/
74+
public function testGetData()
75+
{
76+
$this->message
77+
->setContent('Hello world!')
78+
->setExternalUserName('mremi')
79+
->addTag('tag1')
80+
->addTag('tag2')
81+
->setMessageId(1)
82+
->addError('Error #1')
83+
->addError('Error #2');
84+
85+
$expected = array(
86+
'content' => $this->message->getContent(),
87+
'external_user_name' => $this->message->getExternalUserName(),
88+
'tags' => $this->message->getTags(),
89+
'message_id' => $this->message->getMessageId(),
90+
);
91+
92+
$this->assertEquals($expected, $this->message->getData());
93+
}
94+
7195
/**
7296
* Tests the toArray method
7397
*/
@@ -78,13 +102,16 @@ public function testToArray()
78102
->setExternalUserName('mremi')
79103
->addTag('tag1')
80104
->addTag('tag2')
81-
->setMessageId(1);
105+
->setMessageId(1)
106+
->addError('Error #1')
107+
->addError('Error #2');
82108

83109
$expected = array(
84110
'content' => $this->message->getContent(),
85111
'external_user_name' => $this->message->getExternalUserName(),
86112
'tags' => $this->message->getTags(),
87113
'message_id' => $this->message->getMessageId(),
114+
'errors' => $this->message->getErrors(),
88115
);
89116

90117
$this->assertEquals($expected, $this->message->toArray());

0 commit comments

Comments
 (0)