|
1
|
|
|
<?php |
|
2
|
|
|
namespace Y0lk\SQLDumper; |
|
3
|
|
|
|
|
4
|
|
|
use ArrayObject; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A TableDumperCollection is used to group TableDumper objects together, allowing you to specify dump options on multiple table at once. |
|
8
|
|
|
* All TableDumper methods can be called directly on a TableDumperCollection, and will be executed on all the TableDumper instances in that collection. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Gabriel Jean <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class TableDumperCollection extends ArrayObject |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritDoc} |
|
16
|
|
|
*/ |
|
17
|
5 |
|
public function append($value) |
|
18
|
|
|
{ |
|
19
|
|
|
//Make sure we're adding a TableDumper object |
|
20
|
5 |
|
if (!($value instanceof TableDumper)) { |
|
21
|
1 |
|
throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
//Append with table_name as key |
|
25
|
4 |
|
return $this->offsetSet($value->getTable()->getName(), $value); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
22 |
|
public function offsetSet($index, $newval) |
|
32
|
|
|
{ |
|
33
|
|
|
//Make sure we're adding a TableDumper object |
|
34
|
22 |
|
if (!($newval instanceof TableDumper)) { |
|
35
|
1 |
|
throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
//Append with table_name as key |
|
39
|
21 |
|
return parent::offsetSet($newval->getTable()->getName(), $newval); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Table|string Adds a table, either by name, or by Table instance, to the collection |
|
45
|
|
|
* |
|
46
|
|
|
* @return TableDumper Retruns a TableDumper of the table that was just added |
|
47
|
|
|
*/ |
|
48
|
17 |
|
public function addTable($table) |
|
49
|
|
|
{ |
|
50
|
17 |
|
if ($table instanceof Table) { |
|
51
|
2 |
|
$tableName = $table->getName(); |
|
52
|
17 |
|
} elseif (is_string($table)) { |
|
53
|
14 |
|
$tableName = $table; |
|
54
|
14 |
|
$table = new Table($tableName); |
|
55
|
14 |
|
} else { |
|
56
|
1 |
|
throw new \Exception("Invalid value supplied for argument 'table'", 1); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
//First check if a dumper already exists for this table |
|
60
|
16 |
|
if (!$this->offsetExists($tableName)) { |
|
61
|
|
|
//Create new one |
|
62
|
16 |
|
$this->offsetSet($tableName, new TableDumper($table)); |
|
63
|
16 |
|
} |
|
64
|
|
|
|
|
65
|
16 |
|
return $this->offsetGet($tableName); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param TableDumperCollection|array<TableDumper|Table|string> Adds a list of tables, either by passing TableDumperCollection, or an array containing either TableDumper objects, Table objects or table names |
|
71
|
|
|
* |
|
72
|
|
|
* @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added |
|
73
|
|
|
*/ |
|
74
|
9 |
|
public function addListTables($listTables) |
|
75
|
|
|
{ |
|
76
|
|
|
//If arg is a TableDumperCollection, merge into this one |
|
77
|
9 |
|
if ($listTables instanceof TableDumperCollection) { |
|
78
|
1 |
|
foreach ($listTables as $table) { |
|
79
|
1 |
|
$this->append($table); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $listTables; |
|
83
|
8 |
|
} elseif (is_array($listTables)) { |
|
84
|
|
|
//Create TableDumperCollection |
|
85
|
7 |
|
$listDumpers = new TableDumperCollection; |
|
86
|
|
|
|
|
87
|
7 |
|
foreach ($listTables as $table) { |
|
88
|
|
|
//If table is already a Dumper, simply append to this |
|
89
|
7 |
|
if ($table instanceof TableDumper) { |
|
90
|
2 |
|
$listDumpers[] = $table; |
|
91
|
2 |
|
$this->append($table); |
|
92
|
2 |
|
} else { |
|
93
|
5 |
|
$listDumpers[] = $this->addTable($table); |
|
94
|
|
|
} |
|
95
|
7 |
|
} |
|
96
|
|
|
|
|
97
|
7 |
|
return $listDumpers; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
throw new \Exception("Invalid value supplied for argument 'listTables'", 1); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
2 |
|
public function __call($name, $arguments) |
|
105
|
|
|
{ |
|
106
|
|
|
//Call methods on TableDumper values |
|
107
|
2 |
|
foreach ($this as $value) { |
|
108
|
2 |
|
call_user_func_array([$value, $name], $arguments); |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
} |