/
Builder.php
94 lines (78 loc) · 2 KB
/
Builder.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Compolomus\LSQLQueryBuilder;
use Compolomus\LSQLQueryBuilder\{
System\Traits\Helper,
Parts\Insert,
Parts\Select,
Parts\Update,
Parts\Delete
};
/**
* @method Insert insert(array $args = [])
* @method Select select(array $fields = [])
* @method Update update(array $args = [])
* @method Delete delete(integer $id = 0, string $field = 'id')
*/
class Builder
{
use Helper;
private $table;
private $placeholders = [];
private $data = [];
public function __construct(?string $table = null)
{
if ($table) {
$this->setTable($table);
}
}
public function setTable(string $table): Builder
{
$this->table = $table;
return $this;
}
public function table(): string
{
return $this->table ? $this->escapeField($this->table) : '';
}
public function placeholders(): array
{
$return = $this->placeholders;
$this->placeholders = [];
return $return;
}
public function addPlaceholders($placeholders): void
{
$this->placeholders += $placeholders;
}
public function __set(string $name, $value): void
{
$this->data[$name] = $value;
}
public function __get(string $name)
{
return $this->data[$name] ?? null;
}
// public function __isset(string $name): bool
// {
// return isset($this->data[$name]);
// }
//
// public function __unset(string $name): void
// {
// unset($this->data[$name]);
// }
public function __call(string $name, $args)
{
$class = "Compolomus\\LSQLQueryBuilder\\Parts\\" . ucfirst($name);
if (class_exists($class)) {
$this->$name = new $class(...$args);
$this->$name->setBase($this);
return $this->$name;
}
throw new BuilderException('Undefined class ' . $class . ' |Builder call|');
}
public function __toString(): string
{
return current($this->data)->__toString();
}
}