Completed
Push — 2.0 ( 9a9bd1...98f64c )
by Olivier
02:55
created

ErrorCollectionIterator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getIterator() 0 9 2
A render_error() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie;
13
14
/**
15
 * Iterates over an error collection and return rendered errors.
16
 */
17
class ErrorCollectionIterator implements \IteratorAggregate
18
{
19
	/**
20
	 * @var ErrorCollection
21
	 */
22
	private $collection;
23
24
	/**
25
	 * @var callable|RenderError
26
	 */
27
	private $render_error;
28
29
	/**
30
	 * @param ErrorCollection $collection
31
	 * @param RenderError|callable $render_error
32
	 */
33
	public function __construct(ErrorCollection $collection, callable $render_error = null)
34
	{
35
		$this->collection = $collection;
36
		$this->render_error = $render_error;
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 */
42
	public function getIterator()
43
	{
44
		/* @var $error Error */
45
46
		foreach ($this->collection as $attribute => $error)
47
		{
48
			yield $attribute => $this->render_error($error, $attribute);
49
		}
50
	}
51
52
	/**
53
	 * Renders an error into a string.
54
	 *
55
	 * @param Error $error
56
	 * @param string $attribute
57
	 *
58
	 * @return string
59
	 */
60
	protected function render_error(Error $error, $attribute)
61
	{
62
		$render_error = $this->render_error;
63
64
		return $render_error ? $render_error($error, $attribute, $this->collection) : (string) $error;
65
	}
66
}
67