-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.php
More file actions
60 lines (50 loc) · 1.49 KB
/
Source.php
File metadata and controls
60 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Silver\Database;
use Silver\Database\Source\Table as TableSource;
use Silver\Database\Source\Model as ModelSource;
use Silver\Database\Source\Query as QuerySource;
use Silver\Database\Source;
use Silver\Database\Query;
use Silver\Database\Parts\Name;
abstract class Source
{
private $name;
protected function __construct(string $name)
{
if (empty($name)) {
throw new \Exception("Source must have a name.");
}
$this->name = $name;
}
public function name(): string
{
return $this->name;
}
abstract public function primary();
abstract public function table();
abstract public function sourcePart();
public function namePart(): Name
{
return Name::ensure($this->name());
}
public function reference(string $refName, Query $q, string $alias, array $args=[]): object
{
throw new \Exception('Source ' . static::class . ' does not supports references.');
}
public function relation(string $relName, string $alias): object
{
throw new \Exception('Source ' . static::class . ' does not supports relations.');
}
public static function make($source, string $alias = null): Source
{
if ($source instanceof Query) {
return new QuerySource($source, $alias);
} else if (class_exists($source) and is_subclass_of($source, QueryObject::class)) {
return new ModelSource($source, $alias);
} else if (is_string($source)) {
return new TableSource($source, $alias);
} else {
throw new Exception("Wrong class name '$model'"); // Should be QueryException
}
}
}