-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueryBuilder.php
More file actions
92 lines (76 loc) · 2.33 KB
/
QueryBuilder.php
File metadata and controls
92 lines (76 loc) · 2.33 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
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
<?php
namespace UniMapper;
/**
* @method \UniMapper\Query\Select select()
* @method \UniMapper\Query\SelectOne selectOne($primaryValue)
* @method \UniMapper\Query\Insert insert(array $data)
* @method \UniMapper\Query\Update update(array $data)
* @method \UniMapper\Query\UpdateOne updateOne($primaryValue, array $data)
* @method \UniMapper\Query\Delete delete()
* @method \UniMapper\Query\DeleteOne deleteOne($primaryValue)
* @method \UniMapper\Query\Count count()
*/
class QueryBuilder
{
/** @var array */
private static $queries = [
"count" => "UniMapper\Query\Count",
"raw" => "UniMapper\Query\Raw",
"delete" => "UniMapper\Query\Delete",
"deleteOne" => "UniMapper\Query\DeleteOne",
"select" => "UniMapper\Query\Select",
"selectOne" => "UniMapper\Query\SelectOne",
"insert" => "UniMapper\Query\Insert",
"update" => "UniMapper\Query\Update",
"updateOne" => "UniMapper\Query\UpdateOne"
];
/** @var array */
private static $afterRun = [];
/** @var array */
private static $beforeRun = [];
/** @var Entity\Reflection $reflection */
private $reflection;
/**
* @param mixed $entity Entity object, class or name
*/
public function __construct($entity)
{
$this->reflection = Entity\Reflection::load($entity);
}
public function __call($name, $arguments)
{
if (!isset(self::$queries[$name])) {
throw new Exception\InvalidArgumentException(
"Query with name " . $name . " does not exist!"
);
}
array_unshift($arguments, $this->reflection);
$class = new \ReflectionClass(self::$queries[$name]);
return $class->newInstanceArgs($arguments);
}
public static function beforeRun(callable $callback)
{
self::$beforeRun[] = $callback;
}
public static function afterRun(callable $callback)
{
self::$afterRun[] = $callback;
}
public static function getBeforeRun()
{
return self::$beforeRun;
}
public static function getAfterRun()
{
return self::$afterRun;
}
/**
* Register custom query
*
* @param string $class
*/
public static function registerQuery($class)
{
self::$queries[$class::getName()] = $class;
}
}