Completed
Branch dev (a083dd)
by Arnaud
03:08
created

ArrayField::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use Doctrine\Common\Collections\Collection;
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 Collection) {
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
     * @return mixed
45
     */
46
    public function configureOptions(OptionsResolver $resolver)
47
    {
48
        $resolver->setDefaults([
49
            'glue' => ', ',
50
        ]);
51
    }
52
53
    /**
54
     * Set options values after options resolving.
55
     *
56
     * @param array $options
57
     *
58
     * @return mixed
59
     */
60 1
    public function setOptions(array $options)
61
    {
62 1
        $this->glue = $options['glue'];
63 1
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getType()
69
    {
70
        return 'array';
71
    }
72
}
73