d054307a37d8637abaadc988805ecfb795f186d4
[yaffs-website] / vendor / gabordemooij / redbean / RedBeanPHP / BeanHelper / SimpleFacadeBeanHelper.php
1 <?php
2
3 namespace RedBeanPHP\BeanHelper;
4
5 use RedBeanPHP\BeanHelper as BeanHelper;
6 use RedBeanPHP\Facade as Facade;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\SimpleModelHelper as SimpleModelHelper;
9
10 /**
11  * Bean Helper.
12  *
13  * The Bean helper helps beans to access access the toolbox and
14  * FUSE models. This Bean Helper makes use of the facade to obtain a
15  * reference to the toolbox.
16  *
17  * @file    RedBeanPHP/BeanHelperFacade.php
18  * @author  Gabor de Mooij and the RedBeanPHP Community
19  * @license BSD/GPLv2
20  *
21  * @copyright
22  * (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
23  * This source file is subject to the BSD/GPLv2 License that is bundled
24  * with this source code in the file license.txt.
25  */
26 class SimpleFacadeBeanHelper implements BeanHelper
27 {
28         /**
29          * Factory function to create instance of Simple Model, if any.
30          *
31          * @var \Closure
32          */
33         private static $factory = null;
34
35         /**
36          * Factory method using a customizable factory function to create
37          * the instance of the Simple Model.
38          *
39          * @param string $modelClassName name of the class
40          *
41          * @return SimpleModel
42          */
43         public static function factory( $modelClassName )
44         {
45                 $factory = self::$factory;
46                 return ( $factory ) ? $factory( $modelClassName ) : new $modelClassName();
47         }
48
49         /**
50          * Sets the factory function to create the model when using FUSE
51          * to connect a bean to a model.
52          *
53          * @param \Closure $factory factory function
54          *
55          * @return void
56          */
57         public static function setFactoryFunction( $factory )
58         {
59                 self::$factory = $factory;
60         }
61
62         /**
63          * @see BeanHelper::getToolbox
64          */
65         public function getToolbox()
66         {
67                 return Facade::getToolBox();
68         }
69
70         /**
71          * @see BeanHelper::getModelForBean
72          */
73         public function getModelForBean( OODBBean $bean )
74         {
75                 $model     = $bean->getMeta( 'type' );
76                 $prefix    = defined( 'REDBEAN_MODEL_PREFIX' ) ? REDBEAN_MODEL_PREFIX : '\\Model_';
77
78                 if ( strpos( $model, '_' ) !== FALSE ) {
79                         $modelParts = explode( '_', $model );
80                         $modelName = '';
81                         foreach( $modelParts as $part ) {
82                                 $modelName .= ucfirst( $part );
83                         }
84                         $modelName = $prefix . $modelName;
85
86                         if ( !class_exists( $modelName ) ) {
87                                 //second try
88                                 $modelName = $prefix . ucfirst( $model );
89
90                                 if ( !class_exists( $modelName ) ) {
91                                         return NULL;
92                                 }
93                         }
94
95                 } else {
96
97                         $modelName = $prefix . ucfirst( $model );
98                         if ( !class_exists( $modelName ) ) {
99                                 return NULL;
100                         }
101                 }
102                 $obj = self::factory( $modelName );
103                 $obj->loadBean( $bean );
104
105                 return $obj;
106         }
107
108         /**
109          * @see BeanHelper::getExtractedToolbox
110          */
111         public function getExtractedToolbox()
112         {
113                 return Facade::getExtractedToolbox();
114         }
115 }