\RedBeanPHPLabelMaker

Label Maker.

Makes so-called label beans. A label is a bean with only an id, type and name property. Labels can be used to create simple entities like categories, tags or enums. This service class provides convenience methods to deal with this kind of beans.

Summary

Methods
Properties
Constants
__construct()
dispenseLabels()
gatherLabels()
enum()
No public properties found
No constants found
No protected methods found
$toolbox
N/A
No private methods found
No private properties found
N/A

Properties

Methods

__construct()

__construct(\RedBeanPHP\ToolBox  $toolbox) 

Constructor.

Parameters

\RedBeanPHP\ToolBox $toolbox

dispenseLabels()

dispenseLabels(string  $type, array  $labels) : array

A label is a bean with only an id, type and name property.

This function will dispense beans for all entries in the array. The values of the array will be assigned to the name property of each individual bean.

$people = R::dispenseLabels( 'person', [ 'Santa', 'Claus' ] );

Parameters

string $type

type of beans you would like to have

array $labels

list of labels, names for each bean

Returns

array

gatherLabels()

gatherLabels(array  $beans) : array

Gathers labels from beans. This function loops through the beans, collects the value of the name property for each individual bean and stores the names in a new array. The array then gets sorted using the default sort function of PHP (sort).

Usage:

$o1->name = 'hamburger'; $o2->name = 'pizza'; implode( ',', R::gatherLabels( [ $o1, $o2 ] ) ); //hamburger,pizza

Note that the return value is an array of strings, not beans.

Parameters

array $beans

list of beans to loop through

Returns

array

enum()

enum(string  $enum) : array|\RedBeanPHP\OODBBean

Fetches an ENUM from the database and creates it if necessary.

An ENUM has the following format:

ENUM:VALUE

If you pass 'ENUM' only, this method will return an array of its values:

implode( ',', R::gatherLabels( R::enum( 'flavour' ) ) ) //'BANANA,MOCCA'

If you pass 'ENUM:VALUE' this method will return the specified enum bean and create it in the database if it does not exist yet:

$bananaFlavour = R::enum( 'flavour:banana' ); $bananaFlavour->name;

So you can use this method to set an ENUM value in a bean:

$shake->flavour = R::enum( 'flavour:banana' );

the property flavour now contains the enum bean, a parent bean. In the database, flavour_id will point to the flavour record with name 'banana'.

Parameters

string $enum

ENUM specification for label

Returns

array|\RedBeanPHP\OODBBean