Skip to content

Commit e6d28a8

Browse files
MelvinLoosMelvin Loos
and
Melvin Loos
authored
Deprecated loadFromArray for loadFromStdClass and fixed parsing of contacts in Groups::getContacts (#182)
* fix: fixed loadFromArray method * fix: added missing getContacts method * added return type * fix: updated date fields not allowed to be null * fix: getContacts parses response into Group objs * fix: wrong return type and property type * fix: corrected typehints and initial data * fix: fixed ContactTest::testContactGetGroups * feat: deprecated loadFromArray Co-authored-by: Melvin Loos <[email protected]>
1 parent 7150ec1 commit e6d28a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+361
-99
lines changed

src/MessageBird/Objects/Base.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MessageBird\Objects;
44

5+
use stdClass;
6+
57
/**
68
* Class Base
79
*
@@ -10,9 +12,11 @@
1012
class Base
1113
{
1214
/**
15+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
16+
*
1317
* @param mixed $object
1418
*
15-
* @return $this
19+
* @return self
1620
*/
1721
public function loadFromArray($object)
1822
{
@@ -25,4 +29,18 @@ public function loadFromArray($object)
2529
}
2630
return $this;
2731
}
32+
33+
/**
34+
* @param stdClass $object
35+
* @return self
36+
*/
37+
public function loadFromStdclass(stdClass $object)
38+
{
39+
foreach ($object as $key => $value) {
40+
if (property_exists($this, $key)) {
41+
$this->$key = $value;
42+
}
43+
}
44+
return $this;
45+
}
2846
}

src/MessageBird/Objects/Contact.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MessageBird\Objects;
44

5+
use stdClass;
6+
57
/**
68
* Class Contact
79
*
@@ -65,16 +67,16 @@ class Contact extends Base
6567
/**
6668
* The hash of the group this contact belongs to.
6769
*
68-
* @var array
70+
* @var ?stdClass
6971
*/
70-
protected $groups = [];
72+
protected $groups = null;
7173

7274
/**
7375
* The hash with messages sent to contact.
7476
*
75-
* @var array
77+
* @var ?stdClass
7678
*/
77-
protected $messages = [];
79+
protected $messages = null;
7880

7981
/**
8082
* The date and time of the creation of the contact in RFC3339 format (Y-m-d\TH:i:sP)
@@ -100,12 +102,12 @@ public function getHref(): string
100102
return $this->href;
101103
}
102104

103-
public function getGroups(): array
105+
public function getGroups(): stdClass
104106
{
105107
return $this->groups;
106108
}
107109

108-
public function getMessages(): array
110+
public function getMessages(): stdClass
109111
{
110112
return $this->messages;
111113
}
@@ -115,7 +117,7 @@ public function getCreatedDatetime(): string
115117
return $this->createdDatetime;
116118
}
117119

118-
public function getUpdatedDatetime(): string
120+
public function getUpdatedDatetime(): ?string
119121
{
120122
return $this->updatedDatetime;
121123
}
@@ -126,16 +128,27 @@ public function getCustomDetails(): array
126128
}
127129

128130
/**
131+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
132+
*
129133
* @param mixed $object
130134
*/
131-
public function loadFromArray($object): Contact
135+
public function loadFromArray($object): self
132136
{
133137
unset($this->custom1, $this->custom2, $this->custom3, $this->custom4);
134-
138+
135139
return parent::loadFromArray($object);
136140
}
137141

142+
public function loadFromStdclass(stdClass $object): self
143+
{
144+
unset($this->custom1, $this->custom2, $this->custom3, $this->custom4);
145+
146+
return parent::loadFromStdclass($object);
147+
}
148+
138149
/**
150+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclassForGroups()}
151+
*
139152
* @param mixed $object
140153
*
141154
* @return $this ->object
@@ -154,4 +167,19 @@ public function loadFromArrayForGroups($object)
154167
}
155168
return $object;
156169
}
170+
171+
public function loadFromStdclassForGroups(stdClass $object)
172+
{
173+
parent::loadFromStdclass($object);
174+
175+
if (!empty($object->items)) {
176+
foreach ($object->items as &$item) {
177+
$group = new Group();
178+
$group->loadFromStdclass($item);
179+
180+
$item = $group;
181+
}
182+
}
183+
return $object;
184+
}
157185
}

src/MessageBird/Objects/Conversation/Contact.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MessageBird\Objects\Conversation;
44

55
use MessageBird\Objects\Base;
6+
use stdClass;
67

78
/**
89
* Represents a counterparty with who messages can be exchanged.
@@ -65,6 +66,8 @@ class Contact extends Base
6566
public $updatedDatetime;
6667

6768
/**
69+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
70+
*
6871
* @param mixed $object
6972
*/
7073
public function loadFromArray($object): Contact
@@ -77,4 +80,15 @@ public function loadFromArray($object): Contact
7780

7881
return $this;
7982
}
83+
84+
public function loadFromStdclass(stdClass $object): self
85+
{
86+
parent::loadFromStdclass($object);
87+
88+
if (!empty($this->customDetails)) {
89+
$this->customDetails = (array)$this->customDetails;
90+
}
91+
92+
return $this;
93+
}
8094
}

src/MessageBird/Objects/Conversation/Content.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use JsonSerializable;
66
use MessageBird\Objects\Base;
77
use MessageBird\Objects\Conversation\HSM\Message as HSMMessage;
8+
use stdClass;
89

