Skip to content

Commit 6670567

Browse files
Added setting BodyType and DCS (#39)
* Added support for changing Type property on MessageBody class * Added support for providing a MessageBody object to a Message * Added support for changing DCS property on Message class
1 parent 282804f commit 6670567

File tree

4 files changed

+178
-64
lines changed

4 files changed

+178
-64
lines changed

src/CMText/Message.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class Message implements JsonSerializable
7070
*/
7171
private $richContent;
7272

73+
/**
74+
* @var int Optional Data Coding Scheme property for SMS messages.
75+
*/
76+
private $dcs;
77+
7378
/**
7479
* Fallback value for Sender
7580
*/
@@ -90,21 +95,21 @@ class Message implements JsonSerializable
9095
/**
9196
* Message constructor.
9297
*
93-
* @param string $body
98+
* @param string|MessageBody $body
9499
* @param string|null $from
95100
* @param array $to
96101
* @param string|null $reference
97102
* @throws \CMText\Exceptions\RecipientLimitException
98103
*/
99-
public function __construct(string $body = '', string $from = null, array $to = [], string $reference = null)
104+
public function __construct($body = '', string $from = null, array $to = [], string $reference = null)
100105
{
101-
$this->body = new MessageBody($body);
102-
$this->from = $from ?? self::SENDER_FALLBACK;
103-
$this->reference = $reference;
104-
self::AddRecipients($to);
106+
$this->__set('body', $body);
107+
$this->__set('from', $from);
108+
$this->__set('reference', $reference);
109+
$this->__set('to', $to);
105110

106-
$this->minimumNumberOfMessageParts = self::MESSAGEPARTS_MINIMUM;
107-
$this->maximumNumberOfMessageParts = self::MESSAGEPARTS_MAXIMUM;
111+
$this->__set('minimumNumberOfMessageParts', self::MESSAGEPARTS_MINIMUM);
112+
$this->__set('maximumNumberOfMessageParts', self::MESSAGEPARTS_MAXIMUM);
108113

109114
$this->customgrouping3 = 'text-sdk-php-' . TextClient::VERSION;
110115
}
@@ -120,13 +125,21 @@ public function __set(string $name, $value)
120125
{
121126
switch ($name){
122127
case 'body':
123-
$this->body = new MessageBody($value);
128+
if($value instanceof MessageBody){
129+
$this->body = $value;
130+
}else{
131+
$this->body = new MessageBody($value);
132+
}
124133
break;
125134

126135
case 'from':
136+
$this->{$name} = $value ?? self::SENDER_FALLBACK;;
137+
break;
138+
127139
case 'minimumNumberOfMessageParts':
128140
case 'maximumNumberOfMessageParts':
129141
case 'reference':
142+
case 'dcs':
130143
$this->{$name} = $value;
131144
break;
132145

@@ -189,7 +202,7 @@ public function WithRichMessage(IRichMessage $richMessage){
189202

190203
/**
191204
* Add a Suggestion to a message. Supported Suggestion types depend on the Channel used.
192-
* @param $suggestions
205+
* @param array $suggestions
193206
* @return $this
194207
* @throws \CMText\Exceptions\SuggestionsLimitException
195208
*/
@@ -284,6 +297,10 @@ public function jsonSerialize()
284297
$return['reference'] = $this->reference;
285298
}
286299

300+
if( null !== $this->dcs ){
301+
$return['dcs'] = $this->dcs;
302+
}
303+
287304
return (object)$return;
288305
}
289306
}

src/CMText/MessageBody.php

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,74 @@ class MessageBody implements JsonSerializable
2121
/**
2222
* @var string
2323
*/
24-
private $type = MessageBodyTypes::AUTO;
24+
private $type;
2525

2626

2727
/**
2828
* MessageBody constructor.
2929
* @param string $content
30+
* @param string $type
3031
*/
31-
public function __construct(string $content)
32+
public function __construct(string $content, string $type = MessageBodyTypes::AUTO)
3233
{
33-
$this->content = $content;
34+
$this->__set('content', $content);
35+
$this->__set('type', $type);
3436
}
3537

3638

3739
/**
38-
* Getters for a limited set or properties, providing normalized results
40+
* Setters providing sanitized results
3941
*
4042
* @param $name
41-
* @return bool|string
43+
* @param $value
44+
* @return void
4245
*/
43-
public function __get($name)
46+
public function __set($name, $value)
4447
{
4548
switch ($name){
46-
/**
47-
* message content always as utf8 encoded string
48-
*/
49-
case 'content':
50-
return $this->content;
51-
break;
52-
53-
/**
54-
* message type as is with a fallback to the defined AUTO MessageBodyType
55-
*/
5649
case 'type':
57-
return $this->type ?: MessageBodyTypes::AUTO;
50+
if( in_array(
51+
$value,
52+
(new \ReflectionClass(MessageBodyTypes::class))->getConstants()
53+
) ){
54+
$this->type = $value;
55+
}else{
56+
$this->type = MessageBodyTypes::AUTO;
57+
}
5858
break;
5959

60-
default:
61-
return false;
60+
case 'content':
61+
// try to make sure the content as Json-compatible as possible
62+
if( function_exists('mb_convert_encoding') && function_exists('mb_detect_encoding') ){
63+
$value = mb_convert_encoding(
64+
$value,
65+
'UTF-8',
66+
mb_detect_encoding($value)
67+
);
68+
}
69+
70+
$this->content = $value;
71+
break;
6272
}
6373
}
6474

6575

76+
/**
77+
* @param string $type
78+
* @return $this
79+
*/
80+
public function WithType(string $type): MessageBody
81+
{
82+
$this->__set('type', $type);
83+
return $this;
84+
}
85+
86+
6687
/**
6788
* @return object
6889
*/
6990
public function jsonSerialize()
7091
{
71-
// try to make sure the content as Json-compatible as possible
72-
if( function_exists('mb_convert_encoding') && function_exists('mb_detect_encoding') ){
73-
$this->content = mb_convert_encoding(
74-
$this->content,
75-
'UTF-8',
76-
mb_detect_encoding($this->content)
77-
);
78-
}
79-
8092
return (object)[
8193
'content' => $this->content,
8294
'type' => $this->type,

tests/MessageBodyTest.php

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,93 @@
88
class MessageBodyTest extends TestCase
99
{
1010

11+
public function testJsonSerialization()
12+
{
13+
$messageBody = new MessageBody('content');
14+
15+
// Should be Json Serializable with or without multi-byte-string extension
16+
$json = json_encode($messageBody);
17+
$this->assertJson(
18+
$json
19+
);
20+
}
21+
1122
/**
1223
* Result of setting up a new MessageBody
1324
*/
14-
public function testReadingProperties()
25+
public function testSettingContent()
1526
{
1627
$content = 'test message body content';
1728

1829
$messageBody = new MessageBody($content);
1930

31+
$json = json_encode($messageBody);
32+
2033
// Content should be set correctly
2134
$this->assertEquals(
2235
$content,
23-
$messageBody->content
36+
(json_decode($json))->content
2437
);
38+
}
39+
2540

26-
// Type should be set as expected
41+
/**
42+
* Result of setting up a new MessageBodyType
43+
*/
44+
public function testSettingTypeText()
45+
{
46+
$messageBody = new MessageBody(__METHOD__, \CMText\MessageBodyTypes::TEXT);
47+
48+
$json = json_encode($messageBody);
49+
50+
// Type should be set correctly
2751
$this->assertEquals(
28-
\CMText\MessageBodyTypes::AUTO,
29-
$messageBody->type
52+
\CMText\MessageBodyTypes::TEXT,
53+
(json_decode($json))->type
3054
);
55+
}
56+
57+
58+
/**
59+
* Result of setting up a new MessageBodyType Incorrectly
60+
*/
61+
public function testSettingTypeIncorrectly()
62+
{
63+
$messageBody = new MessageBody('content', 'invalid-type');
64+
65+
$json = json_encode($messageBody);
3166

32-
// unknown properties should return false
33-
$this->assertFalse(
34-
$messageBody->unknownProperty
67+
// Type should be set to AUTO
68+
$this->assertEquals(
69+
\CMText\MessageBodyTypes::AUTO,
70+
(json_decode($json))->type
3571
);
3672

37-
// Should be Json Serializable with or without multi-byte-string extension
38-
$this->assertJson(
39-
json_encode($messageBody)
73+
74+
$messageBody->type = 'still-invalid';
75+
76+
$json = json_encode($messageBody);
77+
78+
// Type should be set to AUTO
79+
$this->assertEquals(
80+
\CMText\MessageBodyTypes::AUTO,
81+
(json_decode($json))->type
4082
);
4183
}
4284

43-
}
85+
86+
public function testWithTypeChaining()
87+
{
88+
$messageBody = (new MessageBody('content'))->WithType(\CMText\MessageBodyTypes::BINARY);
89+
90+
$this->assertInstanceOf(
91+
MessageBody::class,
92+
$messageBody
93+
);
94+
95+
$this->assertEquals(
96+
\CMText\MessageBodyTypes::BINARY,
97+
$messageBody->jsonSerialize()->type
98+
);
99+
}
100+
}

tests/MessageTest.php

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ public function testInitializingCorrectly()
1515
{
1616
$testSender = 'test-sender';
1717

18-
try {
19-
$new = new Message(
20-
'test text',
21-
$testSender,
22-
[
23-
'00334455667788',
24-
]
25-
);
26-
27-
}catch (\Exception $e){
28-
$new = null;
29-
}
18+
$new = new Message(
19+
'test text',
20+
$testSender,
21+
[
22+
'00334455667788',
23+
]
24+
);
3025

3126
$this->assertInstanceOf(
3227
Message::class,
@@ -55,6 +50,7 @@ public function testSettingProperties()
5550
'99999999999999',
5651
];
5752
$reference = 'test-reference';
53+
$dcs = 8;
5854

5955
// initialize an empty Message
6056
$new = new Message();
@@ -64,14 +60,15 @@ public function testSettingProperties()
6460
$new->from = $from;
6561
$new->to = $to;
6662
$new->reference = $reference;
63+
$new->dcs = $dcs;
6764

6865
// get an instance that we can check
6966
$json = $new->jsonSerialize();
7067

7168
// verify the Body content is correctly set
7269
$this->assertEquals(
7370
$body,
74-
$json->body->content
71+
$json->body->jsonSerialize()->content
7572
);
7673

7774
// verify the Sender is correctly set
@@ -86,7 +83,13 @@ public function testSettingProperties()
8683
$json->reference
8784
);
8885

89-
// verify the correct amount of Recipients are added
86+
// verify the dcs is correctly set
87+
$this->assertEquals(
88+
$dcs,
89+
$json->dcs
90+
);
91+
92+
// verify the correct amount of Recipients is added
9093
$this->assertCount(
9194
count($to),
9295
$json->to
@@ -238,4 +241,29 @@ public function testWithPayment()
238241
$json->richContent->conversation[0]->payment
239242
);
240243
}
244+
245+
246+
public function testSettingCustomBodyType()
247+
{
248+
$customMessageBody = new \CMText\MessageBody(
249+
'content',
250+
\CMText\MessageBodyTypes::TEXT
251+
);
252+
253+
$message = new Message($customMessageBody);
254+
255+
$json = json_decode( json_encode($message) );
256+
257+
$this->assertEquals(
258+
\CMText\MessageBodyTypes::TEXT,
259+
$json->body->type
260+
);
261+
}
262+
263+
264+
public function testSettingBodyIncorrectly()
265+
{
266+
$this->expectException(TypeError::class);
267+
new Message(new Channels());
268+
}
241269
}

0 commit comments

Comments
 (0)