@@ -19,155 +19,155 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Settings implements \IteratorAggregate |
| 21 | 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 | - public function __construct($data = []) |
|
| 33 | - { |
|
| 34 | - $this->data = $data; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - */ |
|
| 40 | - public function getIterator() |
|
| 41 | - { |
|
| 42 | - 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 | - public function count() |
|
| 51 | - { |
|
| 52 | - 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 | - public function isEmpty() |
|
| 61 | - { |
|
| 62 | - 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 | - public function containsKey($key) |
|
| 72 | - { |
|
| 73 | - 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 | - public function containsValue($value) |
|
| 83 | - { |
|
| 84 | - 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 | - public function get($key) |
|
| 94 | - { |
|
| 95 | - 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 | - public function set($key, $value) |
|
| 106 | - { |
|
| 107 | - $this->data[$key] = $value; |
|
| 108 | - |
|
| 109 | - 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 | - public function remove($key) |
|
| 119 | - { |
|
| 120 | - unset($this->data[$key]); |
|
| 121 | - |
|
| 122 | - return $this; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Removes all of the mappings from the settings map. |
|
| 127 | - * |
|
| 128 | - * @return $this |
|
| 129 | - */ |
|
| 130 | - public function clear() |
|
| 131 | - { |
|
| 132 | - $this->data = []; |
|
| 133 | - |
|
| 134 | - 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 | - public function load($path = self::DEFAULT_FILENAME) |
|
| 146 | - { |
|
| 147 | - $file = new File($path); |
|
| 148 | - |
|
| 149 | - if (($data = json_decode($file->read(), true)) === null) { |
|
| 150 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $this->data = (array) $data; |
|
| 154 | - |
|
| 155 | - return $this; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Save the settings file at the given location. |
|
| 160 | - * |
|
| 161 | - * @param $path = self::DEFAULT_FILENAME |
|
| 162 | - * @return $this |
|
| 163 | - * @throws FileException on failure. |
|
| 164 | - */ |
|
| 165 | - public function save($path = self::DEFAULT_FILENAME) |
|
| 166 | - { |
|
| 167 | - $file = new File($path); |
|
| 168 | - |
|
| 169 | - $file->write(json_encode($this->data)); |
|
| 170 | - |
|
| 171 | - return $this; |
|
| 172 | - } |
|
| 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 | + public function __construct($data = []) |
|
| 33 | + { |
|
| 34 | + $this->data = $data; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * {@inheritdoc} |
|
| 39 | + */ |
|
| 40 | + public function getIterator() |
|
| 41 | + { |
|
| 42 | + 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 | + public function count() |
|
| 51 | + { |
|
| 52 | + 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 | + public function isEmpty() |
|
| 61 | + { |
|
| 62 | + 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 | + public function containsKey($key) |
|
| 72 | + { |
|
| 73 | + 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 | + public function containsValue($value) |
|
| 83 | + { |
|
| 84 | + 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 | + public function get($key) |
|
| 94 | + { |
|
| 95 | + 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 | + public function set($key, $value) |
|
| 106 | + { |
|
| 107 | + $this->data[$key] = $value; |
|
| 108 | + |
|
| 109 | + 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 | + public function remove($key) |
|
| 119 | + { |
|
| 120 | + unset($this->data[$key]); |
|
| 121 | + |
|
| 122 | + return $this; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Removes all of the mappings from the settings map. |
|
| 127 | + * |
|
| 128 | + * @return $this |
|
| 129 | + */ |
|
| 130 | + public function clear() |
|
| 131 | + { |
|
| 132 | + $this->data = []; |
|
| 133 | + |
|
| 134 | + 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 | + public function load($path = self::DEFAULT_FILENAME) |
|
| 146 | + { |
|
| 147 | + $file = new File($path); |
|
| 148 | + |
|
| 149 | + if (($data = json_decode($file->read(), true)) === null) { |
|
| 150 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $this->data = (array) $data; |
|
| 154 | + |
|
| 155 | + return $this; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Save the settings file at the given location. |
|
| 160 | + * |
|
| 161 | + * @param $path = self::DEFAULT_FILENAME |
|
| 162 | + * @return $this |
|
| 163 | + * @throws FileException on failure. |
|
| 164 | + */ |
|
| 165 | + public function save($path = self::DEFAULT_FILENAME) |
|
| 166 | + { |
|
| 167 | + $file = new File($path); |
|
| 168 | + |
|
| 169 | + $file->write(json_encode($this->data)); |
|
| 170 | + |
|
| 171 | + return $this; |
|
| 172 | + } |
|
| 173 | 173 | } |