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

ArrayField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 39
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 11 4
A getType() 0 4 1
A getConfigurationClass() 0 4 1
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