VirtualTables::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 9
c 4
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 4
nc 4
nop 1
1
<?php
2
namespace Kir\MySQL\Tools;
3
4
use Closure;
5
use Kir\MySQL\Builder\Select;
6
7
class VirtualTables {
8
	/** @var array<string, Closure|Select> */
9
	private array $virtualTables = [];
10
11
	/**
12
	 * @param string $tableName
13
	 * @param Select|Closure $select
14
	 * @return $this
15
	 */
16
	public function add(string $tableName, $select) {
17
		$this->virtualTables[$tableName] = $select;
18
		return $this;
19
	}
20
21
	/**
22
	 * @param string|VirtualTable $tableName
23
	 * @return bool
24
	 */
25
	public function has($tableName): bool {
26
		return array_key_exists((string) $tableName, $this->virtualTables);
27
	}
28
29
	/**
30
	 * @param string|VirtualTable $tableName
31
	 * @return Select|null
32
	 */
33
	public function get($tableName): ?Select {
34
		if($this->has($tableName)) {
35
			$table = $this->virtualTables[(string) $tableName];
36
			if($table instanceof Closure) {
37
				if($tableName instanceof VirtualTable) {
38
					$params = $tableName->getParams();
39
					return $table($params);
40
				}
41
				return $table();
42
			}
43
			return $table;
44
		}
45
		return null;
46
	}
47
}
48