1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\DataAnonymization; |
4
|
|
|
|
5
|
|
|
class Blueprint |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Default primary key for all blueprints. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected static $defaultPrimary = ['id']; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Table to blueprint. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $table; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Array of columns. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $columns = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Table primary key. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public $primary; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Current column. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $currentColumn = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Callback that builds blueprint. |
44
|
|
|
* |
45
|
|
|
* @var callable |
46
|
|
|
*/ |
47
|
|
|
protected $callback; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Blueprint constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $table |
53
|
|
|
* @param callable|null $callback |
54
|
|
|
*/ |
55
|
|
|
public function __construct($table, callable $callback) |
56
|
|
|
{ |
57
|
|
|
$this->table = $table; |
58
|
|
|
$this->callback = $callback; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Setter for default primary key. |
63
|
|
|
* |
64
|
|
|
* @param string|array $key |
65
|
|
|
*/ |
66
|
|
|
public static function setDefaultPrimary($key) |
67
|
|
|
{ |
68
|
|
|
self::$defaultPrimary = (array) $key; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add a column to blueprint. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function column($name) |
79
|
|
|
{ |
80
|
|
|
$this->currentColumn = [ |
81
|
|
|
'name' => $name, |
82
|
|
|
'where' => null, |
83
|
|
|
'replace' => null, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add where to the current column. |
91
|
|
|
* |
92
|
|
|
* @param string $rawSql |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function where($rawSql) |
97
|
|
|
{ |
98
|
|
|
$this->currentColumn['where'] = $rawSql; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set how data should be replaced. |
105
|
|
|
* |
106
|
|
|
* @param callable|string $callback |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function replaceWith($callback) |
111
|
|
|
{ |
112
|
|
|
$this->currentColumn['replace'] = $callback; |
113
|
|
|
|
114
|
|
|
$this->columns[] = $this->currentColumn; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Build the current blueprint. |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function build() |
123
|
|
|
{ |
124
|
|
|
$callback = $this->callback; |
125
|
|
|
|
126
|
|
|
$callback($this); |
127
|
|
|
|
128
|
|
|
if (is_null($this->primary)) { |
129
|
|
|
$this->primary = self::$defaultPrimary; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Setter for a primary key. |
137
|
|
|
* |
138
|
|
|
* @param string|array $key |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function primary($key) |
143
|
|
|
{ |
144
|
|
|
$this->primary = (array) $key; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|