Completed
Pull Request — dev (#17)
by Ben
02:00
created

Json   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 0
cbo 0
dl 0
loc 25
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B canHandle() 0 6 5
A convertTo() 0 4 1
A convertFrom() 0 4 1
1
<?php
2
3
/**
4
 * @author Ben Rowe <[email protected]>
5
 * @license    http://www.opensource.org/licenses/mit-license.html MIT License
6
 */
7
8
namespace Benrowe\Laravel\Config\Modifiers;
9
10
/**
11
 * Json modifier for Config
12
 *
13
 * @package Benrowe\Laravel\Config\Modifiers
14
 */
15
class Json implements Modifier
16
{
17
    /**
18
     * Determine if this value is json
19
     *
20
     * @param  string $value
21
     * @return boolean
22
     */
23
    public function canHandle($value, $direction)
24
    {
25
        $canTo = $direction == self::DIRECTION_TO && is_string($value) && in_array($value[0], ['[', '{']);
26
        $canFrom = $direction == self::DIRECTION_FROM && gettype($value) == 'array' || gettype($value) == 'object';
27
        return $canTo xor $canFrom;
28
    }
29
30
    public function convertTo($value)
31
    {
32
        return json_decode($value);
33
    }
34
35
    public function convertFrom($value)
36
    {
37
        return json_encode($value);
38
    }
39
}
40