Passed
Branch develop (17014a)
by Michael
03:42
created

Settings::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\Settings;
12
13
use miBadger\File\File;
14
15
/**
16
 * The settings class.
17
 *
18
 * @since 1.0.0
19
 */
20
class Settings implements \IteratorAggregate
21
{
22
	const DEFAULT_FILENAME = '.settings.json';
23
24
	/** @var array The settings */
25
	private $data;
26
27
	/**
28
	 * Contruct a settings object with the given data.
29
	 *
30
	 * @param $data = []
31
	 */
32 13
	public function __construct($data = [])
33
	{
34 13
		$this->data = $data;
35 13
	}
36
37
	/**
38
	 * {@inheritdoc}
39
	 */
40 1
	public function getIterator()
41
	{
42 1
		return new \ArrayIterator($this->data);
43
	}
44
45
	/**
46
	 * Returns the number of key-value mappings in the settings map.
47
	 *
48
	 * @return int the number of key-value mappings in the settings map.
49
	 */
50 1
	public function count()
51
	{
52 1
		return count($this->data);
53
	}
54
55
	/**
56
	 * Returns true if the settings map contains no key-value mappings.
57
	 *
58
	 * @return bool true if the settings map contains no key-value mappings.
59
	 */
60 2
	public function isEmpty()
61
	{
62 2
		return empty($this->data);
63
	}
64
65
	/**
66
	 * Returns true if the settings map contains a mapping for the specified key.
67
	 *
68
	 * @param string $key
69
	 * @return bool true if the settings map contains a mapping for the specified key.
70
	 */
71 6
	public function containsKey($key)
72
	{
73 6
		return isset($this->data[$key]);
74
	}
75
76
	/**
77
	 * Returns true if the settings map maps one or more keys to the specified value.
78
	 *
79
	 * @param string $value
80
	 * @return bool true if the settings map maps one or more keys to the specified value.
81
	 */
82 1
	public function containsValue($value)
83
	{
84 1
		return in_array($value, $this->data);
85
	}
86
87
	/**
88
	 * Returns the value to which the specified key is mapped, or null if the settings map contains no mapping for the key.
89
	 *
90
	 * @param string $key
91
	 * @return string the value to which the specified key is mapped, or null if the settings map contains no mapping for the key.
92
	 */
93 4
	public function get($key)
94
	{
95 4
		return $this->containsKey($key) ? $this->data[$key] : null;
96
	}
97
98
	/**
99
	 * Associates the specified value with the specified key in the settings map.
100
	 *
101
	 * @param string $key
102
	 * @param string $value
103
	 * @return $this
104
	 */
105 2
	public function set($key, $value)
106
	{
107 2
		$this->data[$key] = $value;
108
109 2
		return $this;
110
	}
111
112
	/**
113
	 * Removes the mapping for the specified key from the settings map if present.
114
	 *
115
	 * @param string $key
116
	 * @return $this
117
	 */
118 1
	public function remove($key)
119
	{
120 1
		unset($this->data[$key]);
121
122 1
		return $this;
123
	}
124
125
	/**
126
	 * Removes all of the mappings from the settings map.
127
	 *
128
	 * @return $this
129
	 */
130 2
	public function clear()
131
	{
132 2
		$this->data = [];
133
134 2
		return $this;
135
	}
136
137
	/**
138
	 * Load the settings file from the given location.
139
	 *
140
	 * @param $path = self::DEFAULT_FILENAME
141
	 * @return $this
142
	 * @throws FileException on failure.
143
	 * @throws \UnexpectedValueException on failure.
144
	 */
145 4
	public function load($path = self::DEFAULT_FILENAME)
146
	{
147 4
		$file = new File($path);
148
149 4
		if (($data = json_decode($file->read(), true)) === null) {
150 1
			throw new \UnexpectedValueException('Invalid JSON.');
151
		}
152
153 2
		$this->data = (array) $data;
154
155 2
		return $this;
156
	}
157
158
	/**
159
	 * Save the settings file at the given location.
160
	 *
161
	 * @param $path = self::DEFAULT_FILENAME
162
	 * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be Settings|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
163
	 * @throws FileException on failure.
164
	 */
165 1
	public function save($path = self::DEFAULT_FILENAME)
166
	{
167 1
		$file = new File($path);
168
169 1
		$file->write(json_encode($this->data));
170 1
	}
171
}
172