Jsoner   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 31
c 2
b 1
f 0
dl 0
loc 86
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheDir() 0 6 1
A fillArray() 0 12 4
A fill() 0 12 4
A load() 0 13 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-12
6
 * Time: 下午4:28.
7
 */
8
9
namespace MiotApi\Util\Jsoner;
10
11
use MiotApi\Exception\JsonException;
12
use MiotApi\Util\Collection\Collection;
13
14
class Jsoner extends Collection
15
{
16
    const CACHE_DIR = 'json_cache';
17
18
    const SUFFIX = '.json';
19
20
    /**
21
     * 读取json文件里的内容为数组.
22
     *
23
     * @param $file
24
     *
25
     * @return bool|Jsoner
26
     */
27
    public static function load($file)
28
    {
29
        try {
30
            $file = str_replace(':', '_', $file);
31
            $items = JsonLoader::fileToArray(self::getCacheDir().$file.self::SUFFIX);
32
33
            if (!empty($items) && !isset($items['error-code'])) {
34
                return self::make($items);
35
            }
36
37
            return false;
38
        } catch (JsonException $exception) {
39
            return false;
40
        }
41
    }
42
43
    /**
44
     * json数据存储成json文件.
45
     *
46
     * @param $data
47
     * @param $file
48
     *
49
     * @return bool|Jsoner
50
     */
51
    public static function fill($data, $file)
52
    {
53
        try {
54
            $file = str_replace(':', '_', $file);
55
            $items = JsonLoader::dataToFile($data, self::getCacheDir().$file.self::SUFFIX);
56
            if (!empty($items) && !isset($items['error-code'])) {
57
                return self::make($items);
58
            }
59
60
            return false;
61
        } catch (JsonException $exception) {
62
            return false;
63
        }
64
    }
65
66
    /**
67
     * 数组缓存成json文件.
68
     *
69
     * @param $array
70
     * @param $file
71
     *
72
     * @return bool|Jsoner
73
     */
74
    public static function fillArray($array, $file)
75
    {
76
        try {
77
            $file = str_replace(':', '_', $file);
78
            $items = JsonLoader::arrayToFile($array, self::getCacheDir().$file.self::SUFFIX);
79
            if (!empty($items) && !isset($items['error-code'])) {
80
                return self::make($items);
81
            }
82
83
            return false;
84
        } catch (JsonException $exception) {
85
            return false;
86
        }
87
    }
88
89
    /**
90
     * 取缓存文件路径.
91
     *
92
     * @return string
93
     */
94
    public static function getCacheDir()
95
    {
96
        return dirname(dirname(dirname(__DIR__))).
97
            DIRECTORY_SEPARATOR.
98
            self::CACHE_DIR.
99
            DIRECTORY_SEPARATOR;
100
    }
101
}
102