Passed
Push — master ( bf1943...bfd61d )
by Blizzz
09:37
created

SimpleSubstitutionTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processInput() 0 10 3
A checkPlaceholder() 0 9 3
A substituteIfString() 0 5 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) 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 License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files_External\Config;
25
26
/**
27
 * Trait SimpleSubstitutionTrait
28
 *
29
 * @package OCA\Files_External\Config
30
 * @since 16.0.0
31
 */
32
trait SimpleSubstitutionTrait {
33
	/**
34
	 * @var string the placeholder without $ prefix
35
	 * @since 16.0.0
36
	 */
37
	private $placeholder;
38
39
	/** @var string */
40
	protected $sanitizedPlaceholder;
41
42
	/**
43
	 * @param mixed $optionValue
44
	 * @param string $replacement
45
	 * @return mixed
46
	 * @since 16.0.0
47
	 */
48
	private function processInput($optionValue, string $replacement) {
49
		$this->checkPlaceholder();
50
		if (is_array($optionValue)) {
51
			foreach ($optionValue as &$value) {
52
				$value = $this->substituteIfString($value, $replacement);
53
			}
54
		} else {
55
			$optionValue = $this->substituteIfString($optionValue, $replacement);
56
		}
57
		return $optionValue;
58
	}
59
60
	/**
61
	 * @throws \RuntimeException
62
	 */
63
	protected function checkPlaceholder(): void {
64
		$this->sanitizedPlaceholder = trim(strtolower($this->placeholder));
65
		if(!(bool)\preg_match('/^[a-z0-9]*$/', $this->sanitizedPlaceholder)) {
66
			throw new \RuntimeException(sprintf(
67
				'Invalid placeholder %s, only [a-z0-9] are allowed', $this->sanitizedPlaceholder
68
			));
69
		}
70
		if($this->sanitizedPlaceholder === '') {
71
			throw new \RuntimeException('Invalid empty placeholder');
72
		}
73
	}
74
75
	/**
76
	 * @param mixed $value
77
	 * @param string $replacement
78
	 * @return mixed
79
	 */
80
	protected function substituteIfString($value, string $replacement) {
81
		if(is_string($value)) {
82
			return str_ireplace('$' . $this->sanitizedPlaceholder, $replacement, $value);
83
		}
84
		return $value;
85
	}
86
}
87