Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ChangedValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ protected function registerChangedValuesHooks()
return $value;
};

register_hook(get_class($this), 'post_set_values', $resetChangedValuesHook);
register_hook(get_class($this), 'post_set_value', $trackChangesHook);
register_hook(get_class($this), 'post_add_value', $trackChangesHook);
register_hook(get_class($this), HooksEnum::POST_SET_VALUES, $resetChangedValuesHook);
register_hook(get_class($this), HooksEnum::POST_SET_VALUE, $trackChangesHook);
register_hook(get_class($this), HooksEnum::POST_ADD_VALUE, $trackChangesHook);
}
}
20 changes: 20 additions & 0 deletions src/HooksEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Makasim\Values;

class HooksEnum
{
public const PRE_SET_VALUE = 'pre_set_value';
public const PRE_ADD_VALUE = 'pre_add_value';
public const POST_SET_VALUE = 'post_set_value';
public const POST_SET_VALUES = 'post_set_values';
public const POST_ADD_VALUE = 'post_add_values';
public const POST_GET_VALUE = 'post_get_value';

public const POST_SET_OBJECT = 'post_set_object';
public const POST_ADD_OBJECT = 'post_add_object';
public const POST_BUILD_OBJECT = 'post_build_object';
public const POST_BUILD_SUB_OBJECT = 'post_build_sub_object';
public const GET_OBJECT_CLASS = 'get_object_class';
public const BUILD_OBJECT = 'build_object';
}
2 changes: 1 addition & 1 deletion src/ObjectBuilderHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(array $classMap, string $fieldName = 'schema')

