Created
September 9, 2015 12:01
-
-
Save gilsonDNA/3ed0bf5b1eb3a599ae09 to your computer and use it in GitHub Desktop.
Configurator
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 | |
namespace Livraria\Entity; | |
class Configurator { | |
public static function configure($target, $options, $tryCall=false) | |
{ | |
if( !is_object($target)){ | |
throw new \Exception('Target should be an objet'); | |
} | |
if( !($options instanceof Traversable) && !is_array($options) ) | |
{ | |
throw new \Exception('$options should implement Traversable'); | |
} | |
$tryCall = (bool) $tryCall && method_exists($target, '__call'); | |
foreach ($options as $name => &$value) | |
{ | |
$setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); | |
if( $tryCall || method_exists($target, $setter)) | |
{ | |
call_user_func(array($target, $setter), $value ); | |
}else{ | |
continue; | |
} | |
return $target; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment