Completed
Pull Request — dev (#17)
by Ben
11:05
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
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