Skip to content

Commit df07e0e

Browse files
committed
Add trimport().
1 parent eb50b47 commit df07e0e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

RedBeanPHP/OODBBean.php

+15
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ public function import( $array, $selection = FALSE, $notrim = FALSE )
547547
return $this;
548548
}
549549

550+
/**
551+
* Same as import() but trims all values by default.
552+
* Set the second parameter to apply a different function.
553+
*
554+
* @param array $array what you want to import
555+
* @param string $function function to apply (default is trim)
556+
* @param string|array $selection selection of values
557+
* @param boolean $notrim if TRUE selection keys will NOT be trimmed
558+
*
559+
* @return OODBBean
560+
*/
561+
public function trimport( $array, $function='trim', $selection = FALSE, $notrim = FALSE ) {
562+
return $this->import( array_map( $function, $array ), $selection, $notrim );
563+
}
564+
550565
/**
551566
* Imports an associative array directly into the
552567
* internal property array of the bean as well as the

testing/RedUNIT/Base/Bean.php

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@
3030
*/
3131
class Bean extends Base
3232
{
33+
/**
34+
* Test whether we can use trimport().
35+
*/
36+
public function testTrimport()
37+
{
38+
$array = ['greeting'=>' hello ', 'to'=>' world '];
39+
$bean = R::dispense('greeting');
40+
$bean->trimport($array);
41+
asrt($bean->greeting,'hello');
42+
asrt($bean->to,'world');
43+
$bean->trimport($bean->export(), 'strtoupper');
44+
asrt($bean->greeting,'HELLO');
45+
asrt($bean->to,'WORLD');
46+
}
47+
3348
/**
3449
* Test whether we can use findFromSQL to extract beans
3550
* from the result of an SQL query.

0 commit comments

Comments
 (0)