Completed
Push — 2.0 ( 360d87...ea41ea )
by Olivier
03:48 queued 01:44
created

RenderedErrorCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
c 2
b 0
f 2
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
 * Representation of a rendered error collection.
16
 *
17
 * **Note:** The error collection is actually rendered as the instance is iterated.
18
 */
19
class RenderedErrorCollection implements \IteratorAggregate
20
{
21
	/**
22
	 * @var ErrorCollection
23
	 */
24
	private $collection;
25
26
	/**
27
	 * @var callable|RenderError
28
	 */
29
	private $render_error;
30
31
	/**
32
	 * @param ErrorCollection $collection
33
	 * @param RenderError|callable $render_error
34
	 */
35
	public function __construct(ErrorCollection $collection, callable $render_error = null)
36
	{
37
		$this->collection = $collection;
38
		$this->render_error = $render_error;
39
	}
40
41
	/**
42
	 * @inheritdoc
43
	 */
44
	public function getIterator()
45
	{
46
		/* @var $error Error */
47
48
		foreach ($this->collection as $attribute => $error)
49
		{
50
			yield $attribute => $this->render_error($error, $attribute);
51
		}
52
	}
53
54
	/**
55
	 * Renders an error into a string.
56
	 *
57
	 * @param Error $error
58
	 * @param string $attribute
59
	 *
60
	 * @return string
61
	 */
62
	protected function render_error(Error $error, $attribute)
63
	{
64
		$render_error = $this->render_error;
65
66
		return $render_error ? $render_error($error, $attribute, $this) : (string) $error;
67
	}
68
}
69