Completed
Push — master ( dbcf6b...d53ab6 )
by Peter
44:13 queued 37:37
created

MultiLineToSingleLineTransformer::transform()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 9
cts 12
cp 0.75
rs 9.568
c 0
b 0
f 0
cc 4
nc 5
nop 3
crap 4.25
1
<?php
2
3
namespace TreeHouse\IoBundle\Item\Modifier\Data\Transformer;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Exception\TransformationFailedException;
7
use TreeHouse\Feeder\Modifier\Data\Transformer\TransformerInterface;
8
9
class MultiLineToSingleLineTransformer implements TransformerInterface
10
{
11 10
    public function transform($value, $key = null, ParameterBag $item = null)
12
    {
13 10
        if (is_null($value)) {
14
            return $value;
15
        }
16
17 10
        if (is_scalar($value)) {
18 10
            $value = (string) $value;
19
        }
20
21 10
        if (!is_string($value)) {
22
            throw new TransformationFailedException(
23
                sprintf('Expected a string to transform, got %s instead', json_encode($value))
24
            );
25
        }
26
27 10
        $value = preg_replace('/\n{1,}/', ' ', $value); // replace newlines with single newline
28 10
        $value = preg_replace('/ {2,}/', ' ', $value);  // replace double spaces with single space
29 10
        $value = trim($value);                          // trim leading and trailing spaces
30
31 10
        return $value;
32
    }
33
}
34