123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- <?php
- namespace OC\Comments;
- use OCP\Comments\IComment;
- use OCP\Comments\IllegalIDChangeException;
- use OCP\Comments\MessageTooLongException;
- class Comment implements IComment {
- protected array $data = [
- 'id' => '',
- 'parentId' => '0',
- 'topmostParentId' => '0',
- 'childrenCount' => '0',
- 'message' => '',
- 'verb' => '',
- 'actorType' => '',
- 'actorId' => '',
- 'objectType' => '',
- 'objectId' => '',
- 'referenceId' => null,
- 'creationDT' => null,
- 'latestChildDT' => null,
- 'reactions' => null,
- 'expire_date' => null,
- ];
-
- public function __construct(array $data = null) {
- if (is_array($data)) {
- $this->fromArray($data);
- }
- }
-
- public function getId(): string {
- return $this->data['id'];
- }
-
- public function setId($id): IComment {
- if (!is_string($id)) {
- throw new \InvalidArgumentException('String expected.');
- }
- $id = trim($id);
- if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) {
- $this->data['id'] = $id;
- return $this;
- }
- throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.');
- }
-
- public function getParentId(): string {
- return $this->data['parentId'];
- }
-
- public function setParentId($parentId): IComment {
- if (!is_string($parentId)) {
- throw new \InvalidArgumentException('String expected.');
- }
- $this->data['parentId'] = trim($parentId);
- return $this;
- }
-
- public function getTopmostParentId(): string {
- return $this->data['topmostParentId'];
- }
-
- public function setTopmostParentId($id): IComment {
- if (!is_string($id)) {
- throw new \InvalidArgumentException('String expected.');
- }
- $this->data['topmostParentId'] = trim($id);
- return $this;
- }
-
- public function getChildrenCount(): int {
- return $this->data['childrenCount'];
- }
-
- public function setChildrenCount($count): IComment {
- if (!is_int($count)) {
- throw new \InvalidArgumentException('Integer expected.');
- }
- $this->data['childrenCount'] = $count;
- return $this;
- }
-
- public function getMessage(): string {
- return $this->data['message'];
- }
-
- public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH): IComment {
- if (!is_string($message)) {
- throw new \InvalidArgumentException('String expected.');
- }
- $message = trim($message);
- if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) {
- throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters');
- }
- $this->data['message'] = $message;
- return $this;
- }
-
- public function getMentions(): array {
- $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"group\/[a-z0-9_\-@\.\' ]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
- if (!$ok || !isset($mentions[0])) {
- return [];
- }
- $mentionIds = array_unique($mentions[0]);
- usort($mentionIds, static function ($mentionId1, $mentionId2) {
- return mb_strlen($mentionId2) <=> mb_strlen($mentionId1);
- });
- $result = [];
- foreach ($mentionIds as $mentionId) {
- $cleanId = trim(substr($mentionId, 1), '"');
- if (str_starts_with($cleanId, 'guest/')) {
- $result[] = ['type' => 'guest', 'id' => $cleanId];
- } elseif (str_starts_with($cleanId, 'group/')) {
- $result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
- } else {
- $result[] = ['type' => 'user', 'id' => $cleanId];
- }
- }
- return $result;
- }
-
- public function getVerb(): string {
- return $this->data['verb'];
- }
-
- public function setVerb($verb): IComment {
- if (!is_string($verb) || !trim($verb)) {
- throw new \InvalidArgumentException('Non-empty String expected.');
- }
- $this->data['verb'] = trim($verb);
- return $this;
- }
-
- public function getActorType(): string {
- return $this->data['actorType'];
- }
-
- public function getActorId(): string {
- return $this->data['actorId'];
- }
-
- public function setActor($actorType, $actorId): IComment {
- if (
- !is_string($actorType) || !trim($actorType)
- || !is_string($actorId) || $actorId === ''
- ) {
- throw new \InvalidArgumentException('String expected.');
- }
- $this->data['actorType'] = trim($actorType);
- $this->data['actorId'] = $actorId;
- return $this;
- }
-
- public function getCreationDateTime(): \DateTime {
- if (!isset($this->data['creationDT'])) {
- throw new \LogicException('Cannot get creation date before setting one or writting to database');
- }
- return $this->data['creationDT'];
- }
-
- public function setCreationDateTime(\DateTime $dateTime): IComment {
- $this->data['creationDT'] = $dateTime;
- return $this;
- }
-
- public function getLatestChildDateTime(): ?\DateTime {
- return $this->data['latestChildDT'];
- }
-
- public function setLatestChildDateTime(?\DateTime $dateTime = null): IComment {
- $this->data['latestChildDT'] = $dateTime;
- return $this;
- }
-
- public function getObjectType(): string {
- return $this->data['objectType'];
- }
-
- public function getObjectId(): string {
- return $this->data['objectId'];
- }
-
- public function setObject($objectType, $objectId): IComment {
- if (
- !is_string($objectType) || !trim($objectType)
- || !is_string($objectId) || trim($objectId) === ''
- ) {
- throw new \InvalidArgumentException('String expected.');
- }
- $this->data['objectType'] = trim($objectType);
- $this->data['objectId'] = trim($objectId);
- return $this;
- }
-
- public function getReferenceId(): ?string {
- return $this->data['referenceId'];
- }
-
- public function setReferenceId(?string $referenceId): IComment {
- if ($referenceId === null) {
- $this->data['referenceId'] = $referenceId;
- } else {
- $referenceId = trim($referenceId);
- if ($referenceId === '') {
- throw new \InvalidArgumentException('Non empty string expected.');
- }
- $this->data['referenceId'] = $referenceId;
- }
- return $this;
- }
-
- public function getReactions(): array {
- return $this->data['reactions'] ?? [];
- }
-
- public function setReactions(?array $reactions): IComment {
- $this->data['reactions'] = $reactions;
- return $this;
- }
-
- public function setExpireDate(?\DateTime $dateTime): IComment {
- $this->data['expire_date'] = $dateTime;
- return $this;
- }
-
- public function getExpireDate(): ?\DateTime {
- return $this->data['expire_date'];
- }
-
- protected function fromArray($data): IComment {
- foreach (array_keys($data) as $key) {
-
- $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key)));
- $setter = str_replace('Timestamp', 'DateTime', $setter);
- if (method_exists($this, $setter)) {
- $this->$setter($data[$key]);
- }
- }
- foreach (['actor', 'object'] as $role) {
- if (isset($data[$role . '_type']) && isset($data[$role . '_id'])) {
- $setter = 'set' . ucfirst($role);
- $this->$setter($data[$role . '_type'], $data[$role . '_id']);
- }
- }
- return $this;
- }
- }
|