5 use RedBeanPHP\ToolBox as ToolBox;
6 use RedBeanPHP\OODBBean as OODBBean;
10 * Makes so-called label beans.
11 * A label is a bean with only an id, type and name property.
12 * Labels can be used to create simple entities like categories, tags or enums.
13 * This service class provides convenience methods to deal with this kind of
16 * @file RedBeanPHP/LabelMaker.php
17 * @author Gabor de Mooij and the RedBeanPHP Community
21 * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
22 * This source file is subject to the BSD/GPLv2 License that is bundled
23 * with this source code in the file license.txt.
35 * @param ToolBox $toolbox
37 public function __construct( ToolBox $toolbox )
39 $this->toolbox = $toolbox;
43 * A label is a bean with only an id, type and name property.
44 * This function will dispense beans for all entries in the array. The
45 * values of the array will be assigned to the name property of each
49 * $people = R::dispenseLabels( 'person', [ 'Santa', 'Claus' ] );
52 * @param string $type type of beans you would like to have
53 * @param array $labels list of labels, names for each bean
57 public function dispenseLabels( $type, $labels )
59 $labelBeans = array();
60 foreach ( $labels as $label ) {
61 $labelBean = $this->toolbox->getRedBean()->dispense( $type );
62 $labelBean->name = $label;
63 $labelBeans[] = $labelBean;
70 * Gathers labels from beans. This function loops through the beans,
71 * collects the value of the name property for each individual bean
72 * and stores the names in a new array. The array then gets sorted using the
73 * default sort function of PHP (sort).
78 * $o1->name = 'hamburger';
79 * $o2->name = 'pizza';
80 * implode( ',', R::gatherLabels( [ $o1, $o2 ] ) ); //hamburger,pizza
83 * Note that the return value is an array of strings, not beans.
85 * @param array $beans list of beans to loop through
89 public function gatherLabels( $beans )
93 foreach ( $beans as $bean ) {
94 $labels[] = $bean->name;
103 * Fetches an ENUM from the database and creates it if necessary.
104 * An ENUM has the following format:
110 * If you pass 'ENUM' only, this method will return an array of its
114 * implode( ',', R::gatherLabels( R::enum( 'flavour' ) ) ) //'BANANA,MOCCA'
117 * If you pass 'ENUM:VALUE' this method will return the specified enum bean
118 * and create it in the database if it does not exist yet:
121 * $bananaFlavour = R::enum( 'flavour:banana' );
122 * $bananaFlavour->name;
125 * So you can use this method to set an ENUM value in a bean:
128 * $shake->flavour = R::enum( 'flavour:banana' );
131 * the property flavour now contains the enum bean, a parent bean.
132 * In the database, flavour_id will point to the flavour record with name 'banana'.
134 * @param string $enum ENUM specification for label
136 * @return array|OODBBean
138 public function enum( $enum )
140 $oodb = $this->toolbox->getRedBean();
142 if ( strpos( $enum, ':' ) === FALSE ) {
146 list( $type, $value ) = explode( ':', $enum );
147 $value = preg_replace( '/\W+/', '_', strtoupper( trim( $value ) ) );
151 * We use simply find here, we could use inspect() in fluid mode etc,
152 * but this would be useless. At first sight it looks clean, you could even
153 * bake this into find(), however, find not only has to deal with the primary
154 * search type, people can also include references in the SQL part, so avoiding
155 * find failures does not matter, this is still the quickest way making use
156 * of existing functionality.
158 * @note There seems to be a bug in XDebug v2.3.2 causing suppressed
159 * exceptions like these to surface anyway, to prevent this use:
161 * "xdebug.default_enable = 0"
163 * Also see Github Issue #464
165 $values = $oodb->find( $type );
167 if ( $value === FALSE ) {
171 foreach( $values as $enumItem ) {
172 if ( $enumItem->name === $value ) return $enumItem;
175 $newEnumItems = $this->dispenseLabels( $type, array( $value ) );
176 $newEnumItem = reset( $newEnumItems );
178 $oodb->store( $newEnumItem );