forked from gabordemooij/redbean
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cooker.php
executable file
·125 lines (121 loc) · 3.72 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
<?php
/**
* RedBean Cooker
* @file RedBean/Cooker.php
* @description Turns arrays into bean collections for easy persistence.
* @author Gabor de Mooij
* @license BSD
*
* The Cooker is a little candy to make it easier to read-in an HTML form.
* This class turns a form into a collection of beans plus an array
* describing the desired associations.
*
* copyright (c) G.J.G.T. (Gabor) de Mooij
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class RedBean_Cooker {
/**
* Sets the toolbox to be used by graph()
*
* @param RedBean_Toolbox $toolbox toolbox
* @return void
*/
public function setToolbox(RedBean_Toolbox $toolbox) {
$this->toolbox = $toolbox;
$this->redbean = $this->toolbox->getRedbean();
}
/**
* 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 $beans beans
*/
public function graph( $array, $filterEmpty = false ) {
if(is_array($array) && isset($array['type']) && is_array(@$array['id'])){ // allowing multi-select and multi-checkbox by restructuring the array.
foreach($array['id'] as $key=>$value){
foreach($array as $kk=>$vv){
if(is_array($vv)) {
if(isset($vv[$key])) $new[$key][$kk] = $vv[$key];
}
else $new[$key][$kk] = $vv;
}
}
$array = $new;
unset($new);
}
$beans = array();
if (is_array($array) && isset($array['type'])) {
$type = $array['type'];
unset($array['type']);
//Do we need to load the bean?
if (isset($array['id'])) {
$id = (int) $array['id'];
$bean = $this->redbean->load($type,$id);
}
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;
}
}
return $bean;
}
elseif (is_array($array)) {
foreach($array as $key=>$value) {
$listBean = $this->graph($value,$filterEmpty);
if (!($listBean instanceof RedBean_OODBBean)) {
throw new RedBean_Exception_Security('Expected bean but got :'.gettype($listBean));
}
if ($listBean->isEmpty()) {
if (!$filterEmpty) {
$beans[$key] = $listBean;
}
}
else {
$beans[$key] = $listBean;
}
}
return $beans;
}
else {
throw new RedBean_Exception_Security('Expected array but got :'.gettype($array));
}
}
}