Completed
Push — master ( ad782d...430e74 )
by Peter
01:20
created

JsonSettings   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 65
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFilename() 0 4 1
A fromJsonString() 0 9 2
A get() 0 9 2
A getFromSubKeys() 0 16 3
1
<?php
2
/*
3
 MIT License
4
 Copyright (c) 2010 - 2018 Peter Petermann
5
6
 Permission is hereby granted, free of charge, to any person
7
 obtaining a copy of this software and associated documentation
8
 files (the "Software"), to deal in the Software without
9
 restriction, including without limitation the rights to use,
10
 copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 copies of the Software, and to permit persons to whom the
12
 Software is furnished to do so, subject to the following
13
 conditions:
14
15
 The above copyright notice and this permission notice shall be
16
 included in all copies or substantial portions of the Software.
17
18
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
 OTHER DEALINGS IN THE SOFTWARE.
26
27
*/
28
29
namespace King23\Settings;
30
31
class JsonSettings implements SettingsInterface
32
{
33
    private $data = [];
34
35
    /**
36
     * @param string $filename
37
     * @return JsonSettings
38
     */
39
    public static function fromFilename(string $filename): JsonSettings
40
    {
41
        return static::fromJsonString(file_get_contents($filename));
42
    }
43
44
    /**
45
     * @param string $string
46
     * @return JsonSettings
47
     */
48
    public static function fromJsonString(string $string): JsonSettings
49
    {
50
51
        // @todo remove once minimum is php 7.3
52
        if (!defined("JSON_THROW_ON_ERROR")) define("JSON_THROW_ON_ERROR", 4194304);
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
53
        $settings = new JsonSettings();
54
        $settings->data = json_decode($string, true, 512, JSON_THROW_ON_ERROR);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($string, tru...2, JSON_THROW_ON_ERROR) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        return $settings;
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param mixed $default
61
     * @return array|int|mixed|null|string
62
     */
63
    public function get($key, $default = null)
64
    {
65
        $keys = explode(".", $key);
66
        $result = $this->getFromSubKeys($keys, $this->data);
67
        if (is_null($result)) {
68
            return $default;
69
        }
70
        return $result;
71
    }
72
73
    /**
74
     * @param array $keys
75
     * @param array $data
76
     * @return null|string|int|array
77
     */
78
    private function getFromSubKeys(array $keys, array $data)
79
    {
80
        $key = array_shift($keys);
81
82
        // key doesn't exist, so we can return null
83
        if (!array_key_exists($key, $data)) {
84
            return null;
85
        }
86
        // key is the last key, so we can return the contents
87
        if (count($keys) == 0) {
88
            return $data[$key];
89
        }
90
91
        // more subkeys, so lets dive in
92
        return $this->getFromSubKeys($keys, $data[$key]);
93
    }
94
95
}
96