Passed
Push — main ( c2a6e5...44af98 )
by Guillaume
02:49
created

PhpUtils::importFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPMV\utils;
4
5
class PhpUtils {
6
	static private string $delimiter = '---';
7
	static private array $parsedJs = [];
8
9 2
	public static function importFromFile(string $filename, string $extension): string {
10 2
		return \str_replace(["\n","\r","\t"],"",\file_get_contents("$filename.$extension", true));
11
	}
12
13 1
	public static function parseFile(string $filename, string $extension): array {
14 1
		$pattern = '/' . self::$delimiter . '(.+?)' . self::$delimiter . '(.+?)' . self::$delimiter . 'end' . self::$delimiter . '/s';
15 1
		$templateString = self::importFromFile($filename, $extension);
16 1
		\preg_match_all($pattern, $templateString, $templateArray);
17 1
		$iterationNumber = count($templateArray[0]);
18 1
		for ($i = 0; $i < $iterationNumber; $i++) {
19 1
			self::$parsedJs[$templateArray[1][$i]] = $templateArray[2][$i];
20
		}
21 1
		return $templateArray;
22
	}
23
24 1
	public static function getParsedJs(string $name,array $variables = []): string {
25 1
		if(isset($variables)){
26 1
			foreach($variables as $key => $value){
27 1
				self::$parsedJs[$name] = \str_replace("{{ $key }}",$value, self::$parsedJs[$name]);
28
			}
29
		}
30 1
		return self::$parsedJs[$name];
31
	}
32
}
33
34