PHP interface as a tag name
The FQCN[1] of an interface implemented by the classes can be used as the tag name.
A collection injection is declared using the diTaggedAs() helper function or the \Kaspi\DiContainer\Attributes\TaggedAs attribute.
IMPORTANT
PHP classes that have not been explicitly registered in the container will be ignored during tag-based lookups; therefore, you must ensure that these classes are added to the container using one of the following methods:
- importing PHP classes — via the
DiContainerBuilder::import()method. - using configuration files — via the
DiContainerBuilder::load()method.
As an example, let’s look at the \App\Services\FooValidator class:
<?php
declare(strict_types=1);
// file: /app/src/Services/FooValidator.php
namespace App\Services;
use App\Interfaces\RuleInterface;
final class FooValidator
{
/**
* @param iterable<RuleInterface> $rules
*/
public function __construct(public readonly iterable $rules) {}
public function validate(mixed $input): mixed
{
foreach ($this->rules as $rule) {
$input = $rule->validate($input);
}
return $input;
}
}<?php
declare(strict_types=1);
// file: /app/src/Interfaces/RuleInterface.php
namespace App\Interfaces;
interface RuleInterface
{
public function validate(mixed $input): mixed;
}It is necessary to declare the injection of a collection of PHP classes all of which implement the \App\Interfaces\RuleInterface interface into the $rules constructor parameter of the class.
Below are the PHP classes that perform a specific data validation:
<?php
declare(strict_types=1);
// file: /app/src/ValidateRules/RuleA.php
namespace App\ValidateRules;
use App\Interfaces\RuleInterface;
final class RuleA implements RuleInterface
{
public function validate(mixed $input): mixed
{
// …
}
}<?php
declare(strict_types=1);
// file: /app/src/ValidateRules/RuleB.php
namespace App\ValidateRules;
final class RuleB
{
public function validate(mixed $input): mixed
{
// …
}
}<?php
declare(strict_types=1);
// file: /app/src/ValidateRules/RuleC.php
namespace App\ValidateRules;
use App\Interfaces\RuleInterface;
final class RuleC implements RuleInterface
{
public function validate(mixed $input): mixed
{
// …
}
}An important note: the PHP class \App\ValidateRules\RuleB does not implement the \App\Interfaces\RuleInterface interface via the implements keyword.
Declaration in configuration files
Declare the injection of a collection of PHP classes that implement the \App\Interfaces\RuleInterface interface into the $rules constructor parameter of the \App\Services\FooValidator PHP class.
In the services.php configuration file, you must specify the FQCN of the interface \App\Interfaces\RuleInterface within the $tag parameter of the diTaggedAs() helper function.
To register other PHP classes in the container, we use the DiContainerBuilder::import() method.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\FooValidator;
$container = (new DiContainerBuilder())
->load('/app/config/services.php')
->import(namespace: 'App\\', src: '/app/src')
->build();
$validator = $container->get(FooValidator::class);
foreach ($validator->rules as $rule) {
var_dump($rule::class);
}<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Interfaces\RuleInterface;
use App\Services\FooValidator;
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(FooValidator::class)
->bindArguments(
rules: diTaggedAs(RuleInterface::class)
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string (22) "App\ValidateRules\RuleA"
string (22) "App\ValidateRules\RuleC"The class \App\ValidateRules\RuleB was not included in the collection because it does not implement the App\Interfaces\RuleInterface interface.
NOTE
The order of elements in the collection is not defined in this configuration and can be any. Please review the documentation section "Collection Item Priority".
Declaration via attributes
Configure the injection of a collection of PHP classes that implement the \App\Interfaces\RuleInterface interface into the $rules constructor parameter of the \App\Services\FooValidator PHP class.
In the \Kaspi\DiContainer\Attributes\TaggedAs attribute, the $name parameter must be set to the FQCN of the interface: \App\Interfaces\RuleInterface.
<?php
declare(strict_types=1);
// file: /app/src/Services/FooValidator.php
namespace App\Services;
use App\Interfaces\RuleInterface;
use Kaspi\DiContainer\Attributes\TaggedAs;
final class FooValidator
{
/**
* @param iterable<RuleInterface> $rules
*/
public function __construct(
#[TaggedAs(RuleInterface::class)]
public readonly iterable $rules
) {}
public function validate(mixed $input): mixed
{
foreach ($this->rules as $rule) {
$input = $rule->validate($input);
}
return $input;
}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\FooValidator;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$validator = $container->get(FooValidator::class);
foreach ($validator->rules as $rule) {
var_dump($rule::class);
}Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string (22) "App\ValidateRules\RuleA"
string (22) "App\ValidateRules\RuleC"The class \App\ValidateRules\RuleB was not included in the collection because it does not implement the App\Interfaces\RuleInterface interface.
NOTE
The order of elements in the collection is not defined in this configuration and can be any. Please review the documentation section "Collection Item Priority".
Ordering of collection elements
When using an interface's FQCN as a tag name, the order of elements in the injected collection is undefined.
To control the order of elements in such a collection, you can use the $priorityDefaultMethod parameter available in the diTaggedAs() helper function or the \Kaspi\DiContainer\Attributes\TaggedAs attribute to specify a default priority method.
You must add a static method to the relevant PHP class, this method should return the priority value for the collection:
public static function getPriority(): int
{
// Each class has its own priority
return 100;
}If a priority method is not defined by default in a PHP class that serves as an element of a collection, the priority value will be null.
For the diTaggedAs() helper function, specify the priority method getPriority:
<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Interfaces\RuleInterface;
use App\Services\FooValidator;
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(FooValidator::class)
->bindArguments(
rules: diTaggedAs(
RuleInterface::class,
priorityDefaultMethod: 'getPriority',
)
);
};For the \Kaspi\DiContainer\Attributes\TaggedAs attribute, specify the priority method getPriority:
<?php
declare(strict_types=1);
// file: /app/src/Services/FooValidator.php
namespace App\Services;
use App\Interfaces\RuleInterface;
use Kaspi\DiContainer\Attributes\TaggedAs;
final class FooValidator
{
/**
* @param iterable<RuleInterface> $rules
*/
public function __construct(
#[TaggedAs(
RuleInterface::class,
priorityDefaultMethod: 'getPriority',
)]
public readonly iterable $rules
) {}
public function validate(mixed $input): mixed
{
foreach ($this->rules as $rule) {
$input = $rule->validate($input);
}
return $input;
}
}⬆️ Review the information in the “Collection element priority” section of the documentation.
Fully Qualified Class Name – the full name of a class or interface, including its namespace. ↩︎