Example #1
0
    public static function validate($url)
    {
        if (empty($url)) {
            return false;
        }
        try {
            $url = new self($url);
        } catch (InvalidArgumentException $exception) {
            return false;
        }

        if (!$url->getScheme()) {
            return false;
        }

        return $url->isValid();
    }
Example #2
0
 public function compare($v)
 {
     if (!$this->isValid()) {
         return 0;
     }
     if (!$v instanceof self) {
         $v = new self($v);
     }
     if (!$v->isValid()) {
         return 0;
     }
     if ($this->majorVersion > $v->majorVersion) {
         return 1;
     } elseif ($this->majorVersion < $v->majorVersion) {
         return -1;
     }
     if ($this->minorVersion > $v->minorVersion) {
         return 1;
     } elseif ($this->minorVersion < $v->minorVersion) {
         return -1;
     }
     if ($this->patchVersion > $v->patchVersion) {
         return 1;
     } elseif ($this->patchVersion < $v->patchVersion) {
         return -1;
     }
     // 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
     if (!is_null($this->preReleaseVersion)) {
         // 1.0.0-xxx < 1.0.0
         if (is_null($v->preReleaseVersion)) {
             return -1;
         }
         $a = explode('.', $this->preReleaseVersion);
         $b = explode('.', $v->preReleaseVersion);
         foreach ($a as $i => $av) {
             if (!isset($b[$i])) {
                 return 1;
             }
             $bv = $b[$i];
             if (is_numeric($av)) {
                 if (!is_numeric($bv)) {
                     return -1;
                 } elseif ($av < $bv) {
                     return -1;
                 } elseif ($av > $bv) {
                     return 1;
                 }
             } elseif (is_numeric($bv)) {
                 return 1;
             } else {
                 // both A-Z
                 $ret = strcasecmp($av, $bv);
                 if ($ret > 0) {
                     return 1;
                 } elseif ($ret < 0) {
                     return -1;
                 }
             }
         }
         if (count($b) > count($a)) {
             return -1;
         }
     } elseif (!is_null($v->preReleaseVersion)) {
         return 1;
     }
     return 0;
 }