Issues (3)

src/Settings.php (3 issues)

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