Passed
Push — main ( 1c4e95...6e2581 )
by Guillaume
02:37
created

JsUtils::kebabToPascal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPMV\utils;
4
5
class JsUtils {
6
	static private array $removeQuote = ["start" => "!!%", "end" => "%!!"];
7
8
	public static function cleanJSONFunctions(string $json): string {
9
		$pattern = '/(("|\')' . self::$removeQuote['start'] . ')|(' . self::$removeQuote['end'] . '("|\'))/';
10
		return \preg_replace($pattern, '', $json);
11
	}
12
13
	public static function removeQuotes(string $body): string {
14
		return self::$removeQuote["start"] . $body . self::$removeQuote["end"];
15
	}
16
17
	public static function generateFunction(string $body, array $params = [], bool $needRemoveQuote = true): string {
18
		if ($needRemoveQuote) {
19
			return self::removeQuotes("function(" . implode(",", $params) . "){" . $body . "}");
20
		}
21
		return "function(" . implode(",", $params) . "){" . $body . "}";
22
	}
23
24
	public static function declareVariable(string $type, string $name, $value, bool $lineBreak = true): string {
25
		$declaration = $type . " " . $name . " = " . $value . ";";
26
		if ($lineBreak) $declaration .= PHP_EOL;
27
		return $declaration;
28
	}
29
30
	public static function kebabToPascal(string $string): string {
31
		$string[0] = \strtoupper($string[0]);
32
		$pattern = '/(-\w{1})/';
33
		return \preg_replace_callback($pattern,
34
			function ($matches) {
35
				return \strtoupper($matches[1][1]);
36
			}, $string);
37
	}
38
}
39
40