ArrayObjectParamConverter::getArrayObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
ccs 6
cts 6
cp 1
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Gendoria\ParamConverterBundle\Request\ParamConverter;
10
11
use ArrayObject;
12
use InvalidArgumentException;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
14
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * This param converter converts parameters to array objects.
19
 *
20
 * Parameter has to be string of values separated with delimiter. By default
21
 * delimiter is comma, but you can set your own by passing options.
22
 *
23
 * Param converter invocation:
24
 *
25
 * `@ParamConverter("parameter_name")`
26
 *
27
 * With custom delimiter:
28
 *
29
 * `@ParamConverter("parameter_name", options={"delimiter" = "|"})`
30
 *
31
 * @author Tomasz Struczyński <[email protected]>
32
 */
33
class ArrayObjectParamConverter implements ParamConverterInterface
34
{
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @throws InvalidArgumentException Thrown, when parameter has been already parsed to array or object.
39
     */
40 7
    public function apply(Request $request, ParamConverter $configuration)
41
    {
42 7
        $param = $configuration->getName();
43
44 7
        if (!$request->attributes->has($param)) {
45 1
            return false;
46
        }
47
48 6
        $options = $configuration->getOptions();
49 6
        $value = $request->attributes->get($param);
50
51 6
        if (!$value && $configuration->isOptional()) {
52 1
            return false;
53
        }
54
55 6
        if (is_object($value) || is_array($value)) {
56 2
            throw new InvalidArgumentException('Parameter already parsed to object or array.');
57
        }
58
59 4
        if (!empty($options['delimiter'])) {
60 1
            $delimiter = $options['delimiter'];
61 1
        } else {
62 3
            $delimiter = ',';
63
        }
64
65 4
        $array = $this->getArrayObject($delimiter, $value);
66 4
        $request->attributes->set($param, $array);
67
68 4
        return true;
69
    }
70
71
    /**
72
     * Get array object instance based on parameter value.
73
     *
74
     * @param string $delimiter
75
     * @param string $value
76
     *
77
     * @return ArrayObject
78
     */
79 4
    private function getArrayObject($delimiter, $value)
80
    {
81 4
        if (!empty($value)) {
82 3
            $arrayValues = explode($delimiter, $value);
83 3
        } else {
84 1
            $arrayValues = array();
85
        }
86
87 4
        return new ArrayObject($arrayValues);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function supports(ParamConverter $configuration)
94
    {
95 1
        if (null === $configuration->getClass()) {
96 1
            return false;
97
        }
98
99 1
        return 'ArrayObject' === $configuration->getClass();
100
    }
101
}
102