|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Query Factory |
|
7
|
|
|
* |
|
8
|
|
|
* @package Windstorm |
|
9
|
|
|
* @author Rougin Gutib <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class QueryFactory |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Rougin\Windstorm\QueryInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $query; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $table = ''; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Initializes the factory instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
|
27
|
|
|
*/ |
|
28
|
15 |
|
public function __construct(QueryInterface $query) |
|
29
|
|
|
{ |
|
30
|
15 |
|
$this->query = $query; |
|
31
|
15 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Stores a newly created resource in storage. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $data |
|
37
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function create($data) |
|
40
|
|
|
{ |
|
41
|
3 |
|
return $this->query->insertInto($this->table)->values($data); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Deletes the specified resource from storage. |
|
46
|
|
|
* |
|
47
|
|
|
* @param integer $id |
|
48
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function delete($id) |
|
51
|
|
|
{ |
|
52
|
3 |
|
$query = $this->query->deleteFrom($this->table); |
|
53
|
|
|
|
|
54
|
3 |
|
return $query->where('id')->equals($id); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Finds the specified resource from storage. |
|
59
|
|
|
* |
|
60
|
|
|
* @param integer $id |
|
61
|
|
|
* @param array $fields |
|
62
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function find($id, $fields = array()) |
|
65
|
|
|
{ |
|
66
|
3 |
|
empty($fields) && $fields = array($this->table[0] . '.*'); |
|
67
|
|
|
|
|
68
|
3 |
|
$query = $this->query->select($fields)->from($this->table); |
|
69
|
|
|
|
|
70
|
3 |
|
return $query->where('id')->equals((integer) $id)->limit(1); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Paginates the specified page number and items per page. |
|
75
|
|
|
* |
|
76
|
|
|
* @param integer $page |
|
77
|
|
|
* @param integer $limit |
|
78
|
|
|
* @param array $fields |
|
79
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function paginate($limit = 10, $offset = 0, $fields = array()) |
|
82
|
|
|
{ |
|
83
|
3 |
|
empty($fields) && $fields = array($this->table[0] . '.*'); |
|
84
|
|
|
|
|
85
|
3 |
|
$query = $this->query->select($fields)->from($this->table); |
|
86
|
|
|
|
|
87
|
3 |
|
return $query->limit((integer) $limit, (integer) $offset); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Updates the specified resource in storage. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array|integer $id |
|
94
|
|
|
* @param array $data |
|
95
|
|
|
* @return boolean |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function update($id, array $data, $primary = 'id') |
|
98
|
|
|
{ |
|
99
|
3 |
|
$query = $this->query->update($this->table); |
|
100
|
|
|
|
|
101
|
3 |
|
foreach ((array) $data as $key => $value) |
|
102
|
|
|
{ |
|
103
|
3 |
|
$query = $query->set($key, $value); |
|
104
|
2 |
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
return $query->where($primary)->equals($id); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|