Completed
Pull Request — master (#90)
by Arnaud
16:42
created

ArrayField::render()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 4
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use Doctrine\Common\Collections\Collection as DoctrineCollection;
6
use LAG\AdminBundle\Field\AbstractField;
7
use Exception;
8
use LAG\AdminBundle\Field\Configuration\ArrayFieldConfiguration;
9
use Traversable;
10
11
/**
12
 * Array field.
13
 *
14
 * Note : class can not be called Array by php restriction
15
 */
16
class ArrayField extends AbstractField
17
{
18
    /**
19
     * Render field value
20
     *
21
     * @param mixed $value
22
     * @return string
23
     * @throws Exception
24
     */
25 3
    public function render($value)
26
    {
27 3
        if (!is_array($value) && !($value instanceof Traversable)) {
28 1
            throw new Exception('Value should be an array instead of '.gettype($value));
29
        }
30 2
        if ($value instanceof DoctrineCollection) {
31 1
            $value = $value->toArray();
32
        }
33
34 2
        return implode($this->options['glue'], $value);
35
    }
36
    
37
    /**
38
     * @return string
39
     */
40
    public function getType()
41
    {
42
        return 'array';
43
    }
44
    
45
    /**
46
     * Return the Field's configuration class.
47
     *
48
     * @return string
49
     */
50
    public function getConfigurationClass()
51
    {
52
        return ArrayFieldConfiguration::class;
53
    }
54
}
55