FixedLengthFormattedStringIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A newFromTemplate() 0 5 1
A parseString() 0 11 3
1
<?php
2
3
namespace itertools;
4
5
use InvalidArgumentException;
6
use Exception;
7
8
9
class FixedLengthFormattedStringIterator extends MapIterator
10
{
11
	protected $substringMap;
12
	protected $trim = null;
13
14
	public function __construct($inputIterator, array $substringMap, array $options = array())
15
	{
16
		$this->substringMap = $substringMap;
17
18
		if(array_key_exists('trim', $options)) {
19
			$this->trim = $options['trim'];
20
			unset($options['trim']);
21
		}
22
23
		if(count($options) > 0) {
24
			throw new InvalidArgumentException('Unknow options provided: ' . implode(',', array_keys($options)));
25
		}
26
27
		parent::__construct($inputIterator, array($this, 'parseString'));
28
	}
29
30
	static function newFromTemplate($inputIterator, $template, array $nameMap = array(), array $options = array())
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
	{
32
		$converter = new TemplateToSubstringMapConverter();
33
		return new self($inputIterator, $converter->convert($template, $nameMap), $options);
34
	}
35
36
	public function parseString($inputString)
37
	{
38
		$fields = array();
39
		foreach($this->substringMap as $name => $location) {
40
			$fields[$name] = substr($inputString, $location->getOffset(), $location->getLength());
41
			if(null !== $this->trim) {
42
				$fields[$name] = trim($fields[$name], $this->trim);
43
			}
44
		}
45
		return $fields;
46
	}
47
}
48