Completed
Push — master ( de0cb7...0a75f1 )
by Mads
06:19
created

TransformerMethods   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 85
ccs 15
cts 22
cp 0.6818
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A convertString() 0 3 1
A convertBoolean() 0 3 1
A convertInteger() 0 3 1
A convertFloat() 0 7 2
A convertJson() 0 3 1
A convertArray() 0 7 2
A convertDatetime() 0 3 2
A convertObject() 0 7 2
1
<?php
2
3
namespace Napp\Core\Api\Transformers;
4
5
/**
6
 * Class TransformerMethods
7
 * @package Napp\Core\Api\Transformers
8
 */
9
class TransformerMethods
10
{
11
    /**
12
     * @param $value
13
     * @return mixed
14
     */
15
    public static function convertJson($value)
16
    {
17
        return json_decode($value);
18
    }
19
20
    /**
21
     * @param $value
22
     * @return int
23
     */
24 28
    public static function convertInteger($value): int
25
    {
26 28
        return (int) $value;
27
    }
28
29
    /**
30
     * @param $value
31
     * @return string
32
     */
33 26
    public static function convertString($value): string
34
    {
35 26
        return (string) $value;
36
    }
37
38
    /**
39
     * @param $value
40
     * @param $parameters
41
     * @return float
42
     */
43 12
    public static function convertFloat($value, $parameters): float
44
    {
45 12
        if (isset($parameters[0])) {
46 2
            return round($value, $parameters[0], PHP_ROUND_HALF_UP);
47
        }
48
49 10
        return (float) $value;
50
    }
51
52
    /**
53
     * @param $value
54
     * @return bool
55
     */
56 8
    public static function convertBoolean($value): bool
57
    {
58 8
        return (bool) $value;
59
    }
60
61
    /**
62
     * @param $value
63
     * @return array
64
     */
65 2
    public static function convertArray($value): array
66
    {
67 2
        if (\method_exists($value, 'toArray')) {
68
            return $value->toArray();
69
        }
70
71 2
        return (array) $value;
72
    }
73
74
    /**
75
     * @param $value
76
     * @return mixed|object
77
     */
78
    public static function convertObject($value)
79
    {
80
        if ($value instanceof \JsonSerializable) {
81
            return $value->jsonSerialize();
82
        }
83
84
        return (object) $value;
85
    }
86
87
    /**
88
     * @param $value
89
     * @return string
90
     */
91 2
    public static function convertDatetime($value): string
92
    {
93 2
        return strtotime($value) > 0 ? date('c', strtotime($value)) : '';
94
    }
95
}
96