Skip to content

Typecast handlers: setRules method #307

Answered by butschster
sergey-telpuk asked this question in Q&A
Discussion options

You must be logged in to vote

Method setRules runs only once when typecast handlers initialize. In this method you can decide which rules should be processed by each handler.

For example we have two typecast handlers:

Default

final class Typecast implements CastableInterface
{
    private const RULES = ['int', 'bool', 'float', 'datetime'];

    /** @var array<non-empty-string, bool> */
    private array $callableRules = [];

    /** @var array<non-empty-string, mixed> */
    private array $rules = [];

    public function __construct(
        private DatabaseInterface $database
    ) {
    }

    public function setRules(array $rules): array
    {
        foreach ($rules as $key => $rule) {
            if (in_array($rule

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by roxblnfk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #298 on January 10, 2022 09:00.