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
|
10 |
|
public function append($value) |
18
|
|
|
{ |
19
|
|
|
//Make sure we're adding a TableDumper object |
20
|
10 |
|
if (!($value instanceof TableDumper)) { |
21
|
2 |
|
throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
//Append with table_name as key |
25
|
8 |
|
return $this->offsetSet($value->getTable()->getName(), $value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
*/ |
31
|
44 |
|
public function offsetSet($index, $newval) |
32
|
|
|
{ |
33
|
|
|
//Make sure we're adding a TableDumper object |
34
|
44 |
|
if (!($newval instanceof TableDumper)) { |
35
|
2 |
|
throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
//Append with table_name as key |
39
|
42 |
|
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
|
34 |
|
public function addTable($table) |
49
|
|
|
{ |
50
|
34 |
|
if ($table instanceof Table) { |
51
|
4 |
|
$tableName = $table->getName(); |
52
|
34 |
|
} elseif (is_string($table)) { |
53
|
28 |
|
$tableName = $table; |
54
|
28 |
|
$table = new Table($tableName); |
55
|
28 |
|
} else { |
56
|
2 |
|
throw new \Exception("Invalid value supplied for argument 'table'", 1); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
//First check if a dumper already exists for this table |
60
|
32 |
|
if (!$this->offsetExists($tableName)) { |
61
|
|
|
//Create new one |
62
|
32 |
|
$this->offsetSet($tableName, new TableDumper($table)); |
63
|
32 |
|
} |
64
|
|
|
|
65
|
32 |
|
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
|
18 |
|
public function addListTables($listTables) |
75
|
|
|
{ |
76
|
|
|
//If arg is a TableDumperCollection, merge into this one |
77
|
18 |
|
if ($listTables instanceof TableDumperCollection) { |
78
|
2 |
|
foreach ($listTables as $table) { |
79
|
2 |
|
$this->append($table); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
2 |
|
return $listTables; |
83
|
16 |
|
} elseif (is_array($listTables)) { |
84
|
|
|
//Create TableDumperCollection |
85
|
14 |
|
$listDumpers = new TableDumperCollection; |
86
|
|
|
|
87
|
14 |
|
foreach ($listTables as $table) { |
88
|
|
|
//If table is already a Dumper, simply append to this |
89
|
14 |
|
if ($table instanceof TableDumper) { |
90
|
4 |
|
$listDumpers[] = $table; |
91
|
4 |
|
$this->append($table); |
92
|
4 |
|
} else { |
93
|
10 |
|
$listDumpers[] = $this->addTable($table); |
94
|
|
|
} |
95
|
14 |
|
} |
96
|
|
|
|
97
|
14 |
|
return $listDumpers; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
throw new \Exception("Invalid value supplied for argument 'listTables'", 1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
4 |
|
public function __call($name, $arguments) |
105
|
|
|
{ |
106
|
|
|
//Call methods on TableDumper values |
107
|
4 |
|
foreach ($this as $value) { |
108
|
4 |
|
call_user_func_array([$value, $name], $arguments); |
109
|
4 |
|
} |
110
|
|
|
|
111
|
4 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |