Completed
Pull Request — dev (#17)
by Ben
11:05
created

Json::convertTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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