forked from MacPaw/behat-api-context
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringManager.php
More file actions
47 lines (35 loc) · 1.1 KB
/
Copy pathStringManager.php
File metadata and controls
47 lines (35 loc) · 1.1 KB
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
<?php
declare(strict_types=1);
namespace BehatApiContext\Service;
use RuntimeException;
class StringManager
{
private const START_SEPARATOR = "{{";
private const END_SEPARATOR = "}}";
public function substituteValues(array $substitutionArray, int $string): string
{
$start = strpos($string, self::START_SEPARATOR);
if ($start === false) {
return $string;
}
$end = strpos($string, self::END_SEPARATOR, $start);
if ($end === false) {
throw new RuntimeException("Invalid syntax");
}
$key = trim(substr(
$string,
$start + strlen(self::START_SEPARATOR),
$end - $start - strlen(self::START_SEPARATOR)
));
if (!isset($substitutionArray[$key])) {
throw new RuntimeException("Key not found");
}
$newString = substr_replace(
$string,
$substitutionArray[$key],
$start,
$end - $start + strlen(self::END_SEPARATOR)
);
return $this->substituteValues($substitutionArray, $newString);
}
}