Skip to content

Commit

Permalink
empty bean filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Dec 14, 2011
1 parent c146da7 commit 0eb0876
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 15 deletions.
17 changes: 13 additions & 4 deletions RedBean/Cooker.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public function setToolbox(RedBean_Toolbox $toolbox) {
* 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 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 ) {
public function graph( $array, $filterEmpty = false ) {
$beans = array();
if (is_array($array) && isset($array['type'])) {
$type = $array['type'];
Expand All @@ -78,7 +79,7 @@ public function graph( $array ) {
}
foreach($array as $property=>$value) {
if (is_array($value)) {
$bean->$property = $this->graph($value);
$bean->$property = $this->graph($value,$filterEmpty);
}
else {
$bean->$property = $value;
Expand All @@ -88,7 +89,15 @@ public function graph( $array ) {
}
elseif (is_array($array)) {
foreach($array as $key=>$value) {
$beans[$key] = $this->graph($value);
$listBean = $this->graph($value,$filterEmpty);
if ($listBean->isEmpty()) {
if (!$filterEmpty) {
$beans[$key] = $listBean;
}
}
else {
$beans[$key] = $listBean;
}
}
return $beans;
}
Expand Down
7 changes: 4 additions & 3 deletions RedBean/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,14 +868,15 @@ public static function configureFacadeWithToolbox( RedBean_ToolBox $tb ) {
/**
* facade method for Cooker Graph.
*
* @param array $array array containing POST/GET fields or other data
* @param array $array array containing POST/GET fields or other data
* @param boolean $filterEmptyBeans whether you want to exclude empty beans
*
* @return array $arrayOfBeans Beans
*/
public static function graph($array) {
public static function graph($array,$filterEmpty=false) {
$cooker = new RedBean_Cooker();
$cooker->setToolbox(self::$toolbox);
return $cooker->graph($array);
return $cooker->graph($array,$filterEmpty);
}


Expand Down
18 changes: 18 additions & 0 deletions RedBean/OODBBean.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,24 @@ public function count() {
return count($this->properties);
}

/**
* Checks wether a bean is empty or not.
* A bean is empty if it has no other properties than the id field OR
* if all the other property are empty().
*
* @return boolean
*/
public function isEmpty() {
$empty = true;
foreach($this->properties as $key=>$value) {
if ($key=='id') continue;
if (!empty($value)) {
$empty = false;

}
}
return $empty;
}
}


35 changes: 29 additions & 6 deletions testing/RedUNIT/Base/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ public function run() {
asrt(R::count('page'),3);
asrt(R::count('spaceship'),0);

//same, but now with intermediate save, counts must be same
R::nuke();
$document = R::dispense('document');
$page = R::dispense('page');
$document->title = 'test';
$page->content = 'lorem ipsum';
$user = R::dispense('user');
$user->name = 'Leo';
$document->sharedUser[] = $user;
$document->ownPage[] = $page;
$document->starship_id = 3;
$document->planet = R::dispense('planet');
R::store($document);
$duplicate = R::dup($document);
R::store($document);
R::store($duplicate);
R::store($document);
$duplicate = R::dup($document);
R::store($document);
R::store($duplicate);
asrt(R::count('planet'),1);
asrt(R::count('user'),1);
asrt(R::count('document'),3);
asrt(R::count('page'),3);
asrt(R::count('spaceship'),0);

//test recursion
R::nuke();
list($d1,$d2) = R::dispense('document',2);
Expand Down Expand Up @@ -163,12 +189,9 @@ public function run() {
R::count('shared_prop',1);
$arthur = R::findOne('guest',' '.R::$writer->safeColumn('name').' = ? ',array('Arthur Dent'));
asrt($arthur->name,'Arthur Dent');






}

}
}


36 changes: 34 additions & 2 deletions testing/RedUNIT/Base/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public function run() {
}


//save a form using graph and ignore empty beans
R::nuke();
$product = R::dispense('product');
$product->name = 'shampoo';
Expand All @@ -315,14 +316,15 @@ public function run() {
array('id'=>$productID,'type'=>'product'),
),
'ownCustomer'=>array(
array('type'=>'customer','name'=>'Bill')
array('type'=>'customer','name'=>'Bill'),
array('type'=>'customer','name'=>'') //this one should be ignored
),
'sharedCoupon'=>array(
array('type'=>'coupon','name'=>'123'),
array('type'=>'coupon','id'=>$couponID)
)
);
$order = R::graph($form);
$order = R::graph($form, true);
asrt($order->getMeta('type'),'order');
asrt(count($order->ownProduct),1);
asrt(count($order->ownCustomer),1);
Expand All @@ -332,6 +334,36 @@ public function run() {
asrt(end($order->ownCustomer)->name,'Bill');
asrt($order->sharedCoupon[$couponID]->name,'567');

//without ignore empty beans
R::nuke();
$product = R::dispense('product');
$product->name = 'shampoo';
$productID = R::store($product);
$coupon = R::dispense('coupon');
$coupon->name = '567';
$couponID = R::store($coupon);

$form = array(
'type'=>'order',
'ownProduct'=>array(
array('id'=>$productID,'type'=>'product'),
),
'ownCustomer'=>array(
array('type'=>'customer','name'=>'Bill'),
array('type'=>'customer','name'=>'') //this one should be ignored
),
'sharedCoupon'=>array(
array('type'=>'coupon','name'=>'123'),
array('type'=>'coupon','id'=>$couponID)
)
);
$order = R::graph($form, true);
asrt($order->getMeta('type'),'order');
asrt(count($order->ownProduct),1);
asrt(count($order->ownCustomer),2);
asrt(count($order->sharedCoupon),2);
asrt(end($order->ownProduct)->id,$productID);


//make sure zeros are preserved
$form = array('type'=>'laptop','price'=>0);
Expand Down

0 comments on commit 0eb0876

Please sign in to comment.