-
Notifications
You must be signed in to change notification settings - Fork 1
/
BiscuitQuery.php
89 lines (82 loc) · 2.51 KB
/
BiscuitQuery.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
<?php
class BiscuitQuery
{
private $query;
private $type;
private $table_name;
public function __construct($query)
{
$this->query = $query;
$this->type = BiscuitQuery::determineQueryType($query);
}
public function getQuery()
{
return $this->query;
}
protected function setTableName($name)
{
$this->table_name = $name;
}
public function getTableName()
{
return $this->table_name;
}
/*
* Function to determine what query type to use
*/
public static function determineQueryType($query)
{
// Get the first value of the query
$type = explode(' ', $query)[0];
switch (strtolower($type)) {
case 'build':
case 'insert':
$type = 'CREATE';
break;
case 'fetch':
$type = 'READ';
break;
case 'change':
case 'update':
$type = 'UPDATE';
break;
case 'destroy':
case 'remove':
$type = 'DELETE';
break;
default:
throw new BiscuitException('Invalid query type provided: ' . $type);
}
return strtolower($type);
}
/*
* Function to convert to the correct child object type
*/
public static function convertToChildQueryType(BiscuitQuery $query)
{
if ($query instanceof BiscuitQuery) {
// Get the first value of the query
$type = explode(' ', $query->getQuery())[0];
switch (strtolower($type)) {
case 'build':
return new BiscuitBuildQuery($query->getQuery());
case 'insert':
return new BiscuitInsertQuery($query->getQuery());
case 'fetch':
return new BiscuitFetchQuery($query->getQuery());
case 'change':
return new BiscuitChangeQuery($query->getQuery());
case 'update':
return new BiscuitUpdateQuery($query->getQuery());
case 'destroy':
return new BiscuitDestroyQuery($query->getQuery());
case 'remove':
return new BiscuitRemoveQuery($query->getQuery());
default:
throw new BiscuitException('The provided query type does not match a valid type: ' . $type);
}
} else {
return null;
}
}
}