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: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class SimpleFacadeBeanHelper implements BeanHelper
27: {
28: 29: 30: 31: 32:
33: private static $factory = null;
34:
35: 36: 37: 38: 39: 40: 41: 42:
43: public static function factory( $modelClassName )
44: {
45: $factory = self::$factory;
46: return ( $factory ) ? $factory( $modelClassName ) : new $modelClassName();
47: }
48:
49: 50: 51: 52: 53: 54: 55: 56:
57: public static function setFactoryFunction( $factory )
58: {
59: self::$factory = $factory;
60: }
61:
62: 63: 64:
65: public function getToolbox()
66: {
67: return Facade::getToolBox();
68: }
69:
70: 71: 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:
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: 110:
111: public function getExtractedToolbox()
112: {
113: return Facade::getExtractedToolbox();
114: }
115: }
116: