Completed
Branch master (b7eb35)
by Arnaud
02:59
created

ArrayField::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use Doctrine\Common\Collections\Collection as DoctrineCollection;
6
use LAG\AdminBundle\Field\Field;
7
use Exception;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
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 Field
17
{
18
    protected $glue;
19
20
    /**
21
     * Render field value
22
     *
23
     * @param mixed $value
24
     * @return string
25
     * @throws Exception
26
     */
27 1
    public function render($value)
28
    {
29 1
        if (!is_array($value) && !($value instanceof Traversable)) {
30
            throw new Exception('Value should be an array instead of '.gettype($value));
31
        }
32 1
        if ($value instanceof DoctrineCollection) {
33 1
            $value = $value->toArray();
34
        }
35
36 1
        return implode($this->glue, $value);
37
    }
38
39
    /**
40
     * Configure options resolver.
41
     *
42
     * @param OptionsResolver $resolver
43
     */
44
    public function configureOptions(OptionsResolver $resolver)
45
    {
46
        $resolver->setDefaults([
47
            'glue' => ', ',
48
        ]);
49
    }
50
51
    /**
52
     * Set options values after options resolving.
53
     *
54
     * @param array $options
55
     */
56 1
    public function setOptions(array $options)
57
    {
58 1
        $this->glue = $options['glue'];
59 1
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getType()
65
    {
66
        return 'array';
67
    }
68
}
69