Completed
Push — master ( f07209...e7c824 )
by Anton
04:34
created

Map::parseString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 4
nc 3
nop 2
1
<?php
2
3
namespace Utils {
4
5
	use Arr, Explorer, Url;
6
7
	class Map {
8
9
		private $map = [];
10
11
		# Parse string
12
13
		private function parseString(string $string, string $regex) {
14
15
			$parts = preg_split('/\//', $string, 0, PREG_SPLIT_NO_EMPTY);
16
17
			foreach ($parts as $name) if (!preg_match($regex, $name)) return false;
18
19
			# ------------------------
20
21
			return $parts;
22
		}
23
24
		# Parse item
25
26
		private function parseItem(array $item) {
27
28
			$data = Arr::select($item, ['path', 'handler']);
29
30
			if (false === ($path = $this->parseString($data['path'], REGEX_MAP_ITEM_PATH))) return;
31
32
			if (false === ($handler = $this->parseString($data['handler'], REGEX_MAP_ITEM_HANDLER))) return;
33
34
			$this->map[] = ['path' => $path, 'handler' => implode('\\', $handler)];
35
		}
36
37
		# Constructor
38
39
		public function __construct() {
40
41
			$file_name = (DIR_SYSTEM_DATA . 'Map.json');
42
43
			if (false === ($map = Explorer::json($file_name))) return;
44
45
			foreach ($map as $item) if (is_array($item)) $this->parseItem($item);
46
		}
47
48
		# Get handler by url
49
50
		public function handler(Url $url) {
51
52
			foreach ($this->map as $item) if ($item['path'] === $url->path()) return $item['handler'];
53
54
			return false;
55
		}
56
	}
57
}
58