From d1061cdb26effdf934ab89e1bc38998bbb8e01e3 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Wed, 1 Sep 2021 20:14:42 -0400 Subject: [PATCH] Adds object_assign and shadow helpers. --- README.md | 48 ++++++++++++++++------ helpers/common.php | 79 ++++++++++++++++++++++++++++-------- tests/CommonTest.php | 97 ++++++++++++++++++++++++++++++++------------ 3 files changed, 168 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index b5a46f5..02298f6 100644 --- a/README.md +++ b/README.md @@ -31,18 +31,18 @@ This package includes helpful global helpers for your project make almost anythi | | | | |---|---|---| -| [app_call](#app_call) | [in_console](#in_console) | [route_is](#route_is) -| [call_existing](#call_existing) | [in_development](#in_development) | [sleep_between](#sleep_between) -| [created](#created) | [logged_in](#logged_in) | [taptap](#taptap) -| [data_update](#data_update) | [methods_of](#methods_of) | [undot_path](#undot_path) -| [delist](#delist) | [missing_trait](#missing_trait) | [until](#until) -| [diff](#diff) | [none_of](#none_of) | [user](#user) -| [dot_path](#dot_path) | [ok](#ok) | [weekend](#weekend) -| [enclose](#enclose) | [period](#period) | [weekstart](#weekstart) -| [files](#files) | [period_from](#period_from) | [which_of](#which_of) -| [has_trait](#has_trait) | [pipe](#pipe) | [yesterday](#yesterday) -| [hashy](#hashy) | [remember](#remember) | - +| [app_call](#app_call) | [in_development](#in_development) | [route_is](#route_is) +| [call_existing](#call_existing) | [logged_in](#logged_in) | [shadow](#shadow) +| [created](#created) | [methods_of](#methods_of) | [sleep_between](#sleep_between) +| [data_update](#data_update) | [missing_trait](#missing_trait) | [taptap](#taptap) +| [delist](#delist) | [none_of](#none_of) | [undot_path](#undot_path) +| [diff](#diff) | [object_assign](#object_assign) | [until](#until) +| [dot_path](#dot_path) | [ok](#ok) | [user](#user) +| [enclose](#enclose) | [period](#period) | [weekend](#weekend) +| [files](#files) | [period_from](#period_from) | [weekstart](#weekstart) +| [has_trait](#has_trait) | [pipe](#pipe) | [which_of](#which_of) +| [hashy](#hashy) | [remember](#remember) | [yesterday](#yesterday) +| [in_console](#in_console) | [route_is](#route_is) | ### `app_call()` Executes a callable using the application Container. @@ -320,6 +320,18 @@ none_of('foo', ['bar', 'baz', 'qux'], fn ($subject, $compared) => $subject === $ // false ``` +### `object_assign()` + +Assigns an array of values to an object, recursively, using dot notation. + +```php +$object = new stdClass(); + +object_assign($object, ['foo' => 'bar']); + +echo $object->foo; // "bar" +``` + ### `ok()` Returns an HTTP 204 response (OK, No Content). @@ -439,6 +451,18 @@ if (route_is('dahsboard.*')) { } ``` +### `shadow()` + +Calls a method on an object if it exists, or returns false. It supports Macros. + +```php +if ($rendered = shadow($mayRender, 'render')) { + return $rendered; +} + +return response((string)$mayRender); +``` + ### `sleep_between()` Runs a callback while sleeping between multiple executions, returning a Collection of all results. diff --git a/helpers/common.php b/helpers/common.php index 3eda884..8f158f8 100644 --- a/helpers/common.php +++ b/helpers/common.php @@ -1,5 +1,6 @@ toArray(); + } + + foreach ($data as $name => $value) { + data_set($object, $name, $value, $overwrite); + } + + return $object; + } +} + if (!function_exists('pipe')) { /** * Sends an object through a pipeline. @@ -167,6 +209,26 @@ function remember( } } +if (!function_exists('shadow')) { + /** + * Calls a method on an object if it exists, or returns false. + * + * @param object $object + * @param string $method + * @param mixed ...$arguments + * + * @return mixed + */ + function shadow(object $object, string $method, mixed ...$arguments): mixed + { + if (method_exists($object, $method) || method_exists($object, 'hasMacro') && $object::hasMacro($method)) { + return $object->{$method}(...$arguments); + } + + return false; + } +} + if (!function_exists('sleep_between')) { /** * Runs a callback while sleeping between multiple executions. @@ -209,23 +271,6 @@ function taptap(mixed $value, callable $callback = null): mixed } } -if (!function_exists('hashy')) { - /** - * Creates a small BASE64 encoded MD5 hash from a string for portable checksum. - * - * @param \Stringable|\Illuminate\Support\Stringable|string $hashable - * @param string|null $hash The hash to compare the result. - * - * @return string|bool Returns a boolean if a comparable hash has been set to compare. - */ - function hashy(Stringable|LaravelStringable|string $hashable, string $hash = null): string|bool - { - $hashed = base64_encode(md5((string)$hashable, true)); - - return $hash ? hash_equals($hash, $hashed) : $hashed; - } -} - if (!function_exists('which_of')) { /** * Returns the key of the option which comparison or callback returns true. diff --git a/tests/CommonTest.php b/tests/CommonTest.php index afe6cc7..4f4068a 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -4,8 +4,10 @@ use Closure; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Fluent; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Macroable; use Orchestra\Testbench\TestCase; use Stringable; @@ -124,6 +126,25 @@ public function test(): string static::assertSame('bar.quz', $enclose('bar')); } + public function test_hashy(): void + { + $hashable = 'This is a hashable string'; + $expected = 'TJYa8+63dRbdN6w44shX1g=='; + + static::assertSame($expected, hashy($hashable)); + static::assertSame($expected, hashy(Str::of($hashable))); + static::assertSame($expected, hashy(new class implements Stringable { + public function __toString(): string + { + return 'This is a hashable string'; + } + })); + + static::assertTrue(hashy($hashable, $expected)); + static::assertFalse(hashy($hashable, $expected . '=')); + static::assertFalse(hashy($hashable, 'TJYa8+63dRbdN6w44shX1g=')); + } + public function test_in_console(): void { static::assertTrue(in_console()); @@ -186,6 +207,24 @@ public function test_none_of(): void })); } + public function test_object_assign() + { + $object = new class extends Fluent {}; + + $array = ['foo' => 'bar', 'baz' => 'quz']; + static::assertSame($object, object_assign($object, $array)); + static::assertSame('bar', $object->foo); + + $arrayable = new class(['baz' => 'cougar', 'qux' => 'quuz']) extends Fluent {}; + static::assertSame($object, object_assign($object, $arrayable)); + static::assertSame('cougar', $object->baz); + static::assertSame('quuz', $object->qux); + + $array = ['qux' => 'quux']; + static::assertSame($object, object_assign($object, $array, false)); + static::assertSame('quuz', $object->qux); + } + public function test_pipe(): void { $barToQuz = new class { @@ -222,17 +261,27 @@ static function (string $foo, Closure $next): string { static::assertSame('qux', $pipe); } - public function test_while_sleep(): void + public function test_shadow(): void { - $start = time(); - $collection = sleep_between(4, 1000, static function (): string { - return microtime(false); + $object = new class { + public function foo($string) { + return $string; + } + }; + + static::assertSame('bar', shadow($object, 'foo', 'bar')); + static::assertFalse(shadow($object, 'bar', 'bar')); + + $object = new class { + use Macroable; + }; + + $object::macro('bar', function ($string) { + return $string; }); - $end = time(); - static::assertCount(4, $collection); - static::assertGreaterThanOrEqual(3, $end - $start); - static::assertLessThan(5, $end - $start); + static::assertSame('foo', shadow($object, 'bar', 'foo')); + static::assertFalse(shadow($object, 'foo', 'bar')); } public function test_taptap(): void @@ -256,25 +305,6 @@ public function call(): bool { static::assertTrue($object->called); } - public function test_hashy(): void - { - $hashable = 'This is a hashable string'; - $expected = 'TJYa8+63dRbdN6w44shX1g=='; - - static::assertSame($expected, hashy($hashable)); - static::assertSame($expected, hashy(Str::of($hashable))); - static::assertSame($expected, hashy(new class implements Stringable { - public function __toString(): string - { - return 'This is a hashable string'; - } - })); - - static::assertTrue(hashy($hashable, $expected)); - static::assertFalse(hashy($hashable, $expected . '=')); - static::assertFalse(hashy($hashable, 'TJYa8+63dRbdN6w44shX1g=')); - } - public function test_which_of(): void { static::assertSame(3, which_of('foo', ['bar', 'quz', 'qux', 'foo'])); @@ -300,4 +330,17 @@ public function test_which_of(): void } })); } + + public function test_while_sleep(): void + { + $start = time(); + $collection = sleep_between(4, 1000, static function (): string { + return microtime(false); + }); + $end = time(); + + static::assertCount(4, $collection); + static::assertGreaterThanOrEqual(3, $end - $start); + static::assertLessThan(5, $end - $start); + } }