You are here

public static function PathChecker::validatePaths in Optimizely 8

Same name and namespace in other branches
  1. 8.0 src/PathChecker.php \Drupal\optimizely\PathChecker::validatePaths()

Validate the target paths.

Parameters

array $project_paths: An array of the paths to validate.

Return value

bool|string Boolean of TRUE if the paths are valid or a string of the path that failed.

2 calls to PathChecker::validatePaths()
AddUpdateForm::validateForm in src/AddUpdateForm.php
Check to make sure the project code is unique except for the default entry which uses the account ID but should support an additional entry to allow for custom settings.
AjaxEnable::enableDisable in src/AjaxEnable.php
Enable or disable the project.

File

src/PathChecker.php, line 22

Class

PathChecker
Provides static methods to check path validity, etc.

Namespace

Drupal\optimizely

Code

public static function validatePaths(array $project_paths) {

  // Validate entered paths to confirm the paths exist on the website.
  foreach ($project_paths as $path) {

    // Check for sitewide wildcard.
    if (strpos($path, '*') === 0) {

      // Must be just the wildcard itself with nothing trailing.
      if ($path != '*') {
        return $path;
      }
      return count($project_paths) == 1 ? TRUE : $path;
    }
    elseif (strpos($path, '*') !== FALSE) {
      $project_wildpath = substr($path, 0, -2);
      if (\Drupal::pathValidator()
        ->isValid($project_wildpath) == FALSE) {

        // Look for entries in url_alias.
        $query = \Drupal::database()
          ->query("SELECT * FROM {url_alias} WHERE\n            source LIKE :project_wildpath OR alias LIKE :project_wildpath", [
          ':project_wildpath' => $project_wildpath . '%',
        ]);
        $results = $query
          ->fetchCol(0);
        $project_wildpath_match = count($results);

        // No matches found for wildcard path.
        if (!$project_wildpath_match) {
          return $path;
        }
      }
    }
    elseif (strpos($path, '?') !== FALSE) {

      // Look for entries in menu_router.
      $project_parmpath = substr($path, 0, strpos($path, '?'));

      // Look for entry in url_alias table.
      if (self::lookupPathAlias($path) === FALSE && self::lookupSystemPath($path) === FALSE && \Drupal::pathValidator()
        ->isValid($project_parmpath) == FALSE) {
        return $path;
      }
    }
    elseif (\Drupal::pathValidator()
      ->isValid($path) == FALSE) {

      // Look for entry in url_alias table.
      if (self::lookupPathAlias($path) === FALSE && self::lookupSystemPath($path) === FALSE) {
        return $path;
      }
    }
  }
  return TRUE;
}