Skip to content

Commit

Permalink
Merge pull request #11 from makasim/pass-class-or-closure-to-hook
Browse files Browse the repository at this point in the history
add access to classOrClosure in get object class hook .
  • Loading branch information
makasim authored Feb 16, 2018
2 parents 8b673ff + 538ee67 commit 0317f15
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/functions/values.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function get_object_changed_values($object)
function build_object_ref($classOrCallable = null, array &$values, $context = null, $contextKey = null)
{
foreach (get_registered_hooks(HooksEnum::BUILD_OBJECT, HooksEnum::GET_OBJECT_CLASS) as $callback) {
if ($dynamicClassOrCallable = call_user_func($callback, $values, $context, $contextKey)) {
if ($dynamicClassOrCallable = call_user_func($callback, $values, $context, $contextKey, $classOrCallable)) {
$classOrCallable = $dynamicClassOrCallable;
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Model/ObjectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Makasim\Values\Tests\Model;

interface ObjectInterface
{
}
23 changes: 23 additions & 0 deletions tests/ObjectsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function Makasim\Values\register_object_hooks;
use function Makasim\Values\set_values;
use Makasim\Values\Tests\Model\EmptyObject;
use Makasim\Values\Tests\Model\ObjectInterface;
use Makasim\Values\Tests\Model\OtherSubObject;
use Makasim\Values\Tests\Model\SubObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -711,4 +712,26 @@ public function testShouldNotChangeObjectValuesIfGetValuesCopiedTrue()
self::assertSame(['aName' => ['aKey' => ['aSubName' => ['aSubKey' => 'aFooVal']]]], get_values($obj));
self::assertSame(['aSubName' => ['aSubKey' => 'aFooVal']], get_values($subObj));
}

public function testClassProvidedByHookBasedOnInterface()
{
$values = [
'aKey' => [
'aSubName' => ['aSubKey' => 'aFooVal'],
],
];

$obj = new EmptyObject();
set_values($obj, $values);

register_hook(HooksEnum::BUILD_OBJECT, HooksEnum::GET_OBJECT_CLASS, function($values, $context, $contextKey, $classOrCallable) {
$this->assertSame(ObjectInterface::class, $classOrCallable);

return SubObject::class;
});

$subObj = get_object($obj, 'aKey', ObjectInterface::class);

$this->assertInstanceOf(SubObject::class, $subObj);
}
}

0 comments on commit 0317f15

Please sign in to comment.