ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
SanitizerImpl.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Refinery\Factory as Refinery;
24
25class SanitizerImpl implements Sanitizer
26{
27 protected const string HIGHLIGHT_START = '<span class="ilSearchHighlight">';
28 protected const string HIGHLIGHT_END = '</span>';
29 protected const string PLACEHOLDER_START = '[PLACEHOLDER_START]';
30 protected const string PLACEHOLDER_END = '[PLACEHOLDER_END]';
31
32 public function __construct(
33 protected Refinery $refinery
34 ) {
35 }
36
37 public function sanitize(string $text): string
38 {
39 return $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($text);
40 }
41
42 public function sanitizeAndSetUpPlaceholders(string $text): string
43 {
44 $text = str_replace(self::PLACEHOLDER_START, '', $text);
45 $text = str_replace(self::PLACEHOLDER_END, '', $text);
46 $text = $this->replaceInPairs(
47 self::HIGHLIGHT_START,
48 self::HIGHLIGHT_END,
49 self::PLACEHOLDER_START,
50 self::PLACEHOLDER_END,
51 $text
52 );
53 return $this->sanitize($text);
54 }
55
56 public function replacePlaceholders(string $html): string
57 {
58 return $this->replaceInPairs(
59 self::PLACEHOLDER_START,
60 self::PLACEHOLDER_END,
61 self::HIGHLIGHT_START,
62 self::HIGHLIGHT_END,
63 $html
64 );
65 }
66
67 protected function replaceInPairs(
68 string $search_start,
69 string $search_end,
70 string $replace_start,
71 string $replace_end,
72 string $text
73 ): string {
74 $regex = '/' . preg_quote($search_start, '/') . '(.*?)' . preg_quote($search_end, '/') . '/m';
75 $replacement = $replace_start . '$1' . $replace_end;
76 return preg_replace($regex, $replacement, $text);
77 }
78}
Builds data types.
Definition: Factory.php:36
replaceInPairs(string $search_start, string $search_end, string $replace_start, string $replace_end, string $text)
$text
Definition: xapiexit.php:21