gajus / vlad

Input validation library promoting succinct syntax with extendable validators and multilingual support.
Other
104 stars 8 forks source link

Validators aliasing #8

Closed bfontaine closed 3 years ago

bfontaine commented 10 years ago

Let validators declare a keyword to be called with instead of their full namespaced-name, e.g.:

$test->assert("foo")
     ->is("base64");

instead of:

$test->assert("foo")
     ->is("\My\Long\Namespace\Base64");
gajus commented 10 years ago

This would require some sort of alias method.

$test = new \Gajus\Vlad\Test();
$test->alias('My\Long\Namespace\Base64', 'Base64');
$test->assert('foo')->is('Base64');

The problem with the above is that as long as alias is Test specific, it is not much useful.

Alternatively, Test could lookup up static variable, e.g.

\Gajus\Vlad\Test::alias('My\Long\Namespace\Base64', 'Base64');
$test = new \Gajus\Vlad\Test();
$test->assert('foo')->is('Base64');

The problem with the above is that it would introduce a global state.

Final alternative would be to have user land class that defines those aliases, e.g.

class MyTest extends \Gajus\Vlad\Test {
    public function __construct (\Gajus\Vlad\Translator $translator = null) {
        parent::__construct($translator);
        $this->alias('My\Long\Namespace\Base64', 'Base64');
    }
}