stoyantodorov / resolve-utilities
Laravel Package
Requires
- php: ^8.1
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-10-09 22:19:38 UTC
README
This package offers a way to instantiate a class, to send typed input to it and to receive typed result using a convenient interface:
public function useUtility(string $abstract, array $input): mixed
That is implemented by Resolver
class. It takes care to there are no duplicated instances and resets input/output in them before using.
The instantiated class should extend StoyanTodorov\ResolveUtilities\Utility
- so it is obliged to implement method execute
:
abstract public function execute(): self
Тhe data sent to useUtility
through array $input
is available as class properties in the instance. The output, that we expect, should be set in output
property. In this way we may rely on typed input and output without binding the executed code to a certain interface.
Requirements
-
PHP 8.1
-
Laravel
Installation
composer require stoyantodorov/resolve-utilities
Usage
Extend Utility
use StoyanTodorov\ResolveUtilities\Utility; class StringOutputExample extends Utility { protected string $output; protected string|null $propOne = null; protected int|null $propTwo = null; protected array $requiredInput = ['propOne']; protected array $defaultInput = ['propTwo' => 1]; public function execute(): Utility { $this->output = $this->propOne; return $this; } }
- In
requiredInput
add the properties names whichexecute
uses. - In
defaultInput
add the properties names with theirs default values. WhenuseUtility
method is called these values are used unless they aren't added to the second parameter.
Resolver
$resolver = new StoyanTodorov\ResolveUtilities\Resolver; $resultOne = $resolver->useUtility(StringOutputExample::class, ['propOne' => 'test']); $resultTwo = $resolver->useUtility(StringOutputExample::class, ['propOne' => 'test', 'propTwo' => 100]);
- The first parameter sent to
useUtility
may also be an abstract definition like'single-output-example'
. It will be instantiated if there is such definition inLaravel
Service Container
HasResolver
use StoyanTodorov\ResolveUtilities\HasResolver; class ExampleClient { use HasResolver; public function test(string $propOne): string { return $this->useUtility(StringOutputExample::class, compact('propOne')); } } $result = (new ExampleClient)->test('test');
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.