Passed
Push — master ( 71b342...1d62b9 )
by Roeland
33:01 queued 20:23
created

DefinitionParameter::validateValue()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 6
nop 1
dl 0
loc 23
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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 License, version 3,
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Files_External\Lib;
25
26
/**
27
 * Parameter for an external storage definition
28
 */
29
class DefinitionParameter implements \JsonSerializable {
30
	// placeholder value for password fields, when the client updates a storage configuration
31
	// placeholder values are ignored and the field is left unmodified
32
	const UNMODIFIED_PLACEHOLDER = '__unmodified__';
33
34
	/** Value constants */
35
	const VALUE_TEXT = 0;
36
	const VALUE_BOOLEAN = 1;
37
	const VALUE_PASSWORD = 2;
38
	const VALUE_HIDDEN = 3;
39
40
	/** Flag constants */
41
	const FLAG_NONE = 0;
42
	const FLAG_OPTIONAL = 1;
43
	const FLAG_USER_PROVIDED = 2;
44
45
	/** @var string name of parameter */
46
	private $name;
47
48
	/** @var string human-readable parameter text */
49
	private $text;
50
51
	/** @var string human-readable parameter tooltip */
52
	private $tooltip = '';
53
54
	/** @var int value type, see self::VALUE_* constants */
55
	private $type = self::VALUE_TEXT;
56
57
	/** @var int flags, see self::FLAG_* constants */
58
	private $flags = self::FLAG_NONE;
59
60
	/**
61
	 * @param string $name
62
	 * @param string $text
63
	 */
64
	public function __construct($name, $text) {
65
		$this->name = $name;
66
		$this->text = $text;
67
	}
68
69
	/**
70
	 * @return string
71
	 */
72
	public function getName() {
73
		return $this->name;
74
	}
75
76
	/**
77
	 * @return string
78
	 */
79
	public function getText() {
80
		return $this->text;
81
	}
82
83
	/**
84
	 * Get value type
85
	 *
86
	 * @return int
87
	 */
88
	public function getType() {
89
		return $this->type;
90
	}
91
92
	/**
93
	 * Set value type
94
	 *
95
	 * @param int $type
96
	 * @return self
97
	 */
98
	public function setType($type) {
99
		$this->type = $type;
100
		return $this;
101
	}
102
103
	/**
104
	 * @return string
105
	 */
106
	public function getTypeName() {
107
		switch ($this->type) {
108
			case self::VALUE_BOOLEAN:
109
				return 'boolean';
110
			case self::VALUE_TEXT:
111
				return 'text';
112
			case self::VALUE_PASSWORD:
113
				return 'password';
114
			default:
115
				return 'unknown';
116
		}
117
	}
118
119
	/**
120
	 * @return int
121
	 */
122
	public function getFlags() {
123
		return $this->flags;
124
	}
125
126
	/**
127
	 * @param int $flags
128
	 * @return self
129
	 */
130
	public function setFlags($flags) {
131
		$this->flags = $flags;
132
		return $this;
133
	}
134
135
	/**
136
	 * @param int $flag
137
	 * @return self
138
	 */
139
	public function setFlag($flag) {
140
		$this->flags |= $flag;
141
		return $this;
142
	}
143
144
	/**
145
	 * @param int $flag
146
	 * @return bool
147
	 */
148
	public function isFlagSet($flag) {
149
		return (bool)($this->flags & $flag);
150
	}
151
152
	/**
153
	 * @return string
154
	 */
155
	public function getTooltip(): string {
156
		return $this->tooltip;
157
	}
158
159
	/**
160
	 * @param string $tooltip
161
	 * @return self
162
	 */
163
	public function setTooltip(string $tooltip) {
164
		$this->tooltip = $tooltip;
165
		return $this;
166
	}
167
168
	/**
169
	 * Serialize into JSON for client-side JS
170
	 *
171
	 * @return string
172
	 */
173
	public function jsonSerialize() {
174
		return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('value' => ...=> $this->getTooltip()) returns the type array<string,integer|string> which is incompatible with the documented return type string.
Loading history...
175
			'value' => $this->getText(),
176
			'flags' => $this->getFlags(),
177
			'type' => $this->getType(),
178
			'tooltip' => $this->getTooltip(),
179
		];
180
	}
181
182
	public function isOptional() {
183
		return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED);
184
	}
185
186
	/**
187
	 * Validate a parameter value against this
188
	 * Convert type as necessary
189
	 *
190
	 * @param mixed $value Value to check
191
	 * @return bool success
192
	 */
193
	public function validateValue(&$value) {
194
		switch ($this->getType()) {
195
			case self::VALUE_BOOLEAN:
196
				if (!is_bool($value)) {
197
					switch ($value) {
198
						case 'true':
199
							$value = true;
200
							break;
201
						case 'false':
202
							$value = false;
203
							break;
204
						default:
205
							return false;
206
					}
207
				}
208
				break;
209
			default:
210
				if (!$value && !$this->isOptional()) {
211
					return false;
212
				}
213
				break;
214
		}
215
		return true;
216
	}
217
}
218