public function testGetInstanceReturnsUrlLinkerSingleton() { $urlLinkerSingleton1 = UrlLinker::getInstance(); $urlLinkerSingleton2 = UrlLinker::getInstance(); $this->assertSame($urlLinkerSingleton1, $urlLinkerSingleton2, 'Should always return the same instance (singleton)'); $this->assertInstanceOf('Kwi\\UrlLinker', $urlLinkerSingleton1); $this->assertNotSame(new UrlLinker(), $urlLinkerSingleton1); }
/** * process a message: * - replace urls with links * - replace object references with links * @param varchar $message */ public static function processAndFormatMessage($message, $replacements = 'user,object,url') { if (empty($message)) { return $message; } $replacements = Util\toTrimmedArray($replacements); // replace urls with links if (in_array('url', $replacements)) { $message = \Kwi\UrlLinker::getInstance()->linkUrlsAndEscapeHtml($message); } //replace object references with links if (in_array('object', $replacements) && preg_match_all('/(.?)#(\\d+)(.?)/', $message, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { // check if not a html code if ($match[1] == '&' && $match[3] == ';') { continue; } $templateId = Objects::getTemplateId($match[2]); $name = Objects::getName($match[2]); $name = strlen($name) > 30 ? mb_substr($name, 0, 30) . '…' : $name; $message = str_replace($match[0], $match[1] . '<a class="click obj-ref" itemid="' . $match[2] . '" templateid= "' . $templateId . '" title="' . $name . '"' . '>#' . $match[2] . '</a>' . $match[3], $message); } } //replace users with their names if (in_array('user', $replacements) && preg_match_all('/@([\\w\\.\\-]+[\\w])/', $message, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $userId = DM\User::getIdByName($match[1]); if (is_numeric($userId)) { $userName = $match[1]; $message = str_replace($match[0], '<span class="cDB user-ref" title="' . User::getDisplayName($userId) . '">@' . $userName . '</span>', $message); } } } return $message; }