Skip to content

Commit

Permalink
allow build object with callable.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Oct 23, 2017
1 parent 2384062 commit 31ee4c5
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/functions/values.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,22 @@ function get_object_changed_values($object)
}

/**
* @param string|\Closure|null $classOrClosure
* @param string|callable|null $classOrCallable
* @param array $values
* @param object|null $context
* @param string|null $contextKey
*
* @return object
*/
function build_object_ref($classOrClosure = null, array &$values, $context = null, $contextKey = null)
function build_object_ref($classOrCallable = null, array &$values, $context = null, $contextKey = null)
{
foreach (get_registered_hooks('build_object', 'get_object_class') as $callback) {
if ($dynamicClassOrClosure = call_user_func($callback, $values, $context, $contextKey)) {
$classOrClosure = $dynamicClassOrClosure;
if ($dynamicClassOrCallable = call_user_func($callback, $values, $context, $contextKey)) {
$classOrCallable = $dynamicClassOrCallable;
}
}

if (false == $classOrClosure) {
if (false == $classOrCallable) {
if ($context) {
throw new \LogicException(sprintf(
'Cannot built object for %s::%s. Either class or closure has to be passed explicitly or there must be a hook that provide an object class. Values: %s',
Expand All @@ -169,10 +169,10 @@ function build_object_ref($classOrClosure = null, array &$values, $context = nul
}
}

if ($classOrClosure instanceof \Closure) {
$class = $classOrClosure($values);
if (is_callable($classOrCallable)) {
$class = $classOrCallable($values);
} else {
$class = (string) $classOrClosure;
$class = (string) $classOrCallable;
}

$object = new $class();
Expand All @@ -197,14 +197,14 @@ function build_object_ref($classOrClosure = null, array &$values, $context = nul
}

/**
* @param string|\Closure|null $classOrClosure
* @param string|callable|null $classOrCallable
* @param array $values
*
* @return object
*/
function build_object($classOrClosure = null, array $values)
function build_object($classOrCallable = null, array $values)
{
return build_object_ref($classOrClosure, $values);
return build_object_ref($classOrCallable, $values);
}

function clone_object($object)
Expand Down

0 comments on commit 31ee4c5

Please sign in to comment.