Completed
Push — dev ( e55f7a...57e45c )
by Ben
11s
created

Json::canHandle()   B

Complexity

Conditions 5
Paths 18

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
rs 8.8571
cc 5
eloc 4
nc 18
nop 2
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