Completed
Push — master ( 9fb92e...1099f2 )
by Tom
02:18
created

StringExtractor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 33
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B extractStrings() 0 14 5
A rebuild() 0 3 1
A __toString() 0 3 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Parser;
8
class StringExtractor {
9
	private $str;
10
	private $stringTable;
11
12
	public function __construct($str) {
13
		$parts = $this->extractStrings($str);
14
		$this->str = $parts[0];
15
		$this->stringTable = $parts[1];
16
	}
17
18
	private function extractStrings($str) {
19
		$pos = 0;
20
		$num = 0;
21
		$strings = [];
22
		while (isset($str[$pos]) && ($pos = strpos($str, '"', $pos)) !== false) {
23
			$end = strpos($str, '"', $pos+1);
24
			if (!$end) break;
25
			while ($str[$end-1] == '\\') $end = strpos($str, '"', $end+1);
26
			$strings['$___STR' . ++$num] = substr($str, $pos, $end-$pos+1);
27
			$str = substr_replace($str, '$___STR' . $num, $pos, $end-$pos+1);
28
		}
29
30
		return [$str, $strings];
31
	}
32
33
	public function rebuild($str) {
34
		return str_replace(array_keys($this->stringTable), array_values($this->stringTable), $str);
35
	}
36
37
	public function __toString() {
38
		return $this->str;
39
	}
40
}