context = $context; if ($data) { $this->bind($data); } } /** * Binds the message properties with the provided data. * * @param array|object $data The message data. * * @return self This object to support chaining. */ public function bind($data) { if (!is_array($data) && !is_object($data)) { throw new \InvalidArgumentException('Cannot bind chat message! Array or object expected, ' . gettype($data) . ' given.', 400); } foreach ($data as $k => $v) { // never update the context of the message if (!strcasecmp($k, 'context')) { continue; } if (!strcasecmp($k, 'attachments')) { // set attachments accordingly $this->setAttachments((array) $v); } else if (!strcasecmp($k, 'createdon')) { // set creation date accordingly $this->setCreationDate($v); } else if (property_exists($this, $k)) { // inject value only in case the property actually exists $this->{$k} = $v; } } return $this; } /** * Returns the context of the message. * * @return VBOChatContext */ public function getContext() { return $this->context; } /** * Returns the ID the message. * * @return int */ public function getID() { return (int) $this->id; } /** * Returns the name of the user that wrote the message. * * @return string */ public function getSenderName() { return (string) $this->sender_name; } /** * Returns the ID of the sender. * When the value is equal to 0, the message is sent by an administrator. * * @return int */ public function getSenderID() { return (int) $this->id_sender; } /** * Sets the sender information. * * @param string $name The name of the sender. * @param int $id The ID of the sender (0 for admin). * * @return self */ public function setSender(string $name, int $id = 0) { $this->sender_name = $name; $this->id_sender = $id; return $this; } /** * Returns the text of the message. * * @return string */ public function getMessage() { return (string) $this->message; } /** * Sets the text of the message. * * @param string $message * * @return self */ public function setMessage(string $message) { $this->message = $message; return $this; } /** * Returns an array of files. * * @return array */ public function getAttachments() { return $this->attachments; } /** * Sets the specified files as attachments. * * @param VBOChatAttachment[] $attachments * * @return self */ public function setAttachments(array $attachments) { $this->attachments = []; foreach ($attachments as $attachment) { if ($attachment instanceof VBOChatAttachment) { $this->addAttachment($attachment); } } return $this; } /** * Adds a new file as attachment. * * @param VBOChatAttachment $attachment * * @return self * * @throws InvalidArgumentException */ public function addAttachment(VBOChatAttachment $attachment) { // make sure the file exists if (!$attachment->exists()) { throw new \InvalidArgumentException('The attached file does not exist: ' . $attachment->getName(), 404); } $this->attachments[] = $attachment; return $this; } /** * Returns the creation date of the message (UTC). * * @return string */ public function getCreationDate() { if (!$this->createdon) { $this->createdon = JFactory::getDate()->toSql(); } return $this->createdon; } /** * Sets the creation date of the message (UTC). * * @param mixed $date Either a date string or a DateTime object. * * @return self */ public function setCreationDate($date) { try { if (!$date instanceof DateTime) { // make sure the provided date is correct $date = JFactory::getDate($date ?: 'now'); } // convert date object into a string $date = $date->toSql(); // make sure the specified date is not in the future if ($date > JFactory::getDate()->toSql()) { throw new UnexpectedValueException('Cannot accept a date in the future.', 400); } } catch (Exception $error) { // malformed date $date = null; } $this->createdon = $date; return $this; } /** * Returns the user ID of the message author. * * @return int */ public function getAuthor() { if (!$this->createdby) { $this->createdby = \JFactory::getUser()->id; } return $this->createdby; } /** * Returns the ID of the external resource this message is linked to. * * @return string|null * * @since 1.8.8 */ public function getReferenceID() { return $this->ref_id; } /** * Sets the ID of the external resource this message is linked to. * * @param string|null $refId * * @return self * * @since 1.8.8 */ public function setReferenceID(?string $refId) { $this->ref_id = $refId; return $this; } /** * Checks whether the message has been read by the current user. * * @return bool */ public function isRead() { return (bool) $this->read; } /** * @inheritDoc * * @see JsonSerializable */ #[ReturnTypeWillChange] public function jsonSerialize() { return [ 'id' => $this->getID(), 'context' => $this->getContext()->getAlias(), 'id_context' => $this->getContext()->getID(), 'sender_name' => $this->getSenderName(), 'id_sender' => $this->getSenderID(), 'message' => $this->getMessage(), 'attachments' => $this->getAttachments(), 'createdon' => $this->getCreationDate(), 'createdby' => $this->getAuthor(), 'read' => $this->isRead(), 'ref_id' => $this->ref_id, ]; } }