Skip to content

Instantly share code, notes, and snippets.

@gilsonDNA
Created September 9, 2015 12:01
Show Gist options
  • Save gilsonDNA/3ed0bf5b1eb3a599ae09 to your computer and use it in GitHub Desktop.
Save gilsonDNA/3ed0bf5b1eb3a599ae09 to your computer and use it in GitHub Desktop.
Configurator
<?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