You are here

public function VersionParser::parseConstraints in Automatic Updates 7

Parses a constraint string into MultiConstraint and/or Constraint objects.

Parameters

string $constraints:

Return value

ConstraintInterface

File

vendor/composer/semver/src/VersionParser.php, line 229

Class

VersionParser
Version parser.

Namespace

Composer\Semver

Code

public function parseConstraints($constraints) {
  $prettyConstraint = $constraints;
  if (preg_match('{^([^,\\s]*?)@(' . implode('|', self::$stabilities) . ')$}i', $constraints, $match)) {
    $constraints = empty($match[1]) ? '*' : $match[1];
  }
  if (preg_match('{^(dev-[^,\\s@]+?|[^,\\s@]+?\\.x-dev)#.+$}i', $constraints, $match)) {
    $constraints = $match[1];
  }
  $orConstraints = preg_split('{\\s*\\|\\|?\\s*}', trim($constraints));
  $orGroups = array();
  foreach ($orConstraints as $constraints) {
    $andConstraints = preg_split('{(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)}', $constraints);
    if (count($andConstraints) > 1) {
      $constraintObjects = array();
      foreach ($andConstraints as $constraint) {
        foreach ($this
          ->parseConstraint($constraint) as $parsedConstraint) {
          $constraintObjects[] = $parsedConstraint;
        }
      }
    }
    else {
      $constraintObjects = $this
        ->parseConstraint($andConstraints[0]);
    }
    if (1 === count($constraintObjects)) {
      $constraint = $constraintObjects[0];
    }
    else {
      $constraint = new MultiConstraint($constraintObjects);
    }
    $orGroups[] = $constraint;
  }
  if (1 === count($orGroups)) {
    $constraint = $orGroups[0];
  }
  elseif (2 === count($orGroups) && $orGroups[0] instanceof MultiConstraint && $orGroups[1] instanceof MultiConstraint && 2 === count($orGroups[0]
    ->getConstraints()) && 2 === count($orGroups[1]
    ->getConstraints()) && ($a = (string) $orGroups[0]) && strpos($a, '[>=') === 0 && false !== ($posA = strpos($a, '<', 4)) && ($b = (string) $orGroups[1]) && strpos($b, '[>=') === 0 && false !== ($posB = strpos($b, '<', 4)) && substr($a, $posA + 2, -1) === substr($b, 4, $posB - 5)) {
    $constraint = new MultiConstraint(array(
      new Constraint('>=', substr($a, 4, $posA - 5)),
      new Constraint('<', substr($b, $posB + 2, -1)),
    ));
  }
  else {
    $constraint = new MultiConstraint($orGroups, false);
  }
  $constraint
    ->setPrettyString($prettyConstraint);
  return $constraint;
}