-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCooker.php
219 lines (200 loc) · 5.47 KB
/
Cooker.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
216
217
218
219
<?php
namespace RedBeanPHP\Plugin;
use \RedBeanPHP\AssociationManager as AssociationManager;
use \RedBeanPHP\OODB as OODB;
use \RedBeanPHP\OODBBean as OODBBean;
use \RedBeanPHP\RedException as RedException;
use \RedBeanPHP\Facade as R;
use \RedBeanPHP\ToolBox as ToolBox;
/**
* RedBean Cooker
*
* @file RedBean/Cooker.php
* @desc Turns arrays into bean collections for easy persistence.
* @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 Cooker
{
/**
* @var boolean
*/
private static $loadBeans = FALSE;
/**
* @var boolean
*/
private static $useNULLForEmptyString = FALSE;
/**
* @var RedBean_Toolbox
*/
private $toolbox;
/**
* @var RedBean_OODB
*/
private $redbean;
/**
* If you enable bean loading graph will load beans if there is an ID in the array.
* This is very powerful but can also cause security issues if a user knows how to
* manipulate beans and there is no model based ID validation.
*
* @param boolean $yesNo
*
* @return void
*/
public static function enableBeanLoading( $yesNo )
{
self::$loadBeans = ( $yesNo );
}
/**
* Static version of setUseNullFlag.
*
* @param boolean $yesNo
*
* @return void
*/
public static function setUseNullFlagSt( $yesNo )
{
self::$useNULLForEmptyString = (boolean) $yesNo;
}
/**
* Sets the toolbox to be used by graph()
*
* @param RedBean_Toolbox $toolbox toolbox
*
* @return void
*/
public function setToolbox( Toolbox $toolbox )
{
$this->toolbox = $toolbox;
$this->redbean = $this->toolbox->getRedbean();
}
/**
* Loads bean, recurses if one of the property appears to be a list.
*
* @param array $array data array to import as a bean
* @param boolean $filterEmpty if TRUE empty STRING values are converted to NULL (default FALSE)
*
* @return RedBean_OODBBean
*
* @throws RedBean_Exception_Security
*/
private function loadBean( &$array, $filterEmpty )
{
$type = $array['type'];
unset( $array['type'] );
if ( isset( $array['id'] ) ) { // Do we need to load the bean?
if ( self::$loadBeans ) {
$bean = $this->redbean->load( $type, (int) $array['id'] );
} else {
throw new RedException( 'Attempt to load a bean in Cooker. Use enableBeanLoading to override but please read security notices first.' );
}
} else {
$bean = $this->redbean->dispense( $type );
}
foreach ( $array as $property => $value ) {
if ( is_array( $value ) ) {
$bean->$property = $this->graph( $value, $filterEmpty );
} else {
$bean->$property = ( $value == '' && self::$useNULLForEmptyString ) ? NULL : $value;
}
}
return $bean;
}
/**
* Loads a list. Recurses for every bean in the list.
*
* @param array $array data array to import as a list
* @param boolean $filterEmpty if TRUE empty lists will NOT be imported
*
* @return array
*
* @throws RedBean_Exception_Security
*/
private function loadList( &$array, $filterEmpty )
{
$beans = array();
foreach ( $array as $key => $value ) {
$listBean = $this->graph( $value, $filterEmpty );
if ( !( $listBean instanceof OODBBean ) ) {
throw new RedException( 'Expected bean but got :' . gettype( $listBean ) );
}
if ( $listBean->isEmpty() ) {
if ( !$filterEmpty ) {
$beans[$key] = $listBean;
}
} else {
$beans[$key] = $listBean;
}
}
return $beans;
}
/**
* Turns an array (post/request array) into a collection of beans.
* Handy for turning forms into bean structures that can be stored with a
* single call.
*
* Typical usage:
*
* $struct = R::graph($_POST);
* R::store($struct);
*
* Example of a valid array:
*
* $form = array(
* 'type' => 'order',
* 'ownProduct' => array(
* array('id' => 171, 'type' => 'product'),
* ),
* 'ownCustomer' => array(
* array('type' => 'customer', 'name' => 'Bill')
* ),
* 'sharedCoupon' => array(
* array('type' => 'coupon', 'name' => '123'),
* array('type' => 'coupon', 'id' => 3)
* )
* );
*
* Each entry in the array will become a property of the bean.
* The array needs to have a type-field indicating the type of bean it is
* going to be. The array can have nested arrays. A nested array has to be
* named conform the bean-relation conventions, i.e. ownPage/sharedPage
* each entry in the nested array represents another bean.
*
* @param array $array array to be turned into a bean collection
* @param boolean $filterEmpty whether you want to exclude empty beans
*
* @return array
*
* @throws RedBean_Exception_Security
*/
public function graph( $array, $filterEmpty = FALSE )
{
if ( is_array( $array ) && isset( $array['type'] ) ) {
return $this->loadBean( $array, $filterEmpty );
} elseif ( is_array( $array ) ) {
return $this->loadList( $array, $filterEmpty );
} else {
throw new RedException( 'Expected array but got :' . gettype( $array ) );
}
}
/**
* Toggles the use-NULL flag.
*
* @param boolean $yesNo
*
* @return void
*/
public function setUseNullFlag( $yesNo )
{
self::$useNULLForEmptyString = (bool) $yesNo;
}
}
R::ext('graph', function($array, $fe = FALSE){
$cooker = new Cooker;
$cooker->setToolbox(R::getToolBox());
return $cooker->graph($array, $fe);
});