Completed
Push — master ( abe21f...dd633f )
by Ben
02:41
created

Json   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 6
c 3
b 0
f 2
lcom 0
cbo 0
dl 0
loc 45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canHandleFrom() 0 4 2
A canHandleTo() 0 4 2
A convertTo() 0 4 1
A convertFrom() 0 4 1
1
<?php
2
3
namespace Benrowe\Laravel\Config\Modifiers;
4
5
/**
6
 * Json modifier for Config
7
 *
8
 * @package Benrowe\Laravel\Config\Modifiers
9
 */
10
class Json implements Modifier
11
{
12
    /**
13
     * Determine if this value is json (array or object)
14
     *
15
     * @param  string $value
16
     * @return boolean
17
     */
18
    public function canHandleFrom($value)
19
    {
20
        return gettype($value) == 'array' || gettype($value) == 'object';
21
    }
22
23
    /**
24
     * Determine if we can convert this string into json
25
     * @param  string $value
26
     * @return boolean
27
     */
28
    public function canHandleTo($value)
29
    {
30
        return is_string($value) && in_array($value[0], ['[', '{']);
31
    }
32
33
    /**
34
     * Convert the string into the json object/array
35
     *
36
     * @param  string|array $value
37
     * @return mixed
38
     */
39
    public function convertTo($value)
40
    {
41
        return json_decode($value);
42
    }
43
44
    /**
45
     * Convert the complex object back to a string
46
     *
47
     * @param  mixed $value
48
     * @return string
49
     */
50
    public function convertFrom($value)
51
    {
52
        return json_encode($value);
53
    }
54
}
55