public function register()
{
register_global_hook('get_object_class', function(array $values) {
register_global_hook(HooksEnum::GET_OBJECT_CLASS, function(array $values) {
if (false == isset($values[$this->fieldName])) {
return;
}
Expand Down
18 changes: 9 additions & 9 deletions src/functions/objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function set_object($context, $key, $object)

array_set($key, $object, $this->objects);

foreach (get_registered_hooks($context, 'post_set_object') as $callback) {
foreach (get_registered_hooks($context, HooksEnum::POST_SET_OBJECT) as $callback) {
call_user_func($callback, $object, $context, $key);
}
} else {
Expand Down Expand Up @@ -52,7 +52,7 @@ function set_objects($context, $key, $objects)

array_set($key.'.'.$objectKey, $object, $this->objects);

foreach (get_registered_hooks($context, 'post_set_object') as $callback) {
foreach (get_registered_hooks($context, HooksEnum::POST_SET_OBJECT) as $callback) {
call_user_func($callback, $object, $context, $key.'.'.$objectKey);
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ function add_object($context, $key, $object, $objectKey = null)

array_set($key.'.'.$objectKey, $object, $this->objects);

foreach (get_registered_hooks($context, 'post_add_object') as $callback) {
foreach (get_registered_hooks($context, HooksEnum::POST_ADD_OBJECT) as $callback) {
call_user_func($callback, $object, $context, $key.'.'.$objectKey);
}

Expand Down Expand Up @@ -145,9 +145,9 @@ function register_object_hooks()
});
};

register_global_hook('post_set_value', $resetObjectsHook);
register_global_hook('post_add_value', $resetObjectsHook);
register_global_hook('post_set_values', function($object) {
register_global_hook(HooksEnum::POST_SET_VALUE, $resetObjectsHook);
register_global_hook(HooksEnum::POST_ADD_VALUE, $resetObjectsHook);
register_global_hook(HooksEnum::POST_SET_VALUES, function($object) {
call($object, function() {
$this->objects = [];
});
Expand All @@ -156,15 +156,15 @@ function register_object_hooks()

function register_propagate_root_hooks($object)
{
register_hook($object, 'post_set_object', function ($object, $context, $contextKey) {
register_hook($object, HooksEnum::POST_SET_OBJECT, function ($object, $context, $contextKey) {
propagate_root($object, $context, $contextKey);
});

register_hook($object, 'post_add_object', function ($object, $context, $contextKey) {
register_hook($object, HooksEnum::POST_ADD_OBJECT, function ($object, $context, $contextKey) {
propagate_root($object, $context, $contextKey);
});

register_hook($object, 'post_build_sub_object', function ($object, $context, $contextKey) {
register_hook($object, HooksEnum::POST_BUILD_SUB_OBJECT, function ($object, $context, $contextKey) {
register_propagate_root_hooks($object);
propagate_root($object, $context, $contextKey);
});
Expand Down
30 changes: 15 additions & 15 deletions src/functions/values.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function set_values($object, array &$values, bool $byReference = false)
$this->values = $values;
}

foreach (get_registered_hooks($this, 'post_set_values') as $callback) {
foreach (get_registered_hooks($this, HooksEnum::POST_SET_VALUES) as $callback) {
call_user_func($callback, $this, $values, $byReference);
}

Expand All @@ -37,7 +37,7 @@ function get_values($object, bool $copy = true): array
function add_value($object, $key, $value, $valueKey = null)
{
return (function($key, $value, $valueKey) {
foreach (get_registered_hooks($this, 'pre_add_value') as $callback) {
foreach (get_registered_hooks($this, HooksEnum::PRE_ADD_VALUE) as $callback) {
if (null !== $changedValue = call_user_func($callback, $this, $key, $value)) {
$value = $changedValue;
}
Expand All @@ -64,7 +64,7 @@ function add_value($object, $key, $value, $valueKey = null)
$modified = array_set($key, $newValue, $this->values);
}

foreach (get_registered_hooks($this, 'post_add_value') as $callback) {
foreach (get_registered_hooks($this, HooksEnum::POST_ADD_VALUE) as $callback) {
call_user_func($callback, $this, $key.'.'.$valueKey, $value, $modified);
}

Expand All @@ -75,7 +75,7 @@ function add_value($object, $key, $value, $valueKey = null)
function set_value($object, $key, $value)
{
return (function($key, $value) {
foreach (get_registered_hooks($this, 'pre_set_value') as $callback) {
foreach (get_registered_hooks($this, HooksEnum::PRE_SET_VALUE) as $callback) {
if (null !== $newValue = call_user_func($callback, $this, $key, $value)) {
$value = $newValue;
}
Expand All @@ -87,7 +87,7 @@ function set_value($object, $key, $value)
$modified = array_unset($key, $this->values);
}

foreach (get_registered_hooks($this, 'post_set_value') as $callback) {
foreach (get_registered_hooks($this, HooksEnum::POST_SET_VALUE) as $callback) {
call_user_func($callback, $this, $key, $value, $modified);
}
})->call($object, $key, $value);
Expand All @@ -98,7 +98,7 @@ function get_value($object, $key, $default = null, $castTo = null)
return (function($key, $default, $castTo) {
$value = array_get($key, $default , $this->values);

foreach (get_registered_hooks($this, 'post_get_value') as $callback) {
foreach (get_registered_hooks($this, HooksEnum::POST_GET_VALUE) as $callback) {
if (null !== $newValue = call_user_func($callback, $this, $key, $value, $default, $castTo)) {
$value = $newValue;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ function get_object_changed_values($object)
*/
function build_object_ref($classOrCallable = null, array &$values, $context = null, $contextKey = null)
{
foreach (get_registered_hooks('build_object', 'get_object_class') as $callback) {
foreach (get_registered_hooks(HooksEnum::BUILD_OBJECT, HooksEnum::GET_OBJECT_CLASS) as $callback) {
if ($dynamicClassOrCallable = call_user_func($callback, $values, $context, $contextKey)) {
$classOrCallable = $dynamicClassOrCallable;
}
Expand Down Expand Up @@ -186,11 +186,11 @@ function build_object_ref($classOrCallable = null, array &$values, $context = nu
set_values($object, $values, true);

if ($context) {
foreach (get_registered_hooks($context, 'post_build_sub_object') as $callback) {
foreach (get_registered_hooks($context, HooksEnum::POST_BUILD_SUB_OBJECT) as $callback) {
call_user_func($callback, $object, $context, $contextKey);
}
} else {
foreach (get_registered_hooks($object, 'post_build_object') as $callback) {
foreach (get_registered_hooks($object, HooksEnum::POST_BUILD_OBJECT) as $callback) {
call_user_func($callback, $object);
}
}
Expand Down Expand Up @@ -232,13 +232,13 @@ function register_cast_hooks($objectOrClass = null) {
};

if ($objectOrClass) {
register_hook($objectOrClass, 'pre_set_value', $castValueHook);
register_hook($objectOrClass, 'pre_add_value', $castValueHook);
register_hook($objectOrClass, 'post_get_value', $castToHook);
register_hook($objectOrClass, HooksEnum::PRE_SET_VALUE, $castValueHook);
register_hook($objectOrClass, HooksEnum::PRE_ADD_VALUE, $castValueHook);
register_hook($objectOrClass, HooksEnum::POST_GET_VALUE, $castToHook);
} else {
register_global_hook('pre_set_value', $castValueHook);
register_global_hook('pre_add_value', $castValueHook);
register_global_hook('post_get_value', $castToHook);
register_global_hook(HooksEnum::PRE_SET_VALUE, $castValueHook);
register_global_hook(HooksEnum::PRE_ADD_VALUE, $castValueHook);
register_global_hook(HooksEnum::POST_GET_VALUE, $castToHook);
}
}

Expand Down
35 changes: 18 additions & 17 deletions tests/HookStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function Makasim\Values\build_object_ref;
use function Makasim\Values\get_object;
use function Makasim\Values\get_value;
use Makasim\Values\HooksEnum;
use Makasim\Values\HookStorage;
use function Makasim\Values\set_object;
use function Makasim\Values\set_objects;
Expand Down Expand Up @@ -287,7 +288,7 @@ public function testShouldCallPostSetValuesCallbackOnPostSetValues()
$isCalled = false;
$values = ['foo' => 'bar'];

HookStorage::register($obj, 'post_set_values', function() use ($obj, $values, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_SET_VALUES, function() use ($obj, $values, &$isCalled) {
$isCalled = true;

self::assertSame($obj, func_get_arg(0));
Expand All @@ -308,7 +309,7 @@ public function testShouldCallPostSetValueCallbackOnSetValuesAndPassByReferenceA
$isCalled = false;
$values = ['foo' => 'bar'];

HookStorage::register($obj, 'post_set_values', function() use ($obj, $values, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_SET_VALUES, function() use ($obj, $values, &$isCalled) {
$isCalled = true;

self::assertTrue(func_get_arg(2));
Expand All @@ -328,7 +329,7 @@ public function testShouldCallPreAddValueCallbackOnAddValue()
$value = 'bar';
$key = 'foo';

HookStorage::register($obj, 'pre_add_value', function() use ($obj, $key, $value, &$isCalled) {
HookStorage::register($obj, HooksEnum::PRE_ADD_VALUE, function() use ($obj, $key, $value, &$isCalled) {
$isCalled = true;

self::assertSame($obj, func_get_arg(0));
Expand All @@ -352,7 +353,7 @@ public function testShouldAllowModifyValueInPreAddValueCallback()
$value = 'bar';
$key = 'foo';

HookStorage::register($obj, 'pre_add_value', function() use (&$isCalled) {
HookStorage::register($obj, HooksEnum::PRE_ADD_VALUE, function() use (&$isCalled) {
$isCalled = true;

return 'baz';
Expand All @@ -374,7 +375,7 @@ public function testShouldCallPostAddValueCallbackOnAddValue()
$value = 'bar';
$key = 'foo';

HookStorage::register($obj, 'post_add_value', function() use ($obj, $key, $value, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_ADD_VALUE, function() use ($obj, $key, $value, &$isCalled) {
$isCalled = true;

self::assertSame($obj, func_get_arg(0));
Expand All @@ -398,7 +399,7 @@ public function testShouldCallPostAddValueCallbackOnAddValueWithCustomValueKey()
$valueKey = 'valKey';
$key = 'foo';

HookStorage::register($obj, 'post_add_value', function() use ($key, $valueKey, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_ADD_VALUE, function() use ($key, $valueKey, &$isCalled) {
$isCalled = true;

self::assertSame($key.'.'.$valueKey, func_get_arg(1));
Expand All @@ -418,7 +419,7 @@ public function testShouldCallPreSetValueCallbackOnSetValue()
$value = 'bar';
$key = 'foo';

HookStorage::register($obj, 'pre_set_value', function() use ($obj, $key, $value, &$isCalled) {
HookStorage::register($obj, HooksEnum::PRE_SET_VALUE, function() use ($obj, $key, $value, &$isCalled) {
$isCalled = true;

self::assertSame($obj, func_get_arg(0));
Expand All @@ -442,7 +443,7 @@ public function testShouldAllowModifyValueInPreSetValueCallback()
$value = 'bar';
$key = 'foo';

HookStorage::register($obj, 'pre_set_value', function() use (&$isCalled) {
HookStorage::register($obj, HooksEnum::PRE_SET_VALUE, function() use (&$isCalled) {
$isCalled = true;

return 'baz';
Expand All @@ -464,7 +465,7 @@ public function testShouldCallPostSetValueCallbackOnSetValue()
$value = 'bar';
$key = 'foo';

HookStorage::register($obj, 'post_set_value', function() use ($obj, $key, $value, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_SET_VALUE, function() use ($obj, $key, $value, &$isCalled) {
$isCalled = true;

self::assertSame($obj, func_get_arg(0));
Expand All @@ -489,7 +490,7 @@ public function testShouldCallPostGetValueCallbackOnGetValue()

set_value($obj, $key, $value);

HookStorage::register($obj, 'post_get_value', function() use ($obj, $key, $value, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_GET_VALUE, function() use ($obj, $key, $value, &$isCalled) {
$isCalled = true;

self::assertSame($obj, func_get_arg(0));
Expand All @@ -513,7 +514,7 @@ public function testShouldAllowModifyValueInPostGetValueCallback()

set_value($obj, 'foo', 'bar');

HookStorage::register($obj, 'post_get_value', function() use (&$isCalled) {
HookStorage::register($obj, HooksEnum::POST_GET_VALUE, function() use (&$isCalled) {
$isCalled = true;

return 'baz';
Expand All @@ -530,7 +531,7 @@ public function testShouldCallPostBuildObjectCallbackOnBuildObject()
$isCalled = false;
$actualObj = null;

HookStorage::register(EmptyObject::class, 'post_build_object', function() use (&$actualObj, &$isCalled) {
HookStorage::register(EmptyObject::class, HooksEnum::POST_BUILD_OBJECT, function() use (&$actualObj, &$isCalled) {
$isCalled = true;

$actualObj = func_get_arg(0);
Expand All @@ -551,7 +552,7 @@ public function testShouldCallPostBuildObjectCallbackOnBuildObjectWithContext()
$isCalled = false;
$actualObj = null;

HookStorage::register(EmptyObject::class, 'post_build_sub_object', function() use ($parentObj, &$actualObj, &$isCalled) {
HookStorage::register(EmptyObject::class, HooksEnum::POST_BUILD_SUB_OBJECT, function() use ($parentObj, &$actualObj, &$isCalled) {
$isCalled = true;

$actualObj = func_get_arg(0);
Expand All @@ -573,7 +574,7 @@ public function testShouldCallPostSetObjectCallbackOnSetObject()
$isCalled = false;
$actualObj = null;

HookStorage::register($obj, 'post_set_object', function() use ($subObj, $obj, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_SET_OBJECT, function() use ($subObj, $obj, &$isCalled) {
$isCalled = true;

self::assertSame($subObj, func_get_arg(0));
Expand All @@ -594,7 +595,7 @@ public function testShouldCallPostAddObjectCallbackOnAddObject()
$isCalled = false;
$actualObj = null;

HookStorage::register($obj, 'post_add_object', function() use ($subObj, $obj, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_ADD_OBJECT, function() use ($subObj, $obj, &$isCalled) {
$isCalled = true;

self::assertSame($subObj, func_get_arg(0));
Expand All @@ -615,7 +616,7 @@ public function testShouldCallPostSetObjectCallbackOnSetObjects()
$isCalled = false;
$actualObj = null;

HookStorage::register($obj, 'post_set_object', function() use ($subObj, $obj, &$isCalled) {
HookStorage::register($obj, HooksEnum::POST_SET_OBJECT, function() use ($subObj, $obj, &$isCalled) {
$isCalled = true;

self::assertSame($subObj, func_get_arg(0));
Expand All @@ -642,7 +643,7 @@ public function testShouldCallGetObjectClassOnGetObjectIfClassOrClosureArgumentN
$isCalled = false;
$actualObj = null;

HookStorage::register('build_object', 'get_object_class', function() use ($obj, &$isCalled) {
HookStorage::register(HooksEnum::BUILD_OBJECT, HooksEnum::GET_OBJECT_CLASS, function() use ($obj, &$isCalled) {
$isCalled = true;

self::assertSame(['aSubKey' => 'aFooVal'], func_get_arg(0));
Expand Down
5 changes: 3 additions & 2 deletions tests/ObjectsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use function Makasim\Values\get_objects;
use function Makasim\Values\get_values;
use function Makasim\Values\get_object_changed_values;
use Makasim\Values\HooksEnum;
use Makasim\Values\HookStorage;
use function Makasim\Values\register_hook;
use function Makasim\Values\register_object_hooks;
Expand Down Expand Up @@ -660,7 +661,7 @@ public function testShouldBuildObjectFromClassProvidedByHook()
$obj = new EmptyObject();
set_values($obj, $values);

register_hook('build_object', 'get_object_class', function($object, $key, $values) {
register_hook(HooksEnum::BUILD_OBJECT, HooksEnum::GET_OBJECT_CLASS, function($object, $key, $values) {
return SubObject::class;
});

Expand All @@ -683,7 +684,7 @@ public function testClassProvidedByHookShouldTakePriorityOverClassAsArgument()
$obj = new EmptyObject();
set_values($obj, $values);

register_hook('build_object', 'get_object_class', function($object, $key, $values) use ($hookClass) {
register_hook(HooksEnum::BUILD_OBJECT, HooksEnum::GET_OBJECT_CLASS, function($object, $key, $values) use ($hookClass) {
return $hookClass;
});

Expand Down