FixedLengthFormattedStringIterator::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 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