Test Failed
Push — master ( 2a75d8...e4b8e4 )
by Julien
12:49
created

Json::jsonEncode()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
trait Json
15
{
16
    /**
17
     * JSON Encode or fallback to value
18
     * @return string|false
19
     */
20
    public function jsonEncode($value, int $flags = JSON_UNESCAPED_SLASHES, int $depth = 512)
21
    {
22
        return json_encode($value, $flags, $depth) ?: $value;
23
    }
24
    
25
    /**
26
     * JSON Decode or fallback to value
27
     * @return mixed|string
28
     */
29
    public function jsonDecode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0)
30
    {
31
        return json_decode($json, $associative, $depth, $flags) ?: $json;
32
    }
33
}
34