stoyantodorov/resolve-utilities
最新稳定版本:1.0.0
Composer 安装命令:
composer require stoyantodorov/resolve-utilities
包简介
Laravel Package
README 文档
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
requiredInputadd the properties names whichexecuteuses. - In
defaultInputadd the properties names with theirs default values. WhenuseUtilitymethod 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
useUtilitymay also be an abstract definition like'single-output-example'. It will be instantiated if there is such definition inLaravelService 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.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-23