Completed
Pull Request — master (#15)
by Mads
24:44
created

TransformerMethods::convertDatetime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    public static function convertInteger($value): int
25
    {
26
        return (int) $value;
27
    }
28
29
    /**
30
     * @param $value
31
     * @return string
32
     */
33
    public static function convertString($value): string
34
    {
35
        return (string) $value;
36
    }
37
38
    /**
39
     * @param $value
40
     * @param $parameters
41
     * @return float
42
     */
43
    public static function convertFloat($value, $parameters): float
44
    {
45
        if (isset($parameters[0])) {
46
            return round($value, $parameters[0], PHP_ROUND_HALF_UP);
47
        }
48
49
        return (float) $value;
50
    }
51
52
    /**
53
     * @param $value
54
     * @return bool
55
     */
56
    public static function convertBoolean($value): bool
57
    {
58
        return (bool) $value;
59
    }
60
61
    /**
62
     * @param $value
63
     * @return array
64
     */
65
    public static function convertArray($value): array
66
    {
67
        return (array) $value;
68
    }
69
70
    /**
71
     * @param $value
72
     * @return string
73
     */
74
    public static function convertDatetime($value): string
75
    {
76
        return strtotime($value) > 0 ? date('c', strtotime($value)) : '';
77
    }
78
}
79