Completed
Push — master ( 3a8823...81c6e7 )
by Piotr
02:46
created

Collection::format()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 12
nc 5
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Display\Property\Formatter;
11
12
use FSi\Bundle\AdminBundle\Display\Property\ValueFormatter;
13
14
class Collection implements ValueFormatter
15
{
16
    /**
17
     * @var array|\FSi\Bundle\AdminBundle\Display\Property\ValueFormatter[]
18
     */
19
    private $formatters;
20
21
    /**
22
     * @param array|\FSi\Bundle\AdminBundle\Display\Property\ValueFormatter[] $formatters
23
     */
24
    public function __construct(array $formatters)
25
    {
26
        $this->formatters = $formatters;
27
    }
28
29
    /**
30
     * @param mixed $value
31
     * @return array|mixed
32
     */
33
    public function format($value)
34
    {
35
        if (empty($value)) {
36
            return $value;
37
        }
38
39
        if (!is_array($value) && !$value instanceof \Iterator) {
40
            throw new \InvalidArgumentException("Collection decorator require value to be an array or implement \\Iterator");
41
        }
42
43
        $formatted = array();
44
        foreach ($value as $key => $val) {
45
            $formattedValue = $val;
46
            foreach ($this->formatters as $formatter) {
47
                $formattedValue = $formatter->format($formattedValue);
48
            }
49
50
            $formatted[$key] = $formattedValue;
51
        }
52
53
        return $formatted;
54
    }
55
}
56