Passed
Push — main ( 6e070f...cb1775 )
by Jean-Christophe
04:50
created

PhpUtils::getParsedJs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 2
b 0
f 1
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace PHPMV\utils;
4
5
/**
6
 * Some php utilities.
7
 *
8
 * PHPMV\utilsPhpUtils
9
 * This class is part of php-vuejs
10
 *
11
 * @author jguillaumesio
12
 * @version 1.0.0
13
 * @package PHPMV\utils
14
 */
15
16
class PhpUtils {
17
	static private string $delimiter = '-';
18
	static private array $parsedJs = [];
19
20 2
	public static function importFromFile(string $filename, string $extension): string {
21 2
		return \str_replace(["\n","\r","\t"],"",\file_get_contents("$filename.$extension", true));
22
	}
23
24 1
	public static function parseFile(string $filename, string $extension): array {
25 1
		$pattern = '/\/\/' . self::$delimiter . '{3,}(.+?)' . self::$delimiter . '{3,}(.+?)\/\/' . self::$delimiter . '{3,}end' . self::$delimiter . '{3,}/s';
26 1
		$templateString = self::importFromFile($filename, $extension);
27 1
		\preg_match_all($pattern, $templateString, $templateArray);
28 1
		$iterationNumber = \count($templateArray[0]);
29 1
		for ($i = 0; $i < $iterationNumber; $i++) {
30 1
			self::$parsedJs[$templateArray[1][$i]] = $templateArray[2][$i];
31
		}
32 1
		return $templateArray;
33
	}
34
35 1
	public static function getParsedJs(string $name,array $variables = []): string {
36 1
		$element=self::$parsedJs[$name];
37 1
		foreach($variables as $key => $value){
38 1
			$element = \str_replace("{{ $key }}",$value, $element);
39
		}
40
41 1
		return $element;
42
	}
43
}
44