Vanilla\Addon::testRequirement PHP Method

testRequirement() private static method

Requirements are arrays in the following form: - ['and', [requirements]]: All requirements mast be valid. - ['or', [requirements]]: One of the requirements must be valid. - ['op' => '', 'v' => '']: An operator and version to compare. - ['op' => '-', 'v' => '', 'v2' => '']: Compare a range of versions.
private static testRequirement ( string $version, array $req ) : boolean
$version string The version to test.
$req array The requirement to test.
return boolean Returns **true** if the test passes or **false** otherwise.
    private static function testRequirement($version, $req)
    {
        if (isset($req[0])) {
            // This is a boolean group.
            $logic = $req[0];
            foreach ($req[1] as $part) {
                $valid = self::testRequirement($version, $part);
                if ($valid && $logic === 'or') {
                    return true;
                } elseif (!$valid && $logic === 'and') {
                    return false;
                }
            }
            return $logic === 'or' ? false : true;
        } else {
            // This is an individual requirement.
            $req += ['op' => '==', 'v' => '0.0', 'logic' => ',', 'v2' => '999999'];
            $op = $req['op'];
            if ($op === '-') {
                $valid = version_compare($version, $req['v'], '>=') && version_compare($version, $req['v2'], '<=');
            } else {
                $valid = version_compare($version, $req['v'], $op);
            }
            return $valid;
        }
    }