/
BeanExport.php
215 lines (190 loc) · 4.97 KB
/
BeanExport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Recursive Bean Export
*
* @file RedBean/Plugin/BeanExport.php
* @desc Plugin to export beans to arrays recursively
* @author Gabor de Mooij and the RedBeanPHP Community
* @license BSD/GPLv2
*
* (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class RedBean_Plugin_BeanExport implements RedBean_Plugin {
/**
* @var null|\RedBean_Toolbox
*/
protected $toolbox = null;
/**
* @var array
* Array used to check for recursion. This avoids infinite loops.
*/
protected $recurCheck = array();
/**
* @var array
* Recursion shield for types
*/
protected $recurTypeCheck = array();
/**
* @var boolean
* Whether to use a type shield for recursion
*/
protected $typeShield = false;
/**
* @var integer
* Current level of recursion depth
*/
protected $depth = 0;
/**
* @var integer
* Maximum level of recursions allowed by user
*/
protected $maxDepth = false;
/**
* Constructor
* @param RedBean_Toolbox $toolbox
*/
public function __construct( RedBean_Toolbox $toolbox ) {
$this->toolbox = $toolbox;
}
/**
* Loads Schema
* @return void
*/
public function loadSchema() {
$tables = array_flip($this->toolbox->getWriter()->getTables());
foreach($tables as $table=>$columns) {
try{
$tables[$table] = $this->toolbox->getWriter()->getColumns($table);
}
catch(RedBean_Exception_SQL $e) {
$tables[$table] = array();
}
}
$this->tables = $tables;
}
/**
*Returs a serialized representation of the schema
*
*@return string $serialized serialized representation
*/
public function getSchema() {
return serialize($this->tables);
}
/**
* Loads a schema from a string (containing serialized export of schema)
*
* @param string $schema
*/
public function loadSchemaFromString($schema) {
$this->tables = unserialize($schema);
}
/**
* Exports a collection of beans
*
* @param mixed $beans Either array or RedBean_OODBBean
* @param bool $resetRecur Whether we need to reset the recursion check array (first time only)
*
* @return array $export Exported beans
*/
public function export( $beans, $resetRecur=true ) {
if ($resetRecur) {
$this->recurCheck = array();
}
if (!is_array($beans)) {
$beans = array($beans);
}
if ($this->maxDepth!==false) {
$this->depth ++;
if ($this->depth > $this->maxDepth) {
$this->depth--;
return array();
}
}
if ($this->typeShield===true) {
if (is_array($beans) && count($beans)>0) {
$firstBean = reset($beans);
$type = $firstBean->getMeta('type');
if (isset($this->recurTypeCheck[$type])){
if ($this->maxDepth!==false) {
$this->depth --;
}
return array();
}
$this->recurTypeCheck[ $type ] = true;
}
}
$export = array();
foreach($beans as $bean) {
$export[$bean->getID()] = $this->exportBean( $bean );
}
if ($this->maxDepth!==false) {
$this->depth --;
}
return $export;
}
/**
* Exports beans, just like export() but with additional
* parameters for limitation on recursion and depth.
*
* @param array $beans beans to export
* @param boolean $typeShield whether to use a type recursion shield
* @param boolean|integer $depth maximum number of iterations allowed (boolean FALSE to turn off)
*/
public function exportLimited($beans, $typeShield = true, $depth = false) {
$this->depth = 0;
$this->maxDepth = $depth;
$this->typeShield = $typeShield;
$export = $this->export($beans);
$this->typeShield = false;
$this->maxDepth = false;
return $export;
}
/**
* Exports a single bean
*
* @param RedBean_OODBBean $bean Bean to be exported
*
* @return array|null $array Array export of bean
*/
public function exportBean(RedBean_OODBBean $bean) {
$bid = $bean->getMeta('type').'-'.$bean->getID();
if (isset($this->recurCheck[$bid])) return null;
$this->recurCheck[$bid]=$bid;
$export = $bean->export();
foreach($export as $key=>$value) {
if (strpos($key,'_id')!==false) {
$sub = str_replace('_id','',$key);
$subBean = $bean->$sub;
if ($subBean) {
$export[$sub] = $this->export($subBean, false);
}
}
}
$type = $bean->getMeta('type');
$linkField = $type . '_id';
//get all ownProperties
foreach($this->tables as $table=>$cols) {
if (strpos($table,'_')===false) {
if (in_array($linkField,array_keys($cols))) {
$field = 'own'.ucfirst($table);
$export[$field] = self::export($bean->$field, false);
}
}
}
//get all sharedProperties
foreach($this->tables as $table=>$cols) {
if (strpos($table,'_')!==false) {
$parts = explode('_', $table);
if (is_array($parts) && in_array($type,$parts)) {
$other = $parts[0];
if ($other==$type) $other=$parts[1];
$field = 'shared'.ucfirst($other);
$export[$field] = self::export($bean->$field, false);
}
}
}
return $export;
}
}