Skip to content

Instantly share code, notes, and snippets.

@argentinaluiz
Forked from gilsonDNA/Configurator.php
Created September 9, 2015 13:24
Show Gist options
  • Save argentinaluiz/33b6ef4c09e17a48da4b to your computer and use it in GitHub Desktop.
Save argentinaluiz/33b6ef4c09e17a48da4b 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 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