PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/RedBeanPHP/LabelMaker.php

https://github.com/gabordemooij/redbean
PHP | 182 lines | 53 code | 18 blank | 111 comment | 4 complexity | cf6a4c51d3cc181bd3f78ebfca8cab0c MD5 | raw file
  1. <?php
  2. namespace RedBeanPHP;
  3. use RedBeanPHP\ToolBox as ToolBox;
  4. use RedBeanPHP\OODBBean as OODBBean;
  5. /**
  6. * Label Maker.
  7. * Makes so-called label beans.
  8. * A label is a bean with only an id, type and name property.
  9. * Labels can be used to create simple entities like categories, tags or enums.
  10. * This service class provides convenience methods to deal with this kind of
  11. * beans.
  12. *
  13. * @file RedBeanPHP/LabelMaker.php
  14. * @author Gabor de Mooij and the RedBeanPHP Community
  15. * @license BSD/GPLv2
  16. *
  17. * @copyright
  18. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
  19. * This source file is subject to the BSD/GPLv2 License that is bundled
  20. * with this source code in the file license.txt.
  21. */
  22. class LabelMaker
  23. {
  24. /**
  25. * @var ToolBox
  26. */
  27. protected $toolbox;
  28. /**
  29. * Constructor.
  30. *
  31. * @param ToolBox $toolbox
  32. */
  33. public function __construct( ToolBox $toolbox )
  34. {
  35. $this->toolbox = $toolbox;
  36. }
  37. /**
  38. * A label is a bean with only an id, type and name property.
  39. * This function will dispense beans for all entries in the array. The
  40. * values of the array will be assigned to the name property of each
  41. * individual bean.
  42. *
  43. * <code>
  44. * $people = R::dispenseLabels( 'person', [ 'Santa', 'Claus' ] );
  45. * </code>
  46. *
  47. * @param string $type type of beans you would like to have
  48. * @param array $labels list of labels, names for each bean
  49. *
  50. * @return array
  51. */
  52. public function dispenseLabels( $type, $labels )
  53. {
  54. $labelBeans = array();
  55. foreach ( $labels as $label ) {
  56. $labelBean = $this->toolbox->getRedBean()->dispense( $type );
  57. $labelBean->name = $label;
  58. $labelBeans[] = $labelBean;
  59. }
  60. return $labelBeans;
  61. }
  62. /**
  63. * Gathers labels from beans. This function loops through the beans,
  64. * collects the value of the name property for each individual bean
  65. * and stores the names in a new array. The array then gets sorted using the
  66. * default sort function of PHP (sort).
  67. *
  68. * Usage:
  69. *
  70. * <code>
  71. * $o1->name = 'hamburger';
  72. * $o2->name = 'pizza';
  73. * implode( ',', R::gatherLabels( [ $o1, $o2 ] ) ); //hamburger,pizza
  74. * </code>
  75. *
  76. * Note that the return value is an array of strings, not beans.
  77. *
  78. * @param array $beans list of beans to loop through
  79. *
  80. * @return array
  81. */
  82. public function gatherLabels( $beans )
  83. {
  84. $labels = array();
  85. foreach ( $beans as $bean ) {
  86. $labels[] = $bean->name;
  87. }
  88. sort( $labels );
  89. return $labels;
  90. }
  91. /**
  92. * Fetches an ENUM from the database and creates it if necessary.
  93. * An ENUM has the following format:
  94. *
  95. * <code>
  96. * ENUM:VALUE
  97. * </code>
  98. *
  99. * If you pass 'ENUM' only, this method will return an array of its
  100. * values:
  101. *
  102. * <code>
  103. * implode( ',', R::gatherLabels( R::enum( 'flavour' ) ) ) //'BANANA,MOCCA'
  104. * </code>
  105. *
  106. * If you pass 'ENUM:VALUE' this method will return the specified enum bean
  107. * and create it in the database if it does not exist yet:
  108. *
  109. * <code>
  110. * $bananaFlavour = R::enum( 'flavour:banana' );
  111. * $bananaFlavour->name;
  112. * </code>
  113. *
  114. * So you can use this method to set an ENUM value in a bean:
  115. *
  116. * <code>
  117. * $shake->flavour = R::enum( 'flavour:banana' );
  118. * </code>
  119. *
  120. * the property flavour now contains the enum bean, a parent bean.
  121. * In the database, flavour_id will point to the flavour record with name 'banana'.
  122. *
  123. * @param string $enum ENUM specification for label
  124. *
  125. * @return array|OODBBean
  126. */
  127. public function enum( $enum )
  128. {
  129. $oodb = $this->toolbox->getRedBean();
  130. if ( strpos( $enum, ':' ) === FALSE ) {
  131. $type = $enum;
  132. $value = FALSE;
  133. } else {
  134. list( $type, $value ) = explode( ':', $enum );
  135. $value = preg_replace( '/\W+/', '_', strtoupper( trim( $value ) ) );
  136. }
  137. /**
  138. * We use simply find here, we could use inspect() in fluid mode etc,
  139. * but this would be useless. At first sight it looks clean, you could even
  140. * bake this into find(), however, find not only has to deal with the primary
  141. * search type, people can also include references in the SQL part, so avoiding
  142. * find failures does not matter, this is still the quickest way making use
  143. * of existing functionality.
  144. *
  145. * @note There seems to be a bug in XDebug v2.3.2 causing suppressed
  146. * exceptions like these to surface anyway, to prevent this use:
  147. *
  148. * "xdebug.default_enable = 0"
  149. *
  150. * Also see Github Issue #464
  151. */
  152. $values = $oodb->find( $type );
  153. if ( $value === FALSE ) {
  154. return $values;
  155. }
  156. foreach( $values as $enumItem ) {
  157. if ( $enumItem->name === $value ) return $enumItem;
  158. }
  159. $newEnumItems = $this->dispenseLabels( $type, array( $value ) );
  160. $newEnumItem = reset( $newEnumItems );
  161. $oodb->store( $newEnumItem );
  162. return $newEnumItem;
  163. }
  164. }