1# Concrete API
2
3There are many micro-frameworks that rely on magic methods. We don't. In this
4document we're gonna explore the Respect\Validation API without fluent interfaces
5or magic methods. We'll use a traditional dependency injection approach.
6
7```php
8use Respect\Validation\Validator as v;
9
10$usernameValidator = v::alnum()->noWhitespace()->length(1,15);
11$usernameValidator->validate('alganet'); // true
12```
13
14If you `var_dump($usernameValidator)`, you'll see a composite of objects with
15`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\NoWhitespace` and
16`Respect\Validation\Rules\Length`. There is a specific object for each rule, and
17the chain only builds the structure. You can build it by yourself:
18
19```php
20use Respect\Validation\Rules;
21
22$usernameValidator = new Rules\AllOf(
23    new Rules\Alnum(),
24    new Rules\NoWhitespace(),
25    new Rules\Length(1, 15)
26);
27$usernameValidator->validate('alganet'); // true
28```
29
30This is still a very lean API. You can use it in any dependency injection
31container or test it in the way you want. Nesting is still possible:
32
33```php
34use Respect\Validation\Rules;
35
36$usernameValidator = new Rules\AllOf(
37    new Rules\Alnum(),
38    new Rules\NoWhitespace(),
39    new Rules\Length(1, 15)
40);
41$userValidator = new Rules\Key('name', $usernameValidator);
42$userValidator->validate(['name' => 'alganet']); // true
43```
44
45## How It Works?
46
47The Respect\Validation chain is an
48[internal DSL](http://martinfowler.com/bliki/InternalDslStyle.html).
49It acts in the creational realm of objects (where Abstract Factories and Builders
50live), and it's only job is to make rule construction terse and fluent.
51
52## FAQ
53
54> Is `v` in `v::something` a class name?
55
56No! The class is `Respect\Validation\Validator`, we suggest `v` as a very short alias.
57
58> Is `v::something()` a static call?
59
60Yes. Just like the default `DateTime::createFromFormat()` or
61`Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration()`. It builds
62something complex and returns for you.
63
64> I really don't like static calls, can I avoid it?
65
66Yes. Just use `$validator = new Validator();` each time you want a new validator,
67and continue from there.
68
69> Do you have a static method for each rule?
70
71No. We use `__callStatic()`.
72
73> Magic methods are slow! Why do you use them?
74
75They're optional. If you use the `new` interface, they won't be called.
76
77(still, do some benchmarks, you'd be surprised with our implementation).
78