StripKeysPunctuationTransformer::strip()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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 TreeHouse\Feeder\Modifier\Item\Transformer;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
7
class StripKeysPunctuationTransformer implements TransformerInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $punctuation;
13
14
    /**
15
     * @var string
16
     */
17
    protected $regex;
18
19
    /**
20
     * @param array $punctuation
21
     */
22 4
    public function __construct(array $punctuation = ['.', ',', ':', ';'])
23
    {
24 4
        $this->punctuation = $punctuation;
25
26 4
        $this->regex = sprintf(
27 4
            '/[%s+]/',
28 4
            implode(
29 4
                '',
30 4
                array_map(
31 4
                    function ($value) {
32 4
                        return preg_quote($value, '/');
33 4
                    },
34 4
                    $this->punctuation
35 4
                )
36 4
            )
37 4
        );
38 4
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 4
    public function transform(ParameterBag $item)
44
    {
45 4
        $parameters = $item->all();
46 4
        $this->stripKeysPunctuation($parameters);
47 4
        $item->replace($parameters);
48 4
    }
49
50
    /**
51
     * @param array $arr
52
     */
53 4
    protected function stripKeysPunctuation(array &$arr)
54
    {
55 4
        $new = [];
56
57 4
        foreach ($arr as $key => &$value) {
58 4
            if (is_array($value)) {
59 2
                $this->stripKeysPunctuation($value);
60 2
            }
61
62 4
            $new[$this->strip($key)] = $value;
63 4
        }
64
65 4
        $arr = $new;
66 4
    }
67
68
    /**
69
     * @param string $string
70
     *
71
     * @return string
72
     */
73 4
    protected function strip($string)
74
    {
75 4
        return preg_replace($this->regex, '', $string);
76
    }
77
}
78