-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslationTrait.php
45 lines (40 loc) · 1.19 KB
/
TranslationTrait.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
<?php
declare(strict_types=1);
namespace Webiik\Translation;
trait TranslationTrait
{
/**
* @param string $string
* @param bool $assoc
* @return array
*/
private function extractBrackets(string $string, bool $assoc = false): array
{
$extractions = [];
$openingBrackets = 0;
$closingBrackets = 0;
$brackets = [];
for ($i = 0; $i < strlen($string); $i++) {
if ($string[$i] == '{') {
$brackets[] = $i;
$openingBrackets++;
}
if ($string[$i] == '}') {
$brackets[] = $i;
$closingBrackets++;
}
if ($openingBrackets == $closingBrackets && $closingBrackets > 0) {
$extraction = substr($string, $brackets[0] + 1, $brackets[count($brackets) - 1] - $brackets[0] - 1);
if ($assoc) {
$extractions[$extraction] = 1;
} else {
$extractions[] = $extraction;
}
$openingBrackets = 0;
$closingBrackets = 0;
$brackets = [];
}
}
return $extractions;
}
}