Last active
January 11, 2022 12:42
-
-
Save schophil/8d7d852827338aa80213651fe9767dd2 to your computer and use it in GitHub Desktop.
Facade bean helper for RedBeanPHP that allows mapping types with a common prefix to prefix-less models.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper; | |
use RedBeanPHP\OODBBean as OODBBean; | |
/** | |
* Custom facade bean helper that is aware of a textual prefix in the type names. | |
* For example mususer, muscontact, musXXX. | |
* | |
* @author schophil | |
*/ | |
class PrefixFacadeBeanHelper extends SimpleFacadeBeanHelper | |
{ | |
private $prefix; | |
public function __construct($prefix) | |
{ | |
$this->prefix = $prefix; | |
} | |
public function getModelForBean(OODBBean $bean) | |
{ | |
$model = $bean->getMeta('type'); | |
$prefix = defined( 'REDBEAN_MODEL_PREFIX' ) ? REDBEAN_MODEL_PREFIX : '\\Model_'; | |
if (strpos($model, $this->prefix) === 0) { | |
$newModel = substr($model, strlen($this->prefix)); | |
$model = $newModel; | |
} | |
// Below is copied from the super implementation. | |
// https://github.com/gabordemooij/redbean/blob/master/RedBeanPHP/BeanHelper/SimpleFacadeBeanHelper.php | |
if ( strpos( $model, '_' ) !== FALSE ) { | |
$modelParts = explode( '_', $model ); | |
$modelName = ''; | |
foreach( $modelParts as $part ) { | |
$modelName .= ucfirst( $part ); | |
} | |
$modelName = $prefix . $modelName; | |
if ( !class_exists( $modelName ) ) { | |
$modelName = $prefix . ucfirst( $model ); | |
if ( !class_exists( $modelName ) ) { | |
return NULL; | |
} | |
} | |
} else { | |
$modelName = $prefix . ucfirst( $model ); | |
if ( !class_exists( $modelName ) ) { | |
return NULL; | |
} | |
} | |
$obj = self::factory( $modelName ); | |
$obj->loadBean( $bean ); | |
return $obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment