Completed
Pull Request — dev (#17)
by Ben
11:05
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
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
14
     *
15
     * @param  string $value
16
     * @return boolean
17
     */
18
    public function canHandle($value, $direction)
19
    {
20
        $canTo = $direction == self::DIRECTION_TO && is_string($value) && in_array($value[0], ['[', '{']);
21
        $canFrom = $direction == self::DIRECTION_FROM && gettype($value) == 'array' || gettype($value) == 'object';
22
        return $canTo xor $canFrom;
23
    }
24
25
    public function convertTo($value)
26
    {
27
        return json_decode($value);
28
    }
29
30
    public function convertFrom($value)
31
    {
32
        return json_encode($value);
33
    }
34
}
35