]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Entity/Notify.php
Merge pull request #10893 from nupplaphil/feat/depository_introduction
[friendica.git] / src / Navigation / Notifications / Entity / Notify.php
1 <?php
2
3 namespace Friendica\Navigation\Notifications\Entity;
4
5 use DateTime;
6 use Friendica\BaseEntity;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Renderer;
9 use Psr\Http\Message\UriInterface;
10
11 /**
12  * @property-read $type
13  * @property-read $name
14  * @property-read $url
15  * @property-read $photo
16  * @property-read $date
17  * @property-read $msg
18  * @property-read $uid
19  * @property-read $link
20  * @property-read $itemId
21  * @property-read $parent
22  * @property-read $seen
23  * @property-read $verb
24  * @property-read $otype
25  * @property-read $name_cache
26  * @property-read $msg_cache
27  * @property-read $uriId
28  * @property-read $parentUriId
29  * @property-read $id
30  */
31 class Notify extends BaseEntity
32 {
33         /** @var int */
34         protected $type;
35         /** @var string */
36         protected $name;
37         /** @var UriInterface */
38         protected $url;
39         /** @var UriInterface */
40         protected $photo;
41         /** @var DateTime */
42         protected $date;
43         /** @var string */
44         protected $msg;
45         /** @var int */
46         protected $uid;
47         /** @var UriInterface */
48         protected $link;
49         /** @var int */
50         protected $itemId;
51         /** @var int */
52         protected $parent;
53         /** @var bool */
54         protected $seen;
55         /** @var string */
56         protected $verb;
57         /** @var string */
58         protected $otype;
59         /** @var string */
60         protected $name_cache;
61         /** @var string */
62         protected $msg_cache;
63         /** @var int|null */
64         protected $uriId;
65         /** @var int|null */
66         protected $parentUriId;
67         /** @var int */
68         protected $id;
69
70         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)
71         {
72                 $this->type        = $type;
73                 $this->name        = $name;
74                 $this->url         = $url;
75                 $this->photo       = $photo;
76                 $this->date        = $date;
77                 $this->msg         = $msg;
78                 $this->uid         = $uid;
79                 $this->link        = $link;
80                 $this->itemId      = $itemId;
81                 $this->parent      = $parent;
82                 $this->seen        = $seen;
83                 $this->verb        = $verb;
84                 $this->otype       = $otype;
85                 $this->name_cache  = $name_cache;
86                 $this->msg_cache   = $msg_cache;
87                 $this->uriId       = $uriId;
88                 $this->parentUriId = $parentUriId;
89                 $this->id          = $id;
90         }
91
92         public function setSeen()
93         {
94                 $this->seen = true;
95         }
96
97         public function updateMsgFromPreamble($epreamble)
98         {
99                 $this->msg       = Renderer::replaceMacros($epreamble, ['$itemlink' => $this->link->__toString()]);
100                 $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
101         }
102
103         /**
104          * Formats a notification message with the notification author
105          *
106          * Replace the name with {0} but ensure to make that only once. The {0} is used
107          * later and prints the name in bold.
108          *
109          * @param string $name
110          * @param string $message
111          *
112          * @return string Formatted message
113          */
114         public static function formatMessage(string $name, string $message): string
115         {
116                 if ($name != '') {
117                         $pos = strpos($message, $name);
118                 } else {
119                         $pos = false;
120                 }
121
122                 if ($pos !== false) {
123                         $message = substr_replace($message, '{0}', $pos, strlen($name));
124                 }
125
126                 return $message;
127         }
128 }