stoyantodorov/resolve-utilities

1.0.0 2023-10-23 08:46 UTC

This package is auto-updated.

Last update: 2024-09-09 22:05:53 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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 which execute uses.
  • In defaultInput add the properties names with theirs default values. When useUtility 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 in Laravel 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.