TemplateToSubstringMapConverter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A convert() 0 16 4
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