Commit | Line | Data |
---|---|---|
158bd3ca TD |
1 | <?php |
2 | namespace wcf\util; | |
3 | ||
4 | /** | |
5 | * Replaces quoted strings in a text. | |
6 | * | |
9f959ced | 7 | * @author Marcel Werk |
c839bd49 | 8 | * @copyright 2001-2018 WoltLab GmbH |
158bd3ca | 9 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 10 | * @package WoltLabSuite\Core\Util |
158bd3ca | 11 | */ |
18284789 | 12 | final class StringStack { |
c94e3ec7 AE |
13 | /** |
14 | * hash index | |
15 | * @var integer | |
16 | */ | |
17 | protected static $i = 0; | |
18 | ||
9f959ced MS |
19 | /** |
20 | * local string stack | |
5fffbcb8 | 21 | * @var string[][] |
9f959ced | 22 | */ |
058cbd6a | 23 | protected static $stringStack = []; |
158bd3ca TD |
24 | |
25 | /** | |
26 | * Replaces a string with an unique hash value. | |
9f959ced MS |
27 | * |
28 | * @param string $string | |
29 | * @param string $type | |
158bd3ca | 30 | * @return string $hash |
6f37a5f5 | 31 | * @param string $delimiter |
158bd3ca | 32 | */ |
8630d3f6 | 33 | public static function pushToStringStack($string, $type = 'default', $delimiter = '@@') { |
29ddf8ad TD |
34 | self::$i++; |
35 | $hash = $delimiter.StringUtil::getHash(self::$i.uniqid(microtime()).$string).$delimiter; | |
158bd3ca TD |
36 | |
37 | if (!isset(self::$stringStack[$type])) { | |
058cbd6a | 38 | self::$stringStack[$type] = []; |
158bd3ca TD |
39 | } |
40 | ||
41 | self::$stringStack[$type][$hash] = $string; | |
42 | ||
43 | return $hash; | |
44 | } | |
45 | ||
46 | /** | |
9f959ced | 47 | * Reinserts strings that have been replaced by unique hash values. |
59dc0db6 | 48 | * |
9f959ced MS |
49 | * @param string $string |
50 | * @param string $type | |
51 | * @return string | |
158bd3ca TD |
52 | */ |
53 | public static function reinsertStrings($string, $type = 'default') { | |
54 | if (isset(self::$stringStack[$type])) { | |
55 | foreach (self::$stringStack[$type] as $hash => $value) { | |
838e315b SG |
56 | if (mb_strpos($string, $hash) !== false) { |
57 | $string = str_replace($hash, $value, $string); | |
158bd3ca TD |
58 | unset(self::$stringStack[$type][$hash]); |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | return $string; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Returns the stack. | |
9f959ced MS |
68 | * |
69 | * @param string $type | |
158bd3ca TD |
70 | * @return array |
71 | */ | |
72 | public static function getStack($type = 'default') { | |
73 | if (isset(self::$stringStack[$type])) { | |
74 | return self::$stringStack[$type]; | |
75 | } | |
76 | ||
058cbd6a | 77 | return []; |
158bd3ca | 78 | } |
18284789 | 79 | |
1d5f9363 MS |
80 | /** |
81 | * Forbid creation of StringStack objects. | |
82 | */ | |
83 | private function __construct() { | |
84 | // does nothing | |
85 | } | |
dcb3a44c | 86 | } |