TemplateToSubstringMapConverter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace itertools;
4
5
use itertools\SubstringLocation;
6
7
8
class TemplateToSubstringMapConverter
9
{
10
	public function __construct()
11
	{
12
	}
13
14
	public function convert($template, array $nameMap = array())
15
	{
16
		preg_match_all('/<[a-zA-Z0-9_ -]+>|[a-zA-Z0-9_-]+/', $template, $substrings, PREG_OFFSET_CAPTURE);
17
		$map = array();
18
		foreach($substrings[0] as $i => $substring) {
19
			$name = trim($substring[0], '< >');
20
			$name = array_key_exists($name, $nameMap) ? $nameMap[$name] : $name;
21
			if(strpos($substring[0], '<') === false) {
22
				$templatePartAfterField = substr($template, $substring[1] + strlen($substring[0]));
23
				$map[$name] = new SubstringLocation($substring[1], strlen($substring[0]) + strspn($templatePartAfterField, ' '));
24
			} else {
25
				$map[$name] = new SubstringLocation($substring[1], strlen($substring[0]));
26
			}
27
		}
28
		return $map;
29
	}
30
}
31
 
32