| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,158 @@ |
| 1 |
+<?php |
|
| 2 |
+/** |
|
| 3 |
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com> |
|
| 4 |
+ * |
|
| 5 |
+ * @author Double Bastion LLC |
|
| 6 |
+ * |
|
| 7 |
+ * @license GNU AGPL version 3 or any later version |
|
| 8 |
+ * |
|
| 9 |
+ * This program is free software; you can redistribute it and/or |
|
| 10 |
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
| 11 |
+ * License as published by the Free Software Foundation; either |
|
| 12 |
+ * version 3 of the License, or any later version. |
|
| 13 |
+ * |
|
| 14 |
+ * This program is distributed in the hope that it will be useful, |
|
| 15 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 16 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 17 |
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|
| 18 |
+ * |
|
| 19 |
+ * You should have received a copy of the GNU Affero General Public |
|
| 20 |
+ * License along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
| 21 |
+ * |
|
| 22 |
+ */ |
|
| 23 |
+ |
|
| 24 |
+declare(strict_types=1); |
|
| 25 |
+ |
|
| 26 |
+namespace OCA\SIPTripPhone; |
|
| 27 |
+ |
|
| 28 |
+use OCA\SIPTripPhone\AppInfo\Application; |
|
| 29 |
+use OCP\IConfig; |
|
| 30 |
+ |
|
| 31 |
+ |
|
| 32 |
+class AppConfig {
|
|
| 33 |
+ private $appName = 'sip_trip_phone'; |
|
| 34 |
+ |
|
| 35 |
+ /** @var IConfig */ |
|
| 36 |
+ private $config; |
|
| 37 |
+ |
|
| 38 |
+ /** |
|
| 39 |
+ * AppConfig constructor. |
|
| 40 |
+ * |
|
| 41 |
+ * @param IConfig $config |
|
| 42 |
+ */ |
|
| 43 |
+ public function __construct(IConfig $config) {
|
|
| 44 |
+ $this->config = $config; |
|
| 45 |
+ } |
|
| 46 |
+ |
|
| 47 |
+ /** |
|
| 48 |
+ * Get all setting values as an array |
|
| 49 |
+ * @return array |
|
| 50 |
+ */ |
|
| 51 |
+ public function getAllValues() {
|
|
| 52 |
+ $keys = array_keys($this->defaults); |
|
| 53 |
+ $values = array_map([$this, 'getAppValue'], $keys); |
|
| 54 |
+ $preparedKeys = array_map([$this, 'camelCase'], $keys); |
|
| 55 |
+ return array_combine($preparedKeys, $values); |
|
| 56 |
+ } |
|
| 57 |
+ |
|
| 58 |
+ /** |
|
| 59 |
+ * Get a value by key |
|
| 60 |
+ * @param string $key |
|
| 61 |
+ * @return string |
|
| 62 |
+ */ |
|
| 63 |
+ public function getAppValue($key) {
|
|
| 64 |
+ $defaultValue = null; |
|
| 65 |
+ if (array_key_exists($key, $this->defaults)){
|
|
| 66 |
+ $defaultValue = $this->defaults[$key]; |
|
| 67 |
+ } |
|
| 68 |
+ return $this->config->getAppValue($this->appName, $key, $defaultValue); |
|
| 69 |
+ } |
|
| 70 |
+ |
|
| 71 |
+ /** |
|
| 72 |
+ * Set a value by key |
|
| 73 |
+ * @param string $key |
|
| 74 |
+ * @param string $value |
|
| 75 |
+ */ |
|
| 76 |
+ public function setAppValue($key, $value) {
|
|
| 77 |
+ $this->config->setAppValue($this->appName, $key, $value); |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ /** |
|
| 81 |
+ * Set a value with magic __call invocation |
|
| 82 |
+ * @param string $key |
|
| 83 |
+ * @param array $args |
|
| 84 |
+ * @throws \BadFunctionCallException |
|
| 85 |
+ */ |
|
| 86 |
+ protected function setter($key, $args) {
|
|
| 87 |
+ if (array_key_exists($key, $this->defaults)) {
|
|
| 88 |
+ $this->setAppValue($key, $args[0]); |
|
| 89 |
+ } else {
|
|
| 90 |
+ throw new \BadFunctionCallException($key . ' is not a valid key'); |
|
| 91 |
+ } |
|
| 92 |
+ } |
|
| 93 |
+ |
|
| 94 |
+ /** |
|
| 95 |
+ * Get a value with magic __call invocation |
|
| 96 |
+ * @param string $key |
|
| 97 |
+ * @return string |
|
| 98 |
+ * @throws \BadFunctionCallException |
|
| 99 |
+ */ |
|
| 100 |
+ protected function getter($key) {
|
|
| 101 |
+ if (array_key_exists($key, $this->defaults)) {
|
|
| 102 |
+ return $this->getAppValue($key); |
|
| 103 |
+ } |
|
| 104 |
+ |
|
| 105 |
+ throw new \BadFunctionCallException($key . ' is not a valid key'); |
|
| 106 |
+ } |
|
| 107 |
+ |
|
| 108 |
+ /** |
|
| 109 |
+ * Translates property_name into propertyName |
|
| 110 |
+ * @param string $property |
|
| 111 |
+ * @return string |
|
| 112 |
+ */ |
|
| 113 |
+ protected function camelCase($property){
|
|
| 114 |
+ $split = explode('_', $property);
|
|
| 115 |
+ $ucFirst = implode('', array_map('ucfirst', $split));
|
|
| 116 |
+ return lcfirst($ucFirst); |
|
| 117 |
+ } |
|
| 118 |
+ |
|
| 119 |
+ /** |
|
| 120 |
+ * Does all the someConfig to some_config magic |
|
| 121 |
+ * @param string $property |
|
| 122 |
+ * @return string |
|
| 123 |
+ */ |
|
| 124 |
+ protected function propertyToKey($property){
|
|
| 125 |
+ $parts = preg_split('/(?=[A-Z])/', $property);
|
|
| 126 |
+ $column = null; |
|
| 127 |
+ |
|
| 128 |
+ foreach($parts as $part){
|
|
| 129 |
+ if($column === null){
|
|
| 130 |
+ $column = $part; |
|
| 131 |
+ } else {
|
|
| 132 |
+ $column .= '_' . lcfirst($part); |
|
| 133 |
+ } |
|
| 134 |
+ } |
|
| 135 |
+ |
|
| 136 |
+ return $column; |
|
| 137 |
+ } |
|
| 138 |
+ |
|
| 139 |
+ /** |
|
| 140 |
+ * Get/set an option value by calling getSomeOption method |
|
| 141 |
+ * @param string $methodName |
|
| 142 |
+ * @param array $args |
|
| 143 |
+ * @return string|null |
|
| 144 |
+ * @throws \BadFunctionCallException |
|
| 145 |
+ */ |
|
| 146 |
+ public function __call($methodName, $args){
|
|
| 147 |
+ $attr = lcfirst( substr($methodName, 3) ); |
|
| 148 |
+ $key = $this->propertyToKey($attr); |
|
| 149 |
+ if(strpos($methodName, 'set') === 0){
|
|
| 150 |
+ $this->setter($key, $args); |
|
| 151 |
+ } elseif(strpos($methodName, 'get') === 0) {
|
|
| 152 |
+ return $this->getter($key); |
|
| 153 |
+ } else {
|
|
| 154 |
+ throw new \BadFunctionCallException($methodName . |
|
| 155 |
+ ' does not exist'); |
|
| 156 |
+ } |
|
| 157 |
+ } |
|
| 158 |
+} |