* $book = R::dispense( 'book' ); * $book->title = 'My Book'; * R::store( $book ); * * * This method can also be used to create an entire bean graph at once. * Given an array with keys specifying the property names of the beans * and a special _type key to indicate the type of bean, one can * make the Dispense Helper generate an entire hierarchy of beans, including * lists. To make dispense() generate a list, simply add a key like: * ownXList or sharedXList where X is the type of beans it contains and * a set its value to an array filled with arrays representing the beans. * Note that, although the type may have been hinted at in the list name, * you still have to specify a _type key for every bean array in the list. * Note that, if you specify an array to generate a bean graph, the number * parameter will be ignored. * * Usage: * * * $book = R::dispense( [ * '_type' => 'book', * 'title' => 'Gifted Programmers', * 'author' => [ '_type' => 'author', 'name' => 'Xavier' ], * 'ownPageList' => [ ['_type'=>'page', 'text' => '...'] ] * ] ); * * * @param string|array $typeOrBeanArray type or bean array to import * @param integer $num number of beans to dispense * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * * @return array|OODBBean */ public static function dispense( OODB $oodb, $typeOrBeanArray, $num = 1, $alwaysReturnArray = FALSE ) { if ( is_array($typeOrBeanArray) ) { if ( !isset( $typeOrBeanArray['_type'] ) ) { $list = array(); foreach( $typeOrBeanArray as $beanArray ) { if ( !( is_array( $beanArray ) && isset( $beanArray['_type'] ) ) ) { throw new RedException( 'Invalid Array Bean' ); } } foreach( $typeOrBeanArray as $beanArray ) $list[] = self::dispense( $oodb, $beanArray ); return $list; } $import = $typeOrBeanArray; $type = $import['_type']; unset( $import['_type'] ); } else { $type = $typeOrBeanArray; } if (self::$enforceNamingPolicy) self::checkType( $type ); $beanOrBeans = $oodb->dispense( $type, $num, $alwaysReturnArray ); if ( isset( $import ) ) { $beanOrBeans->import( $import ); } return $beanOrBeans; } /** * Takes a comma separated list of bean types * and dispenses these beans. For each type in the list * you can specify the number of beans to be dispensed. * * Usage: * * * list( $book, $page, $text ) = R::dispenseAll( 'book,page,text' ); * * * This will dispense a book, a page and a text. This way you can * quickly dispense beans of various types in just one line of code. * * Usage: * * * list($book, $pages) = R::dispenseAll('book,page*100'); * * * This returns an array with a book bean and then another array * containing 100 page beans. * * @param OODB $oodb OODB * @param string $order a description of the desired dispense order using the syntax above * @param boolean $onlyArrays return only arrays even if amount < 2 * * @return array */ public static function dispenseAll( OODB $oodb, $order, $onlyArrays = FALSE ) { $list = array(); foreach( explode( ',', $order ) as $order ) { if ( strpos( $order, '*' ) !== FALSE ) { list( $type, $amount ) = explode( '*', $order ); } else { $type = $order; $amount = 1; } $list[] = self::dispense( $oodb, $type, $amount, $onlyArrays ); } return $list; } }