Skip to content

Deprecated loadFromArray for loadFromStdClass and fixed parsing of contacts in Groups::getContacts #182

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

Merged
merged 9 commits into from
Mar 3, 2022
Merged
20 changes: 19 additions & 1 deletion src/MessageBird/Objects/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MessageBird\Objects;

use stdClass;

/**
* Class Base
*
Expand All @@ -10,9 +12,11 @@
class Base
{
/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
*
* @param mixed $object
*
* @return $this
* @return self
*/
public function loadFromArray($object)
{
Expand All @@ -25,4 +29,18 @@ public function loadFromArray($object)
}
return $this;
}

/**
* @param stdClass $object
* @return self
*/
public function loadFromStdclass(stdClass $object)
{
foreach ($object as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
return $this;
}
}
46 changes: 37 additions & 9 deletions src/MessageBird/Objects/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MessageBird\Objects;

use stdClass;

/**
* Class Contact
*
Expand Down Expand Up @@ -65,16 +67,16 @@ class Contact extends Base
/**
* The hash of the group this contact belongs to.
*
* @var array
* @var ?stdClass
*/
protected $groups = [];
protected $groups = null;

/**
* The hash with messages sent to contact.
*
* @var array
* @var ?stdClass
*/
protected $messages = [];
protected $messages = null;

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

public function getGroups(): array
public function getGroups(): stdClass
{
return $this->groups;
}

public function getMessages(): array
public function getMessages(): stdClass
{
return $this->messages;
}
Expand All @@ -115,7 +117,7 @@ public function getCreatedDatetime(): string
return $this->createdDatetime;
}

public function getUpdatedDatetime(): string
public function getUpdatedDatetime(): ?string
{
return $this->updatedDatetime;
}
Expand All @@ -126,16 +128,27 @@ public function getCustomDetails(): array
}

/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
*
* @param mixed $object
*/
public function loadFromArray($object): Contact
public function loadFromArray($object): self
{
unset($this->custom1, $this->custom2, $this->custom3, $this->custom4);

return parent::loadFromArray($object);
}

public function loadFromStdclass(stdClass $object): self
{
unset($this->custom1, $this->custom2, $this->custom3, $this->custom4);

return parent::loadFromStdclass($object);
}

/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclassForGroups()}
*
* @param mixed $object
*
* @return $this ->object
Expand All @@ -154,4 +167,19 @@ public function loadFromArrayForGroups($object)
}
return $object;
}

public function loadFromStdclassForGroups(stdClass $object)
{
parent::loadFromStdclass($object);

if (!empty($object->items)) {
foreach ($object->items as &$item) {
$group = new Group();
$group->loadFromStdclass($item);

$item = $group;
}
}
return $object;
}
}
14 changes: 14 additions & 0 deletions src/MessageBird/Objects/Conversation/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MessageBird\Objects\Conversation;

use MessageBird\Objects\Base;
use stdClass;

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

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

return $this;
}

public function loadFromStdclass(stdClass $object): self
{
parent::loadFromStdclass($object);

if (!empty($this->customDetails)) {
$this->customDetails = (array)$this->customDetails;
}

return $this;
}
}
16 changes: 15 additions & 1 deletion src/MessageBird/Objects/Conversation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use JsonSerializable;
use MessageBird\Objects\Base;
use MessageBird\Objects\Conversation\HSM\Message as HSMMessage;
use stdClass;

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

/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
*
* @param mixed $object
*
* @return $this
*/
public function loadFromArray($object)
public function loadFromArray($object): self
{
// Text is already properly set if available due to the response's structure.
parent::loadFromArray($object);
Expand All @@ -70,6 +73,17 @@ public function loadFromArray($object)
return $this;
}

public function loadFromStdclass(stdClass $object): self
{
// Text is already properly set if available due to the response's structure.
parent::loadFromStdclass($object);

$this->loadLocationIfNeeded();
$this->loadMediaIfNeeded();

return $this;
}

/**
* Sets the location on this object if available.
*/
Expand Down
37 changes: 37 additions & 0 deletions src/MessageBird/Objects/Conversation/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MessageBird\Objects\Conversation;

use MessageBird\Objects\Base;
use stdClass;

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

/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
*
* @param mixed $object
*/
public function loadFromArray($object): Conversation
Expand Down Expand Up @@ -131,4 +134,38 @@ public function loadFromArray($object): Conversation

return $this;
}

public function loadFromStdclass(stdClass $object): self
{
parent::loadFromStdclass($object);

if (!empty($object->contact)) {
$newContact = new Contact();
$newContact->loadFromStdclass($object->contact);

$this->contact = $newContact;
}

if (!empty($object->channels)) {
$channels = [];

foreach ($object->channels as $channel) {
$newChannel = new Channel();
$newChannel->loadFromStdclass($channel);

$channels[] = $newChannel;
}

$this->channels = $channels;
}

if (!empty($object->messages)) {
$messages = new MessageReference();
$messages->loadFromStdclass($object->messages);

$this->messages = $messages;
}

return $this;
}
}
16 changes: 16 additions & 0 deletions src/MessageBird/Objects/Conversation/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use JsonSerializable;
use MessageBird\Objects\Base;
use stdClass;

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

/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
*
* @param mixed $object
*/
public function loadFromArray($object): Message
Expand All @@ -109,6 +112,19 @@ public function loadFromArray($object): Message
return $this;
}

public function loadFromStdclass(stdClass $object): self
{
parent::loadFromStdclass($object);

if (property_exists($object, 'content')) {
$content = new Content();
$content->loadFromStdclass($object->content);
$this->content = $content;
}

return $this;
}

/**
* Serialize only non empty fields.
*/
Expand Down
36 changes: 21 additions & 15 deletions src/MessageBird/Objects/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MessageBird\Objects;

use stdClass;

/**
* Class Group
*
Expand All @@ -15,25 +17,28 @@ class Group extends Base
* @var int
*/
public $name;

/**
* An unique random ID which is created on the MessageBird
* platform and is returned upon creation of the object.
*
* @var string
*/
protected $id;

/**
* The URL of the created object.
*
* @var string
*/
protected $href;

/**
* The hash with the contacts in group.
*
* @var array
* @var ?stdClass
*/
protected $contacts = [];
protected $contacts = null;

/**
* The date and time of the creation of the group in RFC3339 format (Y-m-d\TH:i:sP)
Expand Down Expand Up @@ -76,29 +81,30 @@ public function getCreatedDatetime(): string
/**
* Get the $updatedDatetime value
*/
public function getUpdatedDatetime(): string
public function getUpdatedDatetime(): ?string
{
return $this->createdDatetime;
}

public function getContacts(): stdClass
{
return $this->contacts;
}

/**
* @deprecated 2.2.0 No longer used by internal code, please switch to {@see self::loadFromStdclass()}
*
* @param mixed $object
*
* @return $this|void
*/
public function loadFromArray($object)
public function loadFromArray($object): self
{
parent::loadFromArray($object);

if (!empty($object->items)) {
foreach ($object->items as &$item) {
$contact = new Contact();
$contact->loadFromArray($item);

$item = $contact;
}
}
return parent::loadFromArray($object);
}

return $object;
public function loadFromStdclass(stdClass $object): self
{
return parent::loadFromStdclass($object);
}
}
Loading