/** * It checks whether feature is active or not * * @param null $featureName * @param array $attributes * @throws QandidateException * @return True */ public function active($featureName = null, $attributes = []) { if (!$featureName) { throw new QandidateException("Feature name must not be empty"); } if (!is_array($attributes)) { throw new QandidateException("Attribute must be an instance of array"); } $this->getToggleConditions($featureName); $conditionCollection = []; foreach ($this->conditions as $condition) { $class = $this->convertStringToClass($condition->operator); if ('Qandidate\\Toggle\\Operator\\InSet' === $class) { $value = [$condition->value]; } else { $value = $condition->value; } $operator = new $class($value); $conditionCollection[] = new OperatorCondition($condition->key, $operator); } $toggle = new Toggle('toggling', $conditionCollection); $constName = $this->convertStringToConstant($this->toggle->status); if ('INACTIVE' === $constName) { $toggle->deactivate(); } else { $toggle->activate(constant('Qandidate\\Toggle\\Toggle::' . $constName)); } $this->manager->add($toggle); $context = new Context(); foreach ($attributes as $key => $value) { $context->set($key, $value); } return $this->manager->active('toggling', $context); }
/** * @test */ public function it_exposes_all_toggles() { $toggle = new Toggle('some-feature', array()); $toggle2 = new Toggle('some-other-feature', array()); $manager = new ToggleManager(new InMemoryCollection()); $manager->add($toggle); $manager->add($toggle2); $all = $manager->all(); $this->assertCount(2, $all); $this->assertEquals($all['some-feature'], $toggle); $this->assertEquals($all['some-other-feature'], $toggle2); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Qandidate\Toggle\Context; use Qandidate\Toggle\ToggleManager; use Qandidate\Toggle\Serializer\InMemoryCollectionSerializer; // Array value $data = array('some-feature' => array('name' => 'toggling', 'conditions' => array(array('name' => 'operator-condition', 'key' => 'user_id', 'operator' => array('name' => 'greater-than', 'value' => 41))), 'status' => 'conditionally-active')); // Create the ToggleManager $serializer = new InMemoryCollectionSerializer(); $collection = $serializer->deserialize($data); $manager = new ToggleManager($collection); // Create and check a new context for a user with id 42 $context = new Context(); $context->set('user_id', 42); var_dump($manager->active('some-feature', $context)); // true // Create and check a new context for a user with id 21 $context = new Context(); $context->set('user_id', 21); var_dump($manager->active('some-feature', $context)); // false
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Qandidate\Toggle\Context; use Qandidate\Toggle\Operator\LessThan; use Qandidate\Toggle\OperatorCondition; use Qandidate\Toggle\Toggle; use Qandidate\Toggle\ToggleCollection\InMemoryCollection; use Qandidate\Toggle\ToggleManager; // Create the ToggleManager $manager = new ToggleManager(new InMemoryCollection()); // A toggle that will be active is the user id is less than 42 $operator = new LessThan(42); $condition = new OperatorCondition('user_id', $operator); $toggle = new Toggle('toggling', array($condition)); // Add the toggle to the manager $manager->add($toggle); // Create and check a new context for a user with id 42 $context = new Context(); $context->set('user_id', 42); var_dump($manager->active('toggling', $context)); // false // Create and check a new context for a user with id 21 $context = new Context(); $context->set('user_id', 21); var_dump($manager->active('toggling', $context)); // true
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Qandidate\Toggle\Operator\LessThan; use Qandidate\Toggle\OperatorCondition; use Qandidate\Toggle\Toggle; use Qandidate\Toggle\ToggleCollection\PredisCollection; use Qandidate\Toggle\ToggleManager; // Create the ToggleManager $predis = new Predis\Client(); $collection = new PredisCollection('toggle_demo', $predis); $manager = new ToggleManager($collection); // A toggle that will be active when the user id is less than 42 $operator = new LessThan(42); $condition = new OperatorCondition('user_id', $operator); $toggle = new Toggle('toggling', array($condition)); // Add the toggle to the manager $manager->add($toggle);
/** * @param string $name * @return boolean */ public function isActive($name) { return $this->toggleManager->active($name, $this->getContext()); }