1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dummify; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Capsule\Manager as DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Dummify class |
9
|
|
|
*/ |
10
|
|
|
class Dummify |
11
|
|
|
{ |
12
|
|
|
protected static $instance; |
13
|
|
|
|
14
|
|
|
protected $capsule; |
15
|
|
|
|
16
|
|
|
protected $table; |
17
|
|
|
|
18
|
|
|
protected $filter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates a single for Dummify |
22
|
|
|
*/ |
23
|
|
|
public static function initialize() |
24
|
|
|
{ |
25
|
|
|
$instance = new static(); |
26
|
|
|
static::$instance = $instance; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->capsule = new DB(); |
35
|
|
|
$this->table = null; |
36
|
|
|
$this->filter = null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns Dummify's instance |
41
|
|
|
*/ |
42
|
|
|
public static function getInstance() |
43
|
|
|
{ |
44
|
|
|
return static::$instance; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes and defines connection |
49
|
|
|
*/ |
50
|
|
|
public static function connectTo($params) |
51
|
|
|
{ |
52
|
|
|
$instance = static::getInstance(); |
53
|
|
|
|
54
|
|
|
if (is_null($instance)) { |
55
|
|
|
static::initialize(); |
56
|
|
|
$instance = static::getInstance(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$instance->addConnection($params); |
60
|
|
|
|
61
|
|
|
return $instance; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds a connection |
66
|
|
|
*/ |
67
|
|
|
public function addConnection($params) |
68
|
|
|
{ |
69
|
|
|
$this->capsule->addConnection($params, 'default'); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Selects a table and a passes a optional condition |
76
|
|
|
*/ |
77
|
|
|
public function from($table, callable $callable = null) |
78
|
|
|
{ |
79
|
|
|
$this->table = $table; |
80
|
|
|
$this->filter = $callable; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a factory using the connection |
87
|
|
|
*/ |
88
|
|
|
protected function getQuery() |
89
|
|
|
{ |
90
|
|
|
$query = $this->capsule->table($this->table); |
91
|
|
|
|
92
|
|
|
$filter = $this->filter; |
93
|
|
|
if (!is_null($filter)) { |
94
|
|
|
$query = $filter($query); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $query; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Populates a table with a row generator. |
102
|
|
|
*/ |
103
|
|
|
public function insert(callable $callable, $number = 1) |
104
|
|
|
{ |
105
|
|
|
while ($number-- > 0) { |
106
|
|
|
$data = $callable((object) []); |
107
|
|
|
$this->getQuery()->insert((array) $data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Iterates over update record |
115
|
|
|
*/ |
116
|
|
|
public function update(callable $callable) |
117
|
|
|
{ |
118
|
|
|
$data = $this->getQuery()->get(); |
119
|
|
|
|
120
|
|
|
$data->each(function ($row) use ($callable) { |
121
|
|
|
$this->getQuery() |
122
|
|
|
->where((array) $row) |
123
|
|
|
->update((array) $callable($row)); |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|