Test Failed
Push — master ( b3e488...48cf76 )
by Dominik
02:02
created

ValueSerializer::castValue()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 45.0325

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 1
cts 6
cp 0.1666
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 7
nop 1
crap 45.0325
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Serializer\Field;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
use Chubbyphp\Serialization\SerializerInterface;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
11
final class ValueSerializer implements FieldSerializerInterface
12
{
13
    /**
14
     * @var AccessorInterface
15
     */
16
    private $accessor;
17
18
    /**
19
     * @var string|null
20
     */
21
    private $cast;
22
23
    const CAST_BOOL = 'bool';
24
    const CAST_FLOAT = 'float';
25
    const CAST_INT = 'int';
26
27
    /**
28
     * @param AccessorInterface $accessor
29
     * @param string            $cast
30
     */
31 1
    public function __construct(AccessorInterface $accessor, string $cast = null)
32
    {
33 1
        $this->accessor = $accessor;
34
35 1
        if (null !== $cast) {
36
            $supportedCasts = [self::CAST_BOOL, self::CAST_FLOAT, self::CAST_INT];
37
            if (!in_array($cast, $supportedCasts, true)) {
38
                throw new \InvalidArgumentException(
39
                    sprintf('Cast %s is not support, supported casts: %s', $cast, implode(', ', $supportedCasts))
40
                );
41
            }
42
            $this->cast = $cast;
43
        }
44 1
    }
45
46
    /**
47
     * @param string                   $path
48
     * @param Request                  $request
49
     * @param object                   $object
50
     * @param SerializerInterface|null $serializer
51
     *
52
     * @return mixed
53
     */
54 1
    public function serializeField(string $path, Request $request, $object, SerializerInterface $serializer = null)
55
    {
56 1
        return $this->castValue($this->accessor->getValue($object));
57
    }
58 1
59
    /**
60
     * @param mixed $value
61
     *
62
     * @return mixed
63
     */
64
    private function castValue($value)
65
    {
66
        if (is_array($value)) {
67
            $castedValue = [];
68
            foreach ($value as $key => $subValue) {
69
                $castedValue[$key] = $this->castValue($subValue);
70
            }
71
72 1
            return $castedValue;
73
        }
74
75
        if (null !== $value && null !== $this->cast) {
76
            switch ($this->cast) {
77
                case self::CAST_BOOL:
78
                    return (bool) $value;
79
                case self::CAST_FLOAT:
80
                    return (float) $value;
81
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
82
                case self::CAST_INT:
83
                    return (int) $value;
84
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
85
            }
86
        }
87
88
        return $value;
89
    }
90
}
91