-
Notifications
You must be signed in to change notification settings - Fork 7
/
ParametersEscaper.php
50 lines (39 loc) · 1.17 KB
/
ParametersEscaper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Exercise\GoogleTranslateBundle;
class ParametersEscaper
{
/** @var \ArrayObject */
protected $parametersArray;
/** @var \ArrayIterator */
protected $iterator;
public function escapeParameters($string)
{
$this->parametersArray = new \ArrayObject();
return preg_replace_callback(
"|%[\S]*%|",
array($this, 'escapeParametersCallback'),
$string);
}
public function unEscapeParameters($string)
{
if (!$this->parametersArray) {
throw new \Exception('You try unescape string that not be escaped');
}
$this->iterator = $this->parametersArray->getIterator();
return preg_replace_callback(
"|NotTranslatedString|",
array($this, 'unEscapeParametersCallback'),
$string);
}
private function escapeParametersCallback($matches)
{
$this->parametersArray->append($matches['0']);
return 'NotTranslatedString';
}
private function unEscapeParametersCallback($matches)
{
$value = $this->iterator->current();
$this->iterator->next();
return $value;
}
}