Permalink
Browse files

Added LabelMaker, harmonize APIs.

  • Loading branch information...
1 parent 0ddc7f2 commit df08add4ac398435c222e15be4f472b1017170d6 Gabor de Mooij committed Nov 18, 2012
Showing with 82 additions and 11 deletions.
  1. +10 −11 RedBean/Facade.php
  2. +70 −0 RedBean/LabelMaker.php
  3. +1 −0 RedBean/redbean.inc.php
  4. +1 −0 replica.xml
View
@@ -76,6 +76,13 @@ class RedBean_Facade {
public static $duplicationManager;
/**
+ * Holds the Label Maker instance.
+ * This facility allows you to make label beans.
+ * @var RedBean_LabelMaker
+ */
+ public static $labelMaker;
+
+ /**
* Holds the Key of the current database.
* @var string
*/
@@ -868,6 +875,7 @@ public static function configureFacadeWithToolbox( RedBean_ToolBox $tb ) {
self::$redbean = self::$toolbox->getRedBean();
self::$associationManager = new RedBean_AssociationManager( self::$toolbox );
self::$redbean->setAssociationManager(self::$associationManager);
+ self::$labelMaker = new RedBean_LabelMaker(self::$toolbox);
self::$extAssocManager = new RedBean_AssociationManager_ExtAssociationManager( self::$toolbox );
$helper = new RedBean_ModelHelper();
$helper->attachEventListeners(self::$redbean);
@@ -1017,13 +1025,7 @@ public static function trashAll($beans) {
* @return array $bean a list of beans with type and name property
*/
public static function dispenseLabels($type,$labels) {
- $labelBeans = array();
- foreach($labels as $label) {
- $labelBean = self::dispense($type);
- $labelBean->name = $label;
- $labelBeans[] = $labelBean;
- }
- return $labelBeans;
+ return self::$labelMaker->dispenseLabels($type,$labels);
}
/**
@@ -1037,10 +1039,7 @@ public static function dispenseLabels($type,$labels) {
* @return array $array list of names of beans
*/
public static function gatherLabels($beans) {
- $labels = array();
- foreach($beans as $bean) $labels[] = $bean->name;
- sort($labels);
- return $labels;
+ return self::$labelMaker->gatherLabels($beans);
}
View
@@ -0,0 +1,70 @@
+<?php
+/**
+ * RedBean Label Maker
+ *
+ * @file RedBean/LabelMaker.php
+ * @desc Makes so-called label beans
+ * @author Gabor de Mooij and the RedBeanPHP Community
+ * @license BSD/GPLv2
+ *
+ * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
+ * This source file is subject to the BSD/GPLv2 License that is bundled
+ * with this source code in the file license.txt.
+ *
+ */
+class RedBean_LabelMaker {
+
+ /**
+ * Holds the toolbox for this instance.
+ * @var RedBean_Toolbox
+ */
+ protected $toolbox;
+
+ /**
+ * Constructor.
+ * The label maker requires a toolbox to function well.
+ *
+ * @param RedBean_ToolBox $toolbox
+ */
+ public function __construct(RedBean_ToolBox $toolbox) {
+ $this->toolbox = $toolbox;
+ }
+
+ /**
+ * 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.
+ *
+ * @param string $type type of beans you would like to have
+ * @param array $labels list of labels, names for each bean
+ *
+ * @return array $bean a list of beans with type and name property
+ */
+ public function dispenseLabels($type,$labels) {
+ $labelBeans = array();
+ foreach($labels as $label) {
+ $labelBean = $this->toolbox->getRedBean()->dispense($type);
+ $labelBean->name = $label;
+ $labelBeans[] = $labelBean;
+ }
+ return $labelBeans;
+ }
+
+ /**
+ * Gathers labels from beans. This function loops through the beans,
+ * collects the values of the name properties of each individual bean
+ * and stores the names in a new array. The array then gets sorted using the
+ * default sort function of PHP (sort).
+ *
+ * @param array $beans list of beans to loop
+ *
+ * @return array $array list of names of beans
+ */
+ public function gatherLabels($beans) {
+ $labels = array();
+ foreach($beans as $bean) $labels[] = $bean->name;
+ sort($labels);
+ return $labels;
+ }
+}
View
@@ -84,6 +84,7 @@
require($dir.'SimpleModel.php');
require($dir.'ModelHelper.php');
require($dir.'TagManager.php');
+require($dir.'LabelMaker.php');
require($dir.'Facade.php');;
require($dir.'SQLHelper.php');
View
@@ -43,6 +43,7 @@
<item type="php">RedBean/ModelHelper.php</item>
<item type="php">RedBean/SQLHelper.php</item>
<item type="php">RedBean/TagManager.php</item>
+ <item type="php">RedBean/LabelMaker.php</item>
<item type="php">RedBean/Facade.php</item>
<item type="php">RedBean/Plugin.php</item>
<item type="php">RedBean/Plugin/Sync.php</item>

0 comments on commit df08add

Please sign in to comment.