910
/**
1011
* Represents a Message object's actual content. Formatted depending on type.
@@ -55,11 +56,13 @@ class Content extends Base implements JsonSerializable
5556
public $hsm;
5657

5758
/**
59+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
60+
*
5861
* @param mixed $object
5962
*
6063
* @return $this
6164
*/
62-
public function loadFromArray($object)
65+
public function loadFromArray($object): self
6366
{
6467
// Text is already properly set if available due to the response's structure.
6568
parent::loadFromArray($object);
@@ -70,6 +73,17 @@ public function loadFromArray($object)
7073
return $this;
7174
}
7275

76+
public function loadFromStdclass(stdClass $object): self
77+
{
78+
// Text is already properly set if available due to the response's structure.
79+
parent::loadFromStdclass($object);
80+
81+
$this->loadLocationIfNeeded();
82+
$this->loadMediaIfNeeded();
83+
84+
return $this;
85+
}
86+
7387
/**
7488
* Sets the location on this object if available.
7589
*/

src/MessageBird/Objects/Conversation/Conversation.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MessageBird\Objects\Conversation;
44

55
use MessageBird\Objects\Base;
6+
use stdClass;
67

78
/**
89
* A conversation is the view of all messages between you and a customer across
@@ -96,6 +97,8 @@ class Conversation extends Base
9697
public $updatedDatetime;
9798

9899
/**
100+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
101+
*
99102
* @param mixed $object
100103
*/
101104
public function loadFromArray($object): Conversation
@@ -131,4 +134,38 @@ public function loadFromArray($object): Conversation
131134

132135
return $this;
133136
}
137+
138+
public function loadFromStdclass(stdClass $object): self
139+
{
140+
parent::loadFromStdclass($object);
141+
142+
if (!empty($object->contact)) {
143+
$newContact = new Contact();
144+
$newContact->loadFromStdclass($object->contact);
145+
146+
$this->contact = $newContact;
147+
}
148+
149+
if (!empty($object->channels)) {
150+
$channels = [];
151+
152+
foreach ($object->channels as $channel) {
153+
$newChannel = new Channel();
154+
$newChannel->loadFromStdclass($channel);
155+
156+
$channels[] = $newChannel;
157+
}
158+
159+
$this->channels = $channels;
160+
}
161+
162+
if (!empty($object->messages)) {
163+
$messages = new MessageReference();
164+
$messages->loadFromStdclass($object->messages);
165+
166+
$this->messages = $messages;
167+
}
168+
169+
return $this;
170+
}
134171
}

src/MessageBird/Objects/Conversation/Message.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use JsonSerializable;
66
use MessageBird\Objects\Base;
7+
use stdClass;
78

89
/**
910
* Messages that have been sent by, or received from, a customer are
@@ -95,6 +96,8 @@ class Message extends Base implements JsonSerializable
9596
public $updatedDatetime;
9697

9798
/**
99+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
100+
*
98101
* @param mixed $object
99102
*/
100103
public function loadFromArray($object): Message
@@ -109,6 +112,19 @@ public function loadFromArray($object): Message
109112
return $this;
110113
}
111114

115+
public function loadFromStdclass(stdClass $object): self
116+
{
117+
parent::loadFromStdclass($object);
118+
119+
if (property_exists($object, 'content')) {
120+
$content = new Content();
121+
$content->loadFromStdclass($object->content);
122+
$this->content = $content;
123+
}
124+
125+
return $this;
126+
}
127+
112128
/**
113129
* Serialize only non empty fields.
114130
*/

src/MessageBird/Objects/Group.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MessageBird\Objects;
44

5+
use stdClass;
6+
57
/**
68
* Class Group
79
*
@@ -15,25 +17,28 @@ class Group extends Base
1517
* @var int
1618
*/
1719
public $name;
20+
1821
/**
1922
* An unique random ID which is created on the MessageBird
2023
* platform and is returned upon creation of the object.
2124
*
2225
* @var string
2326
*/
2427
protected $id;
28+
2529
/**
2630
* The URL of the created object.
2731
*
2832
* @var string
2933
*/
3034
protected $href;
35+
3136
/**
3237
* The hash with the contacts in group.
3338
*
34-
* @var array
39+
* @var ?stdClass
3540
*/
36-
protected $contacts = [];
41+
protected $contacts = null;
3742

3843
/**
3944
* The date and time of the creation of the group in RFC3339 format (Y-m-d\TH:i:sP)
@@ -76,29 +81,30 @@ public function getCreatedDatetime(): string
7681
/**
7782
* Get the $updatedDatetime value
7883
*/
79-
public function getUpdatedDatetime(): string
84+
public function getUpdatedDatetime(): ?string
8085
{
8186
return $this->createdDatetime;
8287
}
8388

89+
public function getContacts(): stdClass
90+
{
91+
return $this->contacts;
92+
}
93+
8494
/**
95+
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
96+
*
8597
* @param mixed $object
8698
*
8799
* @return $this|void
88100
*/
89-
public function loadFromArray($object)
101+
public function loadFromArray($object): self
90102
{
91-
parent::loadFromArray($object);
92-
93-
if (!empty($object->items)) {
94-
foreach ($object->items as &$item) {
95-
$contact = new Contact();
96-
$contact->loadFromArray($item);
97-
98-
$item = $contact;
99-
}
100-
}
103+
return parent::loadFromArray($object);
104+
}
101105

102-
return $object;
106+
public function loadFromStdclass(stdClass $object): self
107+
{
108+
return parent::loadFromStdclass($object);
103109
}
104110
}

0 commit comments

Comments
 (0)