-
-
Save argentinaluiz/33b6ef4c09e17a48da4b 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 object!"); | |
} | |
if(!($options instanceof \Traversable) && !is_array($options)) | |
{ | |
throw new \Exception("Options should implements 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