Skip to content

Latest commit

 

History

History
35 lines (29 loc) · 1.24 KB

constraint.md

File metadata and controls

35 lines (29 loc) · 1.24 KB

ConstraintActivator

The ConstraintActivator return true or false by checking a constraint. The constraint is a string expression for the symfony expression language and must return true or false. The activator expect an array with feature name as key and the expression as value. Additional, it needs a constraint resolver which implement the ConstraintResolverInterface. Example:

// MyClass.php
class MyClass
{
    public function doSomething(User $user)
    {
        $expressionLanguage = new ExpressionLanguage();
        $constraintResolver = new ConstraintResolver($expressionLanguage);
        
        $features = [
            'feature_foo' => 'user_role === "Admin"',
            'feature_def' => 'user_role === "Guest"',
        ];
        
        $activator = new ConstraintActivator($constraintResolver, $features);

        $context = new Context();
        $context->add('user_role', $user->getRole());
        
        $manager = new FeatureManager($activator);

        // This will return true if user role is 'Guest' otherwise false.
        if ($manager->isActive('feature_def', $context)) {
            // do something
        }
    }
}