]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Entity/Notify.php
Merge pull request #11131 from urbalazs/copyright-missing
[friendica.git] / src / Navigation / Notifications / Entity / Notify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Navigation\Notifications\Entity;
23
24 use DateTime;
25 use Friendica\BaseEntity;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Renderer;
28 use Psr\Http\Message\UriInterface;
29
30 /**
31  * @property-read $type
32  * @property-read $name
33  * @property-read $url
34  * @property-read $photo
35  * @property-read $date
36  * @property-read $msg
37  * @property-read $uid
38  * @property-read $link
39  * @property-read $itemId
40  * @property-read $parent
41  * @property-read $seen
42  * @property-read $verb
43  * @property-read $otype
44  * @property-read $name_cache
45  * @property-read $msg_cache
46  * @property-read $uriId
47  * @property-read $parentUriId
48  * @property-read $id
49  */
50 class Notify extends BaseEntity
51 {
52         /** @var int */
53         protected $type;
54         /** @var string */
55         protected $name;
56         /** @var UriInterface */
57         protected $url;
58         /** @var UriInterface */
59         protected $photo;
60         /** @var DateTime */
61         protected $date;
62         /** @var string */
63         protected $msg;
64         /** @var int */
65         protected $uid;
66         /** @var UriInterface */
67         protected $link;
68         /** @var int */
69         protected $itemId;
70         /** @var int */
71         protected $parent;
72         /** @var bool */
73         protected $seen;
74         /** @var string */
75         protected $verb;
76         /** @var string */
77         protected $otype;
78         /** @var string */
79         protected $name_cache;
80         /** @var string */
81         protected $msg_cache;
82         /** @var int|null */
83         protected $uriId;
84         /** @var int|null */
85         protected $parentUriId;
86         /** @var int */
87         protected $id;
88
89         public function __construct(int $type, string $name, UriInterface $url, UriInterface $photo, DateTime $date, int $uid, UriInterface $link, bool $seen, string $verb, string $otype, string $name_cache, string $msg = null, string $msg_cache = null, int $itemId = null, int $uriId = null, int $parent = null, ?int $parentUriId = null, ?int $id = null)
90         {
91                 $this->type        = $type;
92                 $this->name        = $name;
93                 $this->url         = $url;
94                 $this->photo       = $photo;
95                 $this->date        = $date;
96                 $this->msg         = $msg;
97                 $this->uid         = $uid;
98                 $this->link        = $link;
99                 $this->itemId      = $itemId;
100                 $this->parent      = $parent;
101                 $this->seen        = $seen;
102                 $this->verb        = $verb;
103                 $this->otype       = $otype;
104                 $this->name_cache  = $name_cache;
105                 $this->msg_cache   = $msg_cache;
106                 $this->uriId       = $uriId;
107                 $this->parentUriId = $parentUriId;
108                 $this->id          = $id;
109         }
110
111         public function setSeen()
112         {
113                 $this->seen = true;
114         }
115
116         public function updateMsgFromPreamble($epreamble)
117         {
118                 $this->msg       = Renderer::replaceMacros($epreamble, ['$itemlink' => $this->link->__toString()]);
119                 $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
120         }
121
122         /**
123          * Formats a notification message with the notification author
124          *
125          * Replace the name with {0} but ensure to make that only once. The {0} is used
126          * later and prints the name in bold.
127          *
128          * @param string $name
129          * @param string $message
130          *
131          * @return string Formatted message
132          */
133         public static function formatMessage(string $name, string $message): string
134         {
135                 if ($name != '') {
136                         $pos = strpos($message, $name);
137                 } else {
138                         $pos = false;
139                 }
140
141                 if ($pos !== false) {
142                         $message = substr_replace($message, '{0}', $pos, strlen($name));
143                 }
144
145                 return $message;
146         }
147 }