respect-validation/docs/concrete-api.md
Alexandre Gomes Gaigalas d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00

2.8 KiB

Concrete API

There are many micro-frameworks that rely on magic methods. We don't. In this document we're going to explore the Respect\Validation API without fluent interfaces or magic methods. We'll use a traditional dependency injection approach.

use Respect\Validation\ValidatorBuilder as v;

$usernameValidator = v::alnum()->notSpaced()->length(1, 15);
$usernameValidator->isValid('alganet'); // true

If you var_dump($usernameValidator), you'll see a composite of objects with Respect\Validation\Validators\Alnum, Respect\Validation\Validators\Spaced wrapped in Respect\Validation\Validators\Not and Respect\Validation\Validators\Length. There is a specific object for each validator, and the chain only builds the structure. You can build it by yourself:

use Respect\Validation\Validators;
use Respect\Validation\ValidatorBuilder;

$usernameValidator = ValidatorBuilder::init(
    new Validators\Alnum(),
    new Validators\Not(
        new Validators\Spaced(),
    ),
    new Validators\Length(1, 15),
);
$usernameValidator->isValid('alganet'); // true

This is still a very lean API. You can use it in any dependency injection container or test it in the way you want. Nesting is still possible:

use Respect\Validation\Validators;

$usernameValidator = ValidatorBuilder::init(
    new Validators\Key(
        'name',
        new Validators\AllOf(
            new Validators\Alnum(),
            new Validators\Not(
                new Validators\Spaced(),
            ),
            new Validators\Length(1, 15),
        )
    )
);
$userValidator->isValid(['name' => 'alganet']); // true

How It Works?

The Respect\Validation chain is an internal DSL. It acts in the creational realm of objects (where Abstract Factories and Builders live), and it's only job is to make validator construction terse and fluent.

FAQ

Is v in v::something a class name?

No! The class is Respect\Validation\ValidatorBuilder, we suggest v as a very short alias.

Is v::something() a static call?

Yes. Just like the default DateTime::createFromFormat() or Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(). It builds something complex and returns for you.

I really don't like static calls, can I avoid it?

Yes. Just use $validator = ValidatorBuilder::create(); each time you want a new validator, and continue from there.

Do you have a static method for each validator?

No. We use __callStatic().

Magic methods are slow! Why do you use them?

They're optional. If you use the new interface, they won't be called.

(still, do some benchmarks, you'd be surprised with our implementation).