Passed
Push — master ( 4bfcda...c8f2be )
by Michael
04:20
created

NativeTypesHandler::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 2
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Handler\DataTypeHandler;
5
6
/**
7
 * Handler of PHP-native types
8
 *
9
 * @package Mikemirten\Component\JsonApi\Mapper\Handler\DataTypeHandler
10
 */
11
class NativeTypesHandler implements DataTypeHandlerInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 4
    public function toResource($value, string $type, array $parameters)
17
    {
18 4
        return $this->process($value, $type);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 4
    public function fromResource($value, string $type, array $parameters)
25
    {
26 4
        return $this->process($value, $type);
27
    }
28
29
    /**
30
     * Process native type
31
     *
32
     * @param  mixed  $value
33
     * @param  string $type
34
     * @return mixed
35
     */
36 8
    protected function process($value, string $type)
37
    {
38 8
        if ($type === 'integer') {
39 2
            return (int) $value;
40
        }
41
42 6
        if ($type === 'float') {
43 2
            return (float) $value;
44
        }
45
46 4
        if ($type === 'boolean') {
47 2
            return (bool) $value;
48
        }
49
50 2
        return (string) $value;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supports(): array
57
    {
58
        return ['integer', 'float', 'string', 'boolean'];
59
    }
60
}