1 | <?php |
||
13 | class DbDef implements \JsonSerializable { |
||
14 | /// Properties /// |
||
15 | |||
16 | /** |
||
17 | * @var Db The database connection to send definitions to. |
||
18 | */ |
||
19 | protected $db; |
||
20 | |||
21 | /** |
||
22 | * @var array The columns that need to be set in the table. |
||
23 | */ |
||
24 | protected $columns; |
||
25 | |||
26 | /** |
||
27 | * @var array Options to send to the table definitaion call. |
||
28 | */ |
||
29 | protected $options; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * @var string The name of the currently working table. |
||
34 | */ |
||
35 | protected $table; |
||
36 | |||
37 | /** |
||
38 | * @var array An array of indexes. |
||
39 | */ |
||
40 | protected $indexes; |
||
41 | |||
42 | /// Methods /// |
||
43 | |||
44 | /** |
||
45 | * Initialize an instance of the {@link DbDef} class. |
||
46 | * |
||
47 | * @param Db $db The database to execute against. |
||
48 | */ |
||
49 | 12 | public function __construct($db) { |
|
53 | |||
54 | /** |
||
55 | * Reset the internal state of this object so that it can be re-used. |
||
56 | * |
||
57 | * @return DbDef Returns $this for fluent calls. |
||
58 | */ |
||
59 | 12 | public function reset() { |
|
67 | |||
68 | /** |
||
69 | * Define a column. |
||
70 | * |
||
71 | * @param string $name The column name. |
||
72 | * @param string $type The column type. |
||
73 | * @param mixed $nullDefault Whether the column is required or it's default. |
||
74 | * |
||
75 | * null|true |
||
76 | * : The column is not required. |
||
77 | * false |
||
78 | * : The column is required. |
||
79 | * Anything else |
||
80 | * : The column is required and this is its default. |
||
81 | * |
||
82 | * @param string|array $index The index that the column participates in. |
||
83 | * @return DbDef |
||
84 | */ |
||
85 | 12 | public function column($name, $type, $nullDefault = false, $index = null) { |
|
101 | |||
102 | /** |
||
103 | * Get an array column def from a structured function call. |
||
104 | * |
||
105 | * @param string $type The database type of the column. |
||
106 | * @param mixed $nullDefault Whether or not to allow null or the default value. |
||
107 | * |
||
108 | * null|true |
||
109 | * : The column is not required. |
||
110 | * false |
||
111 | * : The column is required. |
||
112 | * Anything else |
||
113 | * : The column is required and this is its default. |
||
114 | * |
||
115 | * @return array Returns the column def as an array. |
||
116 | */ |
||
117 | 12 | protected function columnDef($type, $nullDefault = false) { |
|
132 | |||
133 | /** |
||
134 | * Define the primary key in the database. |
||
135 | * |
||
136 | * @param string $name The name of the column. |
||
137 | * @param string $type The datatype for the column. |
||
138 | * @return DbDef |
||
139 | */ |
||
140 | 2 | public function primaryKey($name, $type = 'int') { |
|
152 | |||
153 | /** |
||
154 | * Execute the table def against the database. |
||
155 | * |
||
156 | * @param bool $reset Whether or not to reset the db def upon completion. |
||
157 | * @return DbDef $this Returns $this for fluent calls. |
||
158 | */ |
||
159 | 12 | public function exec($reset = true) { |
|
172 | |||
173 | /** |
||
174 | * Set the name of the table. |
||
175 | * |
||
176 | * @param string|null $name The name of the table. |
||
177 | * @return DbDef|string Returns $this for fluent calls. |
||
178 | */ |
||
179 | 12 | public function table($name = null) { |
|
186 | |||
187 | /** |
||
188 | * Add or update an index. |
||
189 | * |
||
190 | * @param string|array $columns An array of columns or a single column name. |
||
191 | * @param string $type One of the `Db::INDEX_*` constants. |
||
192 | * @param string $suffix An index suffix to group columns together in an index. |
||
193 | * @return DbDef Returns $this for fluent calls. |
||
194 | */ |
||
195 | 12 | public function index($columns, $type, $suffix = '') { |
|
236 | |||
237 | /** |
||
238 | * Set an option that will be passed on to the final {@link Db::setTableDef()} call. |
||
239 | * |
||
240 | * @param string $key The option key. |
||
241 | * @param mixed $value The option value. |
||
242 | * @return DbDef Returns $this for fluent calls. |
||
243 | */ |
||
244 | 2 | public function option($key, $value) { |
|
248 | |||
249 | /** |
||
250 | * Specify data which should be serialized to JSON. |
||
251 | * |
||
252 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
253 | * @return mixed data which can be serialized by {@link json_encode()}, |
||
254 | * which is a value of any type other than a resource. |
||
255 | */ |
||
256 | 12 | public function jsonSerialize() { |
|
263 | |||
264 | /** |
||
265 | * Get the db connection to send definitions to. |
||
266 | * |
||
267 | * @return Db Returns the db connection. |
||
268 | * @see DbDef::setDb() |
||
269 | */ |
||
270 | 2 | public function getDb() { |
|
273 | |||
274 | /** |
||
275 | * Set the db connection to send definitions to. |
||
276 | * |
||
277 | * @param Db $db The new database connection. |
||
278 | * @see DbDef::getDef() |
||
279 | */ |
||
280 | public function setDb($db) { |
||
283 | } |
||
284